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)
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"opencatd-open/internal/auth"
|
|
"opencatd-open/internal/store"
|
|
"opencatd-open/internal/pkg/jwt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func Auth(db *gorm.DB) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
authToken := c.GetHeader("Authorization")
|
|
if authToken == "" {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
|
"code": http.StatusUnauthorized,
|
|
"error": "未提供认证信息",
|
|
})
|
|
return
|
|
}
|
|
if len(authToken) > 7 {
|
|
authToken = authToken[7:]
|
|
}
|
|
claim, err := jwt.ValidateToken(authToken, auth.GetSecretKey())
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
|
"code": http.StatusUnauthorized,
|
|
"error": "无效的认证信息",
|
|
})
|
|
return
|
|
}
|
|
var user store.User
|
|
if err := db.First(&user, claim.UserID).Error; err != nil {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
|
"code": http.StatusUnauthorized,
|
|
"error": "无效的认证信息",
|
|
})
|
|
return
|
|
}
|
|
c.Set("user", &user)
|
|
c.Set("user_id", claim.UserID)
|
|
c.Set("user_role", user.Role)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func CheckRole(role string) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Next()
|
|
}
|
|
}
|