diff --git a/cmd/server/main.go b/cmd/server/main.go index 0e99147..e0a93f6 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -523,14 +523,14 @@ func buildServerRuntime(opts serverOptions, infrastructure serverInfrastructure) // authentication 负责 HTTP 会话认证;Secure 策略由启动参数控制。 authentication := &auth.Service{Store: infrastructure.store, Logger: infrastructure.logger, Secure: opts.secure} // compositionServer 仅在迁移期间复用旧应用服务装配逻辑;它不绑定端口、不启动 worker,也不作为 HTTP Server 运行。 - compositionServer, compositionErr := server.New(authentication, mgr, opts.webDir, opts.addr, infrastructure.logger, autoCenter, notifier, server.WithChatService(chatService), server.WithChatDependencies(chatDependencies), server.WithDatabaseHealth(databaseHealth), server.WithOrderReconciliationRecovery(orderReconciliationRecovery), server.WithOrderDependencies(orderDependencies), server.WithAccountDependencies(accountDependencies), server.WithItemDependencies(itemDependencies), server.WithAutomationDependencies(automationDependencies), server.WithTransportApplicationServices(transportApplications), server.WithPlatformDependencies(platformDependencies), server.WithApplicationLifecycle(lifecycleCoordinator)) + compositionServer, compositionErr := server.NewLegacyComposedServer(authentication, mgr, opts.webDir, opts.addr, infrastructure.logger, autoCenter, notifier, server.WithChatService(chatService), server.WithChatDependencies(chatDependencies), server.WithDatabaseHealth(databaseHealth), server.WithOrderReconciliationRecovery(orderReconciliationRecovery), server.WithOrderDependencies(orderDependencies), server.WithAccountDependencies(accountDependencies), server.WithItemDependencies(itemDependencies), server.WithAutomationDependencies(automationDependencies), server.WithTransportApplicationServices(transportApplications), server.WithPlatformDependencies(platformDependencies), server.WithApplicationLifecycle(lifecycleCoordinator)) if compositionErr != nil { return serverRuntime{}, fmt.Errorf("构造应用服务集合失败: %w", compositionErr) } // applications 是组合期一次性构造的只读应用服务集合,HTTP Server 不得在自身构造期间补建业务服务。 applications := compositionServer.ApplicationServices() // httpServer 是只接收完整依赖快照的 HTTP transport 实例。 - httpServer, serverErr := server.NewWithDependencies(server.Dependencies{ + httpServer, serverErr := server.New(server.Dependencies{ Auth: authentication, Manager: mgr, WebDir: opts.webDir, Addr: opts.addr, Logger: infrastructure.logger, Automation: autoCenter, Notifier: notifier, Chat: chatService, OrderDependencies: orderDependencies, AccountDependencies: accountDependencies, ItemDependencies: itemDependencies, ChatDependencies: chatDependencies, diff --git a/internal/server/run_test.go b/internal/server/run_test.go index 6b4a5bb..7ef61cc 100644 --- a/internal/server/run_test.go +++ b/internal/server/run_test.go @@ -104,7 +104,7 @@ func TestPublishRecoveryLifecycleStopsBeforeWorkerWait(t *testing.T) { // TestNewRejectsMissingRequiredDependencies 确保 HTTP 服务构造阶段拒绝缺失核心依赖。 func TestNewRejectsMissingRequiredDependencies(t *testing.T) { // err 是缺少基础设施容器时的构造校验错误。 - if _, err := New(nil, nil, "", ":0", nil, nil, nil); err == nil { + if _, err := NewLegacyComposedServer(nil, nil, "", ":0", nil, nil, nil); err == nil { t.Fatal("缺少基础设施容器时应返回构造错误") } // srv 是用于提供合法 Store 的测试服务;cleanup 负责释放测试数据库。 @@ -116,11 +116,11 @@ func TestNewRejectsMissingRequiredDependencies(t *testing.T) { // authentication 保存构造校验需要的会话中间件依赖。 authentication := &auth.Service{Store: store} // err 是缺少 Manager 时的构造校验错误。 - if _, err := New(authentication, nil, "", ":0", nil, nil, nil); err == nil { + if _, err := NewLegacyComposedServer(authentication, nil, "", ":0", nil, nil, nil); err == nil { t.Fatal("缺少 account.Manager 时应返回构造错误") } // err 是缺少订单专用装配能力时的构造校验错误。 - if _, err := New(authentication, srv.Manager, "", ":0", nil, nil, nil); err == nil { + if _, err := NewLegacyComposedServer(authentication, srv.Manager, "", ":0", nil, nil, nil); err == nil { t.Fatal("缺少订单专用依赖时应返回构造错误") } // orderDependencies 保存账号依赖缺失测试所需的合法订单装配能力。 @@ -129,7 +129,7 @@ func TestNewRejectsMissingRequiredDependencies(t *testing.T) { t.Fatalf("NewOrderDependencies: %v", orderDependencyErr) } // err 是缺少账号专用装配能力时的构造校验错误。 - if _, err := New(authentication, srv.Manager, "", ":0", nil, nil, nil, WithOrderDependencies(orderDependencies)); err == nil { + if _, err := NewLegacyComposedServer(authentication, srv.Manager, "", ":0", nil, nil, nil, WithOrderDependencies(orderDependencies)); err == nil { t.Fatal("缺少账号专用依赖时应返回构造错误") } // accountDependencies 保存账号与订单依赖均合法时的商品依赖缺失测试输入。 @@ -138,7 +138,7 @@ func TestNewRejectsMissingRequiredDependencies(t *testing.T) { t.Fatalf("NewAccountDependencies: %v", accountDependencyErr) } // err 是缺少商品专用装配能力时的构造校验错误。 - if _, err := New(authentication, srv.Manager, "", ":0", nil, nil, nil, WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies)); err == nil { + if _, err := NewLegacyComposedServer(authentication, srv.Manager, "", ":0", nil, nil, nil, WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies)); err == nil { t.Fatal("缺少商品专用依赖时应返回构造错误") } // err 是订单、账号和商品依赖齐全但平台客户端缺失时的构造校验错误。 @@ -189,38 +189,38 @@ func TestNewRejectsMissingRequiredDependencies(t *testing.T) { t.Fatalf("NewReconciliationRecoveryCoordinator: %v", reconciliationErr) } // err 是聊天或健康检查端口等显式依赖缺失时的构造校验错误。 - if _, err := New(authentication, srv.Manager, "", ":0", nil, nil, nil, WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies)); err == nil { + if _, err := NewLegacyComposedServer(authentication, srv.Manager, "", ":0", nil, nil, nil, WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies)); err == nil { t.Fatal("缺少聊天或健康检查端口时应返回构造错误") } // err 是全部业务依赖齐全但遗漏健康检查端口时的构造错误。 - if _, err := New(authentication, srv.Manager, "", ":0", nil, nil, nil, WithChatDependencies(chatDependencies), WithOrderReconciliationRecovery(orderReconciliationRecovery), WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies), WithAutomationDependencies(automationDependencies), WithTransportApplicationServices(transportApplications), WithPlatformDependencies(platformDependencies)); err == nil { + if _, err := NewLegacyComposedServer(authentication, srv.Manager, "", ":0", nil, nil, nil, WithChatDependencies(chatDependencies), WithOrderReconciliationRecovery(orderReconciliationRecovery), WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies), WithAutomationDependencies(automationDependencies), WithTransportApplicationServices(transportApplications), WithPlatformDependencies(platformDependencies)); err == nil { t.Fatal("缺少数据库健康检查端口时应返回构造错误") } // err 是缺少平台依赖注入时的构造校验错误。 - if _, err := New(authentication, srv.Manager, "", ":0", nil, nil, nil, WithChatDependencies(chatDependencies), WithDatabaseHealth(databaseHealth), WithOrderReconciliationRecovery(orderReconciliationRecovery), WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies), WithAutomationDependencies(automationDependencies), WithTransportApplicationServices(transportApplications)); err == nil { + if _, err := NewLegacyComposedServer(authentication, srv.Manager, "", ":0", nil, nil, nil, WithChatDependencies(chatDependencies), WithDatabaseHealth(databaseHealth), WithOrderReconciliationRecovery(orderReconciliationRecovery), WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies), WithAutomationDependencies(automationDependencies), WithTransportApplicationServices(transportApplications)); err == nil { t.Fatal("缺少平台依赖时应返回构造错误") } // err 是缺少自动化专用依赖时的构造校验错误。 - if _, err := New(authentication, srv.Manager, "", ":0", nil, nil, nil, WithChatDependencies(chatDependencies), WithDatabaseHealth(databaseHealth), WithOrderReconciliationRecovery(orderReconciliationRecovery), WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies), WithTransportApplicationServices(transportApplications), WithPlatformDependencies(platformDependencies)); err == nil { + if _, err := NewLegacyComposedServer(authentication, srv.Manager, "", ":0", nil, nil, nil, WithChatDependencies(chatDependencies), WithDatabaseHealth(databaseHealth), WithOrderReconciliationRecovery(orderReconciliationRecovery), WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies), WithTransportApplicationServices(transportApplications), WithPlatformDependencies(platformDependencies)); err == nil { t.Fatal("缺少自动化专用依赖时应返回构造错误") } // err 是缺少 transport 应用服务集合时的构造校验错误。 - if _, err := New(authentication, srv.Manager, "", ":0", nil, nil, nil, WithChatDependencies(chatDependencies), WithDatabaseHealth(databaseHealth), WithOrderReconciliationRecovery(orderReconciliationRecovery), WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies), WithAutomationDependencies(automationDependencies), WithPlatformDependencies(platformDependencies)); err == nil { + if _, err := NewLegacyComposedServer(authentication, srv.Manager, "", ":0", nil, nil, nil, WithChatDependencies(chatDependencies), WithDatabaseHealth(databaseHealth), WithOrderReconciliationRecovery(orderReconciliationRecovery), WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies), WithAutomationDependencies(automationDependencies), WithPlatformDependencies(platformDependencies)); err == nil { t.Fatal("缺少 transport 应用服务集合时应返回构造错误") } // err 是全部基础设施依赖齐全但遗漏进程装配应用服务时的构造错误。 - if _, err := New(authentication, srv.Manager, "", ":0", nil, nil, nil, WithChatDependencies(chatDependencies), WithDatabaseHealth(databaseHealth), WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies), WithAutomationDependencies(automationDependencies), WithTransportApplicationServices(transportApplications), WithPlatformDependencies(platformDependencies)); err == nil { + if _, err := NewLegacyComposedServer(authentication, srv.Manager, "", ":0", nil, nil, nil, WithChatDependencies(chatDependencies), WithDatabaseHealth(databaseHealth), WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies), WithAutomationDependencies(automationDependencies), WithTransportApplicationServices(transportApplications), WithPlatformDependencies(platformDependencies)); err == nil { t.Fatal("缺少订单补偿扫描应用服务时应返回构造错误") } // fullyConstructedServer、constructErr 分别是完整注入后的 HTTP 服务及构造错误。 - fullyConstructedServer, constructErr := New(authentication, srv.Manager, "", ":0", nil, nil, nil, WithChatDependencies(chatDependencies), WithDatabaseHealth(databaseHealth), WithOrderReconciliationRecovery(orderReconciliationRecovery), WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies), WithAutomationDependencies(automationDependencies), WithTransportApplicationServices(transportApplications), WithPlatformDependencies(platformDependencies)) + fullyConstructedServer, constructErr := NewLegacyComposedServer(authentication, srv.Manager, "", ":0", nil, nil, nil, WithChatDependencies(chatDependencies), WithDatabaseHealth(databaseHealth), WithOrderReconciliationRecovery(orderReconciliationRecovery), WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies), WithAutomationDependencies(automationDependencies), WithTransportApplicationServices(transportApplications), WithPlatformDependencies(platformDependencies)) if constructErr != nil || fullyConstructedServer == nil { t.Fatalf("完整依赖注入应构造成功: server=%v err=%v", fullyConstructedServer, constructErr) } } -// TestNewWithDependenciesAcceptsPrebuiltApplicationSet 验证新组合根入口只接收已经装配完成的应用服务集合。 -func TestNewWithDependenciesAcceptsPrebuiltApplicationSet(t *testing.T) { +// TestNewAcceptsPrebuiltApplicationSet 验证新组合根入口只接收已经装配完成的应用服务集合。 +func TestNewAcceptsPrebuiltApplicationSet(t *testing.T) { // source、cleanup 提供一个由兼容测试装配器构造完成的完整服务集合。 source, _, cleanup := newTestServer(t) defer cleanup() @@ -236,9 +236,9 @@ func TestNewWithDependenciesAcceptsPrebuiltApplicationSet(t *testing.T) { ApplicationLifecycle: source.applicationLifecycle, } // serverInstance、constructErr 保存纯注入入口的构造结果。 - serverInstance, constructErr := NewWithDependencies(dependencies) + serverInstance, constructErr := New(dependencies) if constructErr != nil { - t.Fatalf("NewWithDependencies: %v", constructErr) + t.Fatalf("New: %v", constructErr) } if serverInstance == nil || serverInstance.applications == nil || serverInstance.applications.orders != source.applications.orders { t.Fatal("新组合根入口未保留预构造应用服务集合") diff --git a/internal/server/server.go b/internal/server/server.go index 79cd11c..d86a2a5 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -50,7 +50,7 @@ type qrLoginPersistence struct { type ServerOption func(*Server) // Dependencies 是 HTTP Server 的不可变组合依赖;应用服务和生命周期组件必须在进入 Server 前完成装配。 -// 旧的 ServerOption 构造器仅作为迁移期间兼容入口,生产代码应使用 NewWithDependencies。 +// 旧的 ServerOption 构造器仅作为迁移期间兼容入口,生产代码应使用 New。 type Dependencies struct { // Auth 是负责会话解析和认证中间件的应用认证服务。 Auth *auth.Service @@ -311,8 +311,8 @@ func WithApplicationLifecycle(coordinator *lifecycleapp.Coordinator) ServerOptio } } -// NewWithDependencies 构造纯 HTTP transport Server;所有应用服务、平台客户端和生命周期组件必须已由组合根创建。 -func NewWithDependencies(dependencies Dependencies) (*Server, error) { +// New 构造纯 HTTP transport Server;所有应用服务、平台客户端和生命周期组件必须已由组合根创建。 +func New(dependencies Dependencies) (*Server, error) { if dependencies.Auth == nil { return nil, fmt.Errorf("server 依赖认证服务不能为空") } @@ -378,9 +378,9 @@ func (s *Server) ApplicationServices() *ApplicationServices { return &copiedApplications } -// New 构造并校验 HTTP 服务所需依赖;订单、账号、商品、系统和平台能力必须由显式依赖边界注入。 -// autoCenter/notifier 由调用方完成创建后注入,构造顺序为 adapter → manager → automation → notifier → server。 -func New(authentication *auth.Service, manager *account.Manager, webDir, addr string, logger *slog.Logger, autoCenter *automation.Center, notifier *notify.Notifier, options ...ServerOption) (*Server, error) { +// NewLegacyComposedServer 是迁移期兼容构造器,会在 Server 内构建应用服务。 +// 删除条件:cmd/server 和所有测试均改为由 adapter/组合根构造 ApplicationServices 后,删除本函数及 ServerOption。 +func NewLegacyComposedServer(authentication *auth.Service, manager *account.Manager, webDir, addr string, logger *slog.Logger, autoCenter *automation.Center, notifier *notify.Notifier, options ...ServerOption) (*Server, error) { if authentication == nil { return nil, fmt.Errorf("server 依赖认证服务不能为空") } diff --git a/internal/server/server_test.go b/internal/server/server_test.go index acf8947..a58da80 100644 --- a/internal/server/server_test.go +++ b/internal/server/server_test.go @@ -96,7 +96,7 @@ func newTestServer(t *testing.T) (*Server, *db.Store, func()) { // newTestServer // authentication 保存测试 HTTP 会话中间件需要的认证服务。 authentication := &auth.Service{Store: store} // srv、err 保存测试 HTTP 服务构造结果及失败原因。 - srv, err := New(authentication, mgr, "", ":0", nil, nil, nil, WithChatDependencies(chatDependencies), WithDatabaseHealth(databaseHealth), WithOrderReconciliationRecovery(orderReconciliationRecovery), WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies), WithAutomationDependencies(automationDependencies), WithTransportApplicationServices(transportApplications), WithPlatformDependencies(platformDependencies)) + srv, err := NewLegacyComposedServer(authentication, mgr, "", ":0", nil, nil, nil, WithChatDependencies(chatDependencies), WithDatabaseHealth(databaseHealth), WithOrderReconciliationRecovery(orderReconciliationRecovery), WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies), WithAutomationDependencies(automationDependencies), WithTransportApplicationServices(transportApplications), WithPlatformDependencies(platformDependencies)) if err != nil { t.Fatalf("New: %v", err) } @@ -204,7 +204,7 @@ func newUninitializedTestServer(t *testing.T) (*Server, *db.Store, func()) { // authentication 保存未初始化数据库上的会话中间件依赖。 authentication := &auth.Service{Store: store} // srv、err 保存未初始化测试服务构造结果及失败原因。 - srv, err := New(authentication, mgr, "", ":0", nil, nil, nil, WithChatDependencies(chatDependencies), WithDatabaseHealth(databaseHealth), WithOrderReconciliationRecovery(orderReconciliationRecovery), WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies), WithAutomationDependencies(automationDependencies), WithTransportApplicationServices(transportApplications), WithPlatformDependencies(platformDependencies)) + srv, err := NewLegacyComposedServer(authentication, mgr, "", ":0", nil, nil, nil, WithChatDependencies(chatDependencies), WithDatabaseHealth(databaseHealth), WithOrderReconciliationRecovery(orderReconciliationRecovery), WithOrderDependencies(orderDependencies), WithAccountDependencies(accountDependencies), WithItemDependencies(itemDependencies), WithAutomationDependencies(automationDependencies), WithTransportApplicationServices(transportApplications), WithPlatformDependencies(platformDependencies)) if err != nil { t.Fatalf("New: %v", err) }