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
+70
View File
@@ -0,0 +1,70 @@
package store
import (
"fmt"
"log"
"opencatd-open/pkg/config"
_ "github.com/lib/pq"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
var DB *gorm.DB
func InitDB(cfg *config.Config) (*gorm.DB, error) {
var dialector gorm.Dialector
switch cfg.DB_Type {
case "sqlite":
dialector = sqliteDialector(cfg.DSN)
case "postgres":
dialector = postgresDialector(cfg.DSN)
case "mysql":
dialector = mysqlDialector(cfg.DSN)
default:
return nil, fmt.Errorf("unsupported database type: %s", cfg.DB_Type)
}
db, err := gorm.Open(dialector, &gorm.Config{})
if err != nil {
return nil, fmt.Errorf("failed to connect database: %w", err)
}
sqlDB, err := db.DB()
if err != nil {
return nil, fmt.Errorf("failed to get underlying *sql.DB: %w", err)
}
sqlDB.SetMaxOpenConns(cfg.DBMaxOpenConns)
sqlDB.SetMaxIdleConns(cfg.DBMaxIdleConns)
if err := db.AutoMigrate(AllModels()...); err != nil {
log.Printf("AutoMigrate warning: %v", err)
}
DB = db
return db, nil
}
func sqliteDialector(dsn string) gorm.Dialector {
if dsn == "" {
dsn = "opencatd.db"
}
return sqlite.Open(dsn)
}
func postgresDialector(dsn string) gorm.Dialector {
if dsn == "" {
dsn = "host=localhost user=postgres password=postgres dbname=opencatd port=5432 sslmode=disable"
}
return postgres.Open(dsn)
}
func mysqlDialector(dsn string) gorm.Dialector {
if dsn == "" {
dsn = "root:password@tcp(127.0.0.1:3306)/opencatd?charset=utf8mb4&parseTime=True&loc=Local"
}
return mysql.Open(dsn)
}