From 0ff810a2970689c72fc59828fe18a4a1761c0178 Mon Sep 17 00:00:00 2001 From: hhs <386998068@qq.com> Date: Sun, 14 Jun 2026 16:43:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20main.go=20=E6=9D=A1=E4=BB=B6=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=20PostgreSQL=20=E8=BF=9E=E6=8E=A5=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 store 包导入 - 当 storage.driver 为 postgres 时创建 pgxpool - 使用 defer 确保连接池正确关闭 - 添加日志记录连接状态 --- backend/cmd/server/main.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/backend/cmd/server/main.go b/backend/cmd/server/main.go index 829c415..8091719 100644 --- a/backend/cmd/server/main.go +++ b/backend/cmd/server/main.go @@ -19,6 +19,7 @@ import ( "github.com/hhs/camtalk/internal/logger" "github.com/hhs/camtalk/internal/orchestrator" "github.com/hhs/camtalk/internal/session" + "github.com/hhs/camtalk/internal/store" "github.com/hhs/camtalk/internal/ws" ) @@ -44,6 +45,21 @@ func main() { "addr", cfg.Server.Addr(), ) + // 初始化存储层(条件初始化 PostgreSQL) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + if cfg.Storage.Driver == "postgres" { + pool, err := store.NewPostgresPool(ctx, cfg.Storage.DSN) + if err != nil { + logger.Log.Fatalw("failed to connect to postgres", "error", err) + } + defer pool.Close() + logger.Log.Infow("postgres connected", "driver", cfg.Storage.Driver) + // TODO: Phase 2 - 初始化 UserRepository 和 MessageRepository + _ = pool + } + // 初始化 Session Manager(MVP 默认内存实现) var sessionMgr session.Manager // TODO: 当 Redis 配置非空时切换为 RedisManager