feat: bootstrap control plane app skeleton

This commit is contained in:
phamnazage-jpg
2026-05-12 22:44:30 +08:00
parent 1c02fcdaa7
commit 9d52b22b8d
10 changed files with 606 additions and 0 deletions

31
cmd/server/main.go Normal file
View 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
View 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)
}
}