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:
@@ -1,35 +0,0 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"opencatd-open/llm/claude"
|
||||
"opencatd-open/llm/google"
|
||||
"opencatd-open/llm/openai"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ChatHandler(c *gin.Context) {
|
||||
var chatreq openai.ChatCompletionRequest
|
||||
if err := c.ShouldBindJSON(&chatreq); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if strings.Contains(chatreq.Model, "gpt") || strings.HasPrefix(chatreq.Model, "o1") || strings.HasPrefix(chatreq.Model, "o3") {
|
||||
openai.ChatProxy(c, &chatreq)
|
||||
return
|
||||
}
|
||||
|
||||
if strings.HasPrefix(chatreq.Model, "claude") {
|
||||
claude.ChatProxy(c, &chatreq)
|
||||
return
|
||||
}
|
||||
|
||||
if strings.HasPrefix(chatreq.Model, "gemini") || strings.HasPrefix(chatreq.Model, "learnlm") {
|
||||
google.ChatProxy(c, &chatreq)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"opencatd-open/llm/claude"
|
||||
oai "opencatd-open/llm/openai"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func HandleProxy(c *gin.Context) {
|
||||
if c.Request.URL.Path == "/v1/complete" {
|
||||
claude.ClaudeProxy(c)
|
||||
return
|
||||
}
|
||||
if c.Request.URL.Path == "/v1/audio/transcriptions" {
|
||||
oai.WhisperProxy(c)
|
||||
return
|
||||
}
|
||||
if c.Request.URL.Path == "/v1/audio/speech" {
|
||||
oai.SpeechProxy(c)
|
||||
return
|
||||
}
|
||||
|
||||
if c.Request.URL.Path == "/v1/images/generations" {
|
||||
oai.DallEProxy(c)
|
||||
return
|
||||
}
|
||||
|
||||
if c.Request.URL.Path == "/v1/realtime" {
|
||||
oai.RealTimeProxy(c)
|
||||
return
|
||||
}
|
||||
|
||||
if c.Request.URL.Path == "/v1/chat/completions" {
|
||||
ChatHandler(c)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func HandleReverseProxy(c *gin.Context, targetHost string) {
|
||||
proxy := &httputil.ReverseProxy{
|
||||
Director: func(req *http.Request) {
|
||||
req.URL.Scheme = "https"
|
||||
req.URL.Host = targetHost
|
||||
},
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).DialContext,
|
||||
ForceAttemptHTTP2: true,
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
},
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(c.Request.Method, c.Request.URL.Path, c.Request.Body)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
req.Header = c.Request.Header
|
||||
|
||||
proxy.ServeHTTP(c.Writer, req)
|
||||
return
|
||||
}
|
||||
+64
-80
@@ -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{})
|
||||
|
||||
Reference in New Issue
Block a user