feat: bootstrap control plane app skeleton
This commit is contained in:
28
cmd/cli/main.go
Normal file
28
cmd/cli/main.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
|
||||
"sub2api-cn-relay-manager/internal/config"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := execute(context.Background(), log.Writer(), func(context.Context) (config.StartupConfig, error) {
|
||||
return config.LoadStartupFromEnv()
|
||||
}); err != nil {
|
||||
log.Fatalf("run cli: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func execute(ctx context.Context, output io.Writer, loadConfig func(context.Context) (config.StartupConfig, error)) error {
|
||||
cfg, err := loadConfig(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = fmt.Fprintf(output, "sub2api-cn-relay-manager cli ready\nlisten_addr=%s\nsqlite_dsn=%s\n", cfg.Server.ListenAddr, cfg.Database.SQLiteDSN)
|
||||
return err
|
||||
}
|
||||
79
cmd/cli/main_test.go
Normal file
79
cmd/cli/main_test.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"sub2api-cn-relay-manager/internal/config"
|
||||
)
|
||||
|
||||
type errWriter struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (w errWriter) Write([]byte) (int, error) {
|
||||
return 0, w.err
|
||||
}
|
||||
|
||||
func TestExecuteWritesConfigSummaryAfterBootstrap(t *testing.T) {
|
||||
var output bytes.Buffer
|
||||
loadCalled := false
|
||||
|
||||
err := execute(context.Background(), &output, func(context.Context) (config.StartupConfig, error) {
|
||||
loadCalled = true
|
||||
return config.StartupConfig{
|
||||
Server: config.ServerConfig{ListenAddr: ":9191"},
|
||||
Database: config.DatabaseConfig{
|
||||
SQLiteDSN: "file:test.db?_foreign_keys=on",
|
||||
},
|
||||
}, nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("execute() returned error: %v", err)
|
||||
}
|
||||
|
||||
if !loadCalled {
|
||||
t.Fatal("execute() did not load config")
|
||||
}
|
||||
|
||||
got := output.String()
|
||||
if !strings.Contains(got, "sub2api-cn-relay-manager cli ready") {
|
||||
t.Fatalf("execute() output = %q, want readiness line", got)
|
||||
}
|
||||
|
||||
if !strings.Contains(got, "listen_addr=:9191") {
|
||||
t.Fatalf("execute() output = %q, want listen addr summary", got)
|
||||
}
|
||||
|
||||
if !strings.Contains(got, "sqlite_dsn=file:test.db?_foreign_keys=on") {
|
||||
t.Fatalf("execute() output = %q, want sqlite dsn summary", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteReturnsBootstrapError(t *testing.T) {
|
||||
wantErr := errors.New("load config failed")
|
||||
|
||||
err := execute(context.Background(), &bytes.Buffer{}, func(context.Context) (config.StartupConfig, error) {
|
||||
return config.StartupConfig{}, wantErr
|
||||
})
|
||||
if !errors.Is(err, wantErr) {
|
||||
t.Fatalf("execute() error = %v, want %v", err, wantErr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteReturnsWriteError(t *testing.T) {
|
||||
wantErr := errors.New("write failed")
|
||||
|
||||
err := execute(context.Background(), errWriter{err: wantErr}, func(context.Context) (config.StartupConfig, error) {
|
||||
return config.StartupConfig{
|
||||
Server: config.ServerConfig{ListenAddr: ":9292"},
|
||||
Database: config.DatabaseConfig{SQLiteDSN: "file:test.db"},
|
||||
}, nil
|
||||
})
|
||||
if !errors.Is(err, wantErr) {
|
||||
t.Fatalf("execute() error = %v, want %v", err, wantErr)
|
||||
}
|
||||
}
|
||||
31
cmd/server/main.go
Normal file
31
cmd/server/main.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"sub2api-cn-relay-manager/internal/app"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
if err := run(ctx, app.Bootstrap, func(ctx context.Context, server *app.Server) error {
|
||||
return server.Run(ctx)
|
||||
}); err != nil {
|
||||
log.Fatalf("run server: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func run(ctx context.Context, bootstrap func(context.Context) (*app.Server, error), runServer func(context.Context, *app.Server) error) error {
|
||||
server, err := bootstrap(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return runServer(ctx, server)
|
||||
}
|
||||
77
cmd/server/main_test.go
Normal file
77
cmd/server/main_test.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"sub2api-cn-relay-manager/internal/app"
|
||||
)
|
||||
|
||||
func TestRunCallsApplicationServerRunnerAfterBootstrap(t *testing.T) {
|
||||
serverApp := app.NewServer("127.0.0.1:0", nil)
|
||||
bootstrapCalled := false
|
||||
runnerCalled := false
|
||||
|
||||
err := run(
|
||||
context.Background(),
|
||||
func(context.Context) (*app.Server, error) {
|
||||
bootstrapCalled = true
|
||||
return serverApp, nil
|
||||
},
|
||||
func(_ context.Context, got *app.Server) error {
|
||||
runnerCalled = true
|
||||
if got != serverApp {
|
||||
t.Fatal("run() passed unexpected server instance to runner")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("run() returned error: %v", err)
|
||||
}
|
||||
|
||||
if !bootstrapCalled {
|
||||
t.Fatal("run() did not call bootstrap")
|
||||
}
|
||||
|
||||
if !runnerCalled {
|
||||
t.Fatal("run() did not call server runner")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunReturnsBootstrapError(t *testing.T) {
|
||||
wantErr := errors.New("bootstrap failed")
|
||||
|
||||
err := run(
|
||||
context.Background(),
|
||||
func(context.Context) (*app.Server, error) {
|
||||
return nil, wantErr
|
||||
},
|
||||
func(context.Context, *app.Server) error {
|
||||
t.Fatal("run() called runner after bootstrap error")
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if !errors.Is(err, wantErr) {
|
||||
t.Fatalf("run() error = %v, want %v", err, wantErr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunReturnsApplicationRunError(t *testing.T) {
|
||||
wantErr := errors.New("server run failed")
|
||||
serverApp := app.NewServer("127.0.0.1:0", nil)
|
||||
|
||||
err := run(
|
||||
context.Background(),
|
||||
func(context.Context) (*app.Server, error) {
|
||||
return serverApp, nil
|
||||
},
|
||||
func(context.Context, *app.Server) error {
|
||||
return wantErr
|
||||
},
|
||||
)
|
||||
if !errors.Is(err, wantErr) {
|
||||
t.Fatalf("run() error = %v, want %v", err, wantErr)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user