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:
+11
-36
@@ -4,13 +4,10 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"opencatd-open/internal/model"
|
||||
"opencatd-open/pkg/store"
|
||||
"opencatd-open/internal/store"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/duke-git/lancet/v2/fileutil"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@@ -20,16 +17,17 @@ var LoadCmd = &cobra.Command{
|
||||
Short: "import user.json -> db",
|
||||
Long: "\nimport user.json -> db",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
db := store.GetDB()
|
||||
db := store.DB
|
||||
var cont int64
|
||||
if err := db.Model(model.User{}).Count(&cont).Error; err != nil {
|
||||
if err := db.Model(&store.User{}).Count(&cont).Error; err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
if cont == 0 {
|
||||
fmt.Println("创建管理员之后再操作")
|
||||
return
|
||||
}
|
||||
if !fileutil.IsExist("./db/user.json") {
|
||||
if _, err := os.Stat("./db/user.json"); os.IsNotExist(err) {
|
||||
log.Fatalln("404! user.json is not found.")
|
||||
return
|
||||
}
|
||||
@@ -41,40 +39,22 @@ var LoadCmd = &cobra.Command{
|
||||
defer file.Close()
|
||||
|
||||
var usermap []map[string]string
|
||||
|
||||
if err := json.NewDecoder(file).Decode(&usermap); err != nil {
|
||||
fmt.Println("解析文件失败:", err)
|
||||
return
|
||||
}
|
||||
for _, um := range usermap {
|
||||
var name string
|
||||
if um["username"] != "" {
|
||||
name := um["username"]
|
||||
if name == "" {
|
||||
name = um["name"]
|
||||
} else if um["name"] == "" {
|
||||
name = um["username"]
|
||||
} else {
|
||||
}
|
||||
if name == "" {
|
||||
fmt.Println("获取不到数据")
|
||||
continue
|
||||
}
|
||||
var user = model.User{
|
||||
Username: name,
|
||||
Name: name,
|
||||
Tokens: []model.Token{
|
||||
{
|
||||
Name: "default",
|
||||
Key: "sk-team-" + strings.ReplaceAll(uuid.New().String(), "-", ""),
|
||||
},
|
||||
{
|
||||
Name: name,
|
||||
Key: um["token"],
|
||||
},
|
||||
},
|
||||
}
|
||||
if err := db.Create(&user).Error; err != nil {
|
||||
fmt.Printf("\nCreate User %s Error:%s", user.Username, err)
|
||||
}
|
||||
_ = "sk-ot-" + strings.ReplaceAll(uuid.New().String(), "-", "")
|
||||
fmt.Printf("Import user: %s\n", name)
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
@@ -82,10 +62,5 @@ var SaveCmd = &cobra.Command{
|
||||
Use: "save",
|
||||
Short: "backup user info -> user.json",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
// SaveCmd.Flags().StringP("user", "u", "", "Save User")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user