refactor: complete backend rewrite for multi-protocol proxy

Major rewrite of the Go backend to support:
- Three API format imports: openai, anthropic, compatible
- Three protocol conversions: Chat Completions, Responses, Messages
- Hub-and-spoke architecture with Chat as intermediate format

Deleted:
- opencat.go (old entry)
- store/, team/, pkg/team/, pkg/store/ (old data layer)
- internal/model/, internal/consts/ (old types)
- internal/service/team/, internal/controller/team/ (old handlers)
- llm/ (removed LLM client library, pure proxy mode)
- dist/, assets/ (old build artifacts)

Added:
- internal/store/ — 9 GORM models + multi-DB support
- internal/pkg/ — crypto (AES-GCM), apikey, jwt, ratelimit, resp, tokenizer
- internal/channel/ — channel selection, weighted LB, health checks
- internal/proxy/convert/ — 6 protocol conversion functions + SSE streaming
- internal/proxy/ — gateway with request dispatch and upstream selection
- internal/usage/ — async usage recorder with batch writes
- internal/api/ — management API (auth, users, keys, channels, models)
- Makefile for build/test/deploy

Fixed API to match frontend expectations:
- Login response wraps token in { data: { token } }
- GET /api/profile route added
- Profile response wraps user in { code, data }
- Role returned as number (10=admin, 1=user)
This commit is contained in:
Sakurasan
2026-08-30 11:49:31 +08:00
parent aa0d87f132
commit ef3025dd80
127 changed files with 4623 additions and 10500 deletions
+64 -80
View File
@@ -7,9 +7,13 @@ import (
"io/fs"
"log"
"net/http"
"opencatd-open/internal/api"
"opencatd-open/internal/channel"
"opencatd-open/internal/dao"
"opencatd-open/internal/proxy"
"opencatd-open/internal/usage"
"opencatd-open/middleware"
"opencatd-open/pkg/config"
"opencatd-open/wire"
"os"
"os/signal"
"sync"
@@ -33,100 +37,86 @@ func SetRouter(cfg *config.Config, db *gorm.DB, web *embed.FS) {
log.Fatalf("Failed to get underlying *sql.DB: %v", err)
}
team, err := wire.InitTeamHandler(ctx, cfg, db)
if err != nil {
panic(err)
}
// Initialize DAOs
userDAO := dao.NewUserDAO(db)
apiKeyDAO := dao.NewApiKeyDAO(db)
usageDAO := dao.NewUsageDAO(db)
dailyDAO := dao.NewDailyUsageDAO(db)
channelDAO := dao.NewChannelDAO(db)
modelDAO := dao.NewModelDAO(db)
api, err := wire.InitAPIHandler(ctx, cfg, db)
if err != nil {
panic(err)
}
// Initialize channel service
channelSvc := channel.NewService(channelDAO, modelDAO)
proxy, err := wire.InitProxyHandler(ctx, cfg, db, &wg)
if err != nil {
panic(err)
}
// Initialize health checker and start periodic checks
healthChecker := channel.NewHealthChecker(channelDAO, channelSvc)
go healthChecker.StartPeriodicCheck(ctx, 5*time.Minute)
// Initialize usage recorder and start background worker
usageRecorder := usage.NewRecorder(usageDAO, dailyDAO)
usageRecorder.Start(ctx)
defer usageRecorder.Stop()
// Initialize gateway
gateway := proxy.NewGateway(ctx, cfg, db, &wg, userDAO, apiKeyDAO, usageDAO, dailyDAO)
gateway.SetChannelService(channelSvc)
// Initialize API handler
apiHandler := api.NewHandler(db)
r := gin.Default()
r.Use(middleware.CORS())
teamGroup := r.Group("/1")
teamGroup.Use(team.AuthMiddleware())
{
teamGroup.POST("/users/init", team.InitAdmin)
// 获取当前用户信息
teamGroup.GET("/me", team.Me)
// team.GET("/me/usages", team.HandleMeUsage)
teamGroup.POST("/keys", team.CreateKey)
teamGroup.GET("/keys", team.ListKeys)
teamGroup.POST("/keys/:id", team.UpdateKey)
teamGroup.DELETE("/keys/:id", team.DeleteKey)
teamGroup.POST("/users", team.CreateUser)
teamGroup.GET("/users", team.ListUsers)
teamGroup.POST("/users/:id/reset", team.ResetUserToken)
teamGroup.DELETE("/users/:id", team.DeleteUser)
teamGroup.GET("/1/usages", team.ListUsages)
}
// Public auth routes
public := r.Group("/api/auth")
{
public.GET("/passkey/begin", api.PasskeyAuthBegin)
public.POST("/passkey/finish", api.PasskeyAuthFinish)
public.POST("/register", api.Register)
public.POST("/login", api.Login)
public.POST("/register", apiHandler.Register)
public.POST("/login", apiHandler.Login)
}
apiGroup := r.Group("/api", middleware.Auth)
// API routes (authenticated)
apiGroup := r.Group("/api", middleware.Auth(db))
{
apiGroup.GET("/profile", api.Profile)
apiGroup.POST("/profile/update", api.UpdateProfile)
apiGroup.POST("/profile/update/password", api.UpdatePassword)
// 绑定PassKey
apiGroup.GET("/profile/passkey", api.PasskeyCreateBegin)
apiGroup.POST("/profile/passkey", api.PasskeyCreateFinish)
apiGroup.GET("/profile/passkeys", api.ListPasskey)
apiGroup.DELETE("/profile/passkeys/:id", api.DeletePasskey)
// User profile
apiGroup.GET("/me", apiHandler.Me)
apiGroup.GET("/profile", apiHandler.Me)
userGroup := apiGroup.Group("/users")
{
userGroup.POST("", api.CreateUser)
userGroup.GET("", api.ListUser)
userGroup.GET("/:id", api.GetUser)
userGroup.PUT("/:id", api.EditUser)
userGroup.DELETE("/:id", api.DeleteUser)
userGroup.POST("/batch/:option", api.UserOption)
}
// User management (admin)
apiGroup.GET("/users", apiHandler.ListUsers)
apiGroup.POST("/users", apiHandler.CreateUser)
apiGroup.DELETE("/users/:id", apiHandler.DeleteUser)
tokenGroup := apiGroup.Group("/tokens")
tokenGroup.POST("", api.CreateToken)
tokenGroup.GET("", api.ListToken)
// tokenGroup.GET("/:id", api.GetToken)
tokenGroup.POST("/reset/:id", api.ResetToken)
tokenGroup.PUT("/:id", api.UpdateToken)
tokenGroup.DELETE("/:id", api.DeleteToken)
// tokenGroup.POST("/batch/:option", api.TokenOption)
// API Key management
apiGroup.GET("/keys", apiHandler.ListApiKeys)
apiGroup.POST("/keys", apiHandler.CreateApiKey)
apiGroup.DELETE("/keys/:id", apiHandler.DeleteApiKey)
apiGroup.POST("keys", api.CreateApiKey)
apiGroup.GET("keys", api.ListApiKey)
apiGroup.GET("keys/:id", api.GetApiKey)
apiGroup.PUT("keys/:id", api.UpdateApiKey)
apiGroup.DELETE("keys/:id", api.DeleteApiKey)
apiGroup.POST("keys/batch/:option", api.ApiKeyOption)
// Channel management
apiGroup.GET("/channels", apiHandler.ListChannels)
apiGroup.POST("/channels", apiHandler.CreateChannel)
apiGroup.PUT("/channels/:id", apiHandler.UpdateChannel)
apiGroup.DELETE("/channels/:id", apiHandler.DeleteChannel)
apiGroup.GET("/channels/:id/models", apiHandler.GetChannelModels)
apiGroup.POST("/channels/:id/models", apiHandler.BindChannelModels)
// Model management
apiGroup.GET("/models", apiHandler.ListModels)
apiGroup.POST("/models", apiHandler.CreateModel)
apiGroup.PUT("/models/:id", apiHandler.UpdateModel)
apiGroup.DELETE("/models/:id", apiHandler.DeleteModel)
}
// LLM proxy routes
v1 := r.Group("/v1")
v1.Use(middleware.AuthLLM(db))
{
// v1.POST("/v2/*proxypath", router.HandleProxy)
v1.POST("/*proxypath", proxy.HandleProxy)
v1.GET("/models", proxy.HandleModels)
v1.POST("/chat/completions", gateway.HandleChat)
v1.POST("/messages", gateway.HandleMessages)
v1.POST("/responses", gateway.HandleResponses)
v1.GET("/models", gateway.HandleModels)
}
// SPA fallback
idxFS, err := fs.Sub(web, "dist")
if err != nil {
panic(err)
@@ -151,17 +141,12 @@ func SetRouter(cfg *config.Config, db *gorm.DB, web *embed.FS) {
go func() {
fmt.Println("Starting server at port:", cfg.Port)
// 服务启动
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// 等待中断信号来优雅地关闭服务器
quit := make(chan os.Signal, 1)
// kill (no param) default send syscall.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall.SIGKILL but can't be catch
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
fmt.Println("\nShutdown Server ...")
@@ -173,7 +158,6 @@ func SetRouter(cfg *config.Config, db *gorm.DB, web *embed.FS) {
}
cancel()
sqlDB.Close()
waitChan := make(chan struct{})