- ServeWS 和 serveWS 函数新增 *auth.TokenManager 参数 - main.go 传入 tokenMgr 到 ServeWS - handler_test.go 适配新签名
196 lines
6.2 KiB
Go
196 lines
6.2 KiB
Go
package main
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"net/http"
|
||
"os/signal"
|
||
"strings"
|
||
"syscall"
|
||
"time"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
|
||
"github.com/hhs/camtalk/internal/api"
|
||
"github.com/hhs/camtalk/internal/auth"
|
||
"github.com/hhs/camtalk/internal/ai/llm"
|
||
"github.com/hhs/camtalk/internal/ai/stt"
|
||
"github.com/hhs/camtalk/internal/ai/tts"
|
||
"github.com/hhs/camtalk/internal/config"
|
||
"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"
|
||
)
|
||
|
||
// Version 通过构建时 -ldflags 注入,如:
|
||
// go build -ldflags "-X main.Version=v1.0.0" ./cmd/server
|
||
var Version string
|
||
|
||
var startTime = time.Now()
|
||
|
||
func main() {
|
||
// 加载配置
|
||
cfg, err := config.Load()
|
||
if err != nil {
|
||
panic("failed to load config: " + err.Error())
|
||
}
|
||
|
||
// 初始化日志
|
||
logger.Init(cfg.Log.Level, cfg.Log.Format)
|
||
defer logger.Sync()
|
||
|
||
logger.Log.Infow("config loaded",
|
||
"env", cfg.App.Env,
|
||
"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
|
||
}
|
||
|
||
// 初始化 UserRepository(内存模式用于无 DB 场景)
|
||
var userRepo store.UserRepository
|
||
userRepo = store.NewMemUserRepository()
|
||
|
||
// 初始化 Session Manager(MVP 默认内存实现)
|
||
var sessionMgr session.Manager
|
||
// TODO: 当 Redis 配置非空时切换为 RedisManager
|
||
sessionMgr = session.NewMemoryManager(
|
||
time.Duration(cfg.Session.TTL)*time.Minute,
|
||
cfg.Session.MaxHistory,
|
||
)
|
||
defer sessionMgr.(*session.MemoryManager).Stop()
|
||
|
||
// 初始化 AI 服务
|
||
logger.Log.Infow("initializing AI services",
|
||
"stt.provider", cfg.AI.STT.Provider,
|
||
"stt.model", cfg.AI.STT.Model,
|
||
"llm.provider", cfg.AI.LLM.Provider,
|
||
"llm.model", cfg.AI.LLM.Model,
|
||
"tts.provider", cfg.AI.TTS.Provider,
|
||
"tts.model", cfg.AI.TTS.Model,
|
||
"tts.voice", cfg.AI.TTS.Voice,
|
||
)
|
||
|
||
var sttService stt.Service
|
||
switch strings.ToLower(cfg.AI.STT.Provider) {
|
||
case "mimo", "xiaomi":
|
||
sttService = stt.NewMiMoService(cfg.AI.STT.APIKey, cfg.AI.STT.Model, cfg.AI.STT.Endpoint, cfg.AI.STT.Timeout, logger.Log)
|
||
logger.Log.Infow("STT service initialized", "provider", "mimo", "model", cfg.AI.STT.Model, "endpoint", cfg.AI.STT.Endpoint)
|
||
default:
|
||
sttService = stt.NewDeepgramService(cfg.AI.STT.APIKey, cfg.AI.STT.Model, cfg.AI.STT.Endpoint, cfg.AI.STT.Timeout, logger.Log)
|
||
logger.Log.Infow("STT service initialized", "provider", "deepgram", "model", cfg.AI.STT.Model)
|
||
}
|
||
llmService := llm.NewOpenAIService(cfg.AI.LLM.APIKey, cfg.AI.LLM.Model, cfg.AI.LLM.Endpoint, cfg.AI.LLM.Timeout, cfg.AI.LLM.HTTPClientTimeout, logger.Log)
|
||
logger.Log.Infow("LLM service initialized", "provider", cfg.AI.LLM.Provider, "model", cfg.AI.LLM.Model, "endpoint", cfg.AI.LLM.Endpoint, "timeout", cfg.AI.LLM.Timeout)
|
||
|
||
var ttsService tts.Service
|
||
switch strings.ToLower(cfg.AI.TTS.Provider) {
|
||
case "mimo", "xiaomi":
|
||
ttsService = tts.NewMiMoService(cfg.AI.TTS.APIKey, cfg.AI.TTS.Model, cfg.AI.TTS.Voice, cfg.AI.TTS.Endpoint, cfg.AI.TTS.Timeout, cfg.AI.TTS.HTTPClientTimeout, logger.Log)
|
||
logger.Log.Infow("TTS service initialized", "provider", "mimo", "model", cfg.AI.TTS.Model, "voice", cfg.AI.TTS.Voice, "endpoint", cfg.AI.TTS.Endpoint)
|
||
default:
|
||
ttsService = tts.NewOpenAIService(cfg.AI.TTS.APIKey, cfg.AI.TTS.Model, cfg.AI.TTS.Voice, cfg.AI.TTS.Endpoint, cfg.AI.TTS.Speed, cfg.AI.TTS.Timeout, cfg.AI.TTS.HTTPClientTimeout, logger.Log)
|
||
logger.Log.Infow("TTS service initialized", "provider", "openai", "model", cfg.AI.TTS.Model, "voice", cfg.AI.TTS.Voice, "speed", cfg.AI.TTS.Speed)
|
||
}
|
||
|
||
// 初始化 Orchestrator
|
||
orch := orchestrator.New(sttService, llmService, ttsService, sessionMgr, cfg)
|
||
|
||
// 初始化认证服务
|
||
tokenMgr := auth.NewTokenManager(
|
||
cfg.Auth.JWTSecret,
|
||
time.Duration(cfg.Auth.AccessTTL)*time.Minute,
|
||
time.Duration(cfg.Auth.RefreshTTL)*time.Minute,
|
||
)
|
||
authService := auth.NewAuthService(tokenMgr, userRepo)
|
||
|
||
// Gin 模式
|
||
if cfg.App.Env == "prod" {
|
||
gin.SetMode(gin.ReleaseMode)
|
||
}
|
||
|
||
r := gin.New()
|
||
r.Use(gin.Recovery())
|
||
|
||
// REST API
|
||
apiGroup := r.Group("/api")
|
||
{
|
||
apiGroup.GET("/health", healthHandler(sessionMgr, cfg))
|
||
}
|
||
|
||
// Session REST 端点
|
||
sessionHandler := api.NewSessionHandler(sessionMgr)
|
||
sessionHandler.RegisterRoutes(apiGroup)
|
||
|
||
// Auth REST 端点
|
||
authHandler := api.NewAuthHandler(authService, tokenMgr)
|
||
authHandler.RegisterRoutes(apiGroup)
|
||
|
||
// Conversation REST 端点
|
||
convHandler := api.NewConversationHandler(sessionMgr, tokenMgr)
|
||
convHandler.RegisterRoutes(apiGroup)
|
||
|
||
// WebSocket
|
||
r.GET("/ws", ws.ServeWS(sessionMgr, orch, cfg, tokenMgr))
|
||
|
||
// HTTP Server
|
||
srv := &http.Server{
|
||
Addr: cfg.Server.Addr(),
|
||
Handler: r,
|
||
ReadTimeout: time.Duration(cfg.Server.ReadTimeout) * time.Second,
|
||
WriteTimeout: time.Duration(cfg.Server.WriteTimeout) * time.Second,
|
||
}
|
||
|
||
// Graceful shutdown
|
||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||
defer stop()
|
||
|
||
go func() {
|
||
logger.Log.Infow("server starting", "addr", srv.Addr)
|
||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||
logger.Log.Fatalw("listen failed", "error", err)
|
||
}
|
||
}()
|
||
|
||
<-ctx.Done()
|
||
logger.Log.Info("shutting down...")
|
||
|
||
shutdownCtx, cancel := context.WithTimeout(context.Background(), time.Duration(cfg.Server.ShutdownTimeout)*time.Second)
|
||
defer cancel()
|
||
|
||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||
logger.Log.Errorw("shutdown error", "error", err)
|
||
}
|
||
logger.Log.Info("server stopped")
|
||
}
|
||
|
||
// healthHandler 健康检查。
|
||
func healthHandler(sessionMgr session.Manager, cfg *config.Config) gin.HandlerFunc {
|
||
return func(c *gin.Context) {
|
||
version := Version
|
||
if version == "" {
|
||
version = cfg.App.Version
|
||
}
|
||
c.JSON(200, gin.H{
|
||
"status": "ok",
|
||
"version": version,
|
||
"uptime_seconds": int(time.Since(startTime).Seconds()),
|
||
"active_sessions": sessionMgr.ActiveCount(),
|
||
})
|
||
}
|
||
}
|