feat: 三协议互转网关 + 鉴权修复 + 管理端增强
后端 - 新增 proxy/convert 三协议(chat/messages/responses)请求、响应与 SSE 流式互转, 以 Chat 为中间模型;usage.go 统一提取三协议 token 用量(含单测) - gateway: 跨协议调度(渠道未声明客户端协议时转为渠道首选格式), streamResponse 按 \n\n 分块逐行转换直通,bufferResponse 转换失败时剥非 JSON 前缀 - gateway: 新增 SetUsageRecorder 注入异步用量记录器 - auth_llm: 修复 key_prefix 查询长度错配([:8] vs 存储的 [:12])导致全部 401; 修复长度 8-11 的 key 切片越界 panic;统一 unauthorized 响应 - usage: 日报表改为增量累加 upsert,避免多次 flush 互相清零;记录协议/错误码/时延等字段 - channel: 新增渠道并发槽 TryAcquire;健康检查支持可配置参数 - api: 新增 admin 渠道/模型/系统配置管理端点(旧端点保留兼容) 前端 - 新增渠道管理、模型管理、系统配置视图与 ChannelModelsDrawer - 新增 ui 基础组件(Button/Badge/Input/Modal)与 protocol.ts - 调整 Toast 样式、密钥页、路由菜单;dev 代理默认指向 3000 端口
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"opencatd-open/internal/store"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// AdminGetConfig GET /api/admin/config — 获取系统配置。
|
||||
func (h *Handler) AdminGetConfig(c *gin.Context) {
|
||||
configs := map[string]string{}
|
||||
var rows []store.SystemConfig
|
||||
h.db.Find(&rows)
|
||||
for _, r := range rows {
|
||||
configs[r.Key] = r.Value
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": configs})
|
||||
}
|
||||
|
||||
// AdminUpdateConfig PUT /api/admin/config — 更新系统配置。
|
||||
func (h *Handler) AdminUpdateConfig(c *gin.Context) {
|
||||
var req map[string]string
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid input"})
|
||||
return
|
||||
}
|
||||
for key, value := range req {
|
||||
var sc store.SystemConfig
|
||||
result := h.db.Where("key = ?", key).First(&sc)
|
||||
if result.Error == nil {
|
||||
sc.Value = value
|
||||
h.db.Save(&sc)
|
||||
} else {
|
||||
sc = store.SystemConfig{Key: key, Value: value}
|
||||
h.db.Create(&sc)
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true})
|
||||
}
|
||||
|
||||
// AdminGetRegistration GET /api/admin/config/registration — 获取注册配置。
|
||||
func (h *Handler) AdminGetRegistration(c *gin.Context) {
|
||||
var sc store.SystemConfig
|
||||
enabled := "true"
|
||||
if err := h.db.Where("key = ?", "registration_enabled").First(&sc).Error; err == nil {
|
||||
enabled = sc.Value
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": gin.H{"enabled": enabled == "true"}})
|
||||
}
|
||||
|
||||
// AdminUpdateRegistration PUT /api/admin/config/registration — 更新注册配置。
|
||||
func (h *Handler) AdminUpdateRegistration(c *gin.Context) {
|
||||
var req struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid input"})
|
||||
return
|
||||
}
|
||||
value := "false"
|
||||
if req.Enabled {
|
||||
value = "true"
|
||||
}
|
||||
var sc store.SystemConfig
|
||||
if err := h.db.Where("key = ?", "registration_enabled").First(&sc).Error; err == nil {
|
||||
sc.Value = value
|
||||
h.db.Save(&sc)
|
||||
} else {
|
||||
sc = store.SystemConfig{Key: "registration_enabled", Value: value}
|
||||
h.db.Create(&sc)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true})
|
||||
}
|
||||
|
||||
// AdminGetPasswordLogin GET /api/admin/config/password-login — 获取密码登录配置。
|
||||
func (h *Handler) AdminGetPasswordLogin(c *gin.Context) {
|
||||
var sc store.SystemConfig
|
||||
enabled := "true"
|
||||
if err := h.db.Where("key = ?", "password_login_enabled").First(&sc).Error; err == nil {
|
||||
enabled = sc.Value
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"data": gin.H{"enabled": enabled == "true"}})
|
||||
}
|
||||
|
||||
// AdminUpdatePasswordLogin PUT /api/admin/config/password-login — 更新密码登录配置。
|
||||
func (h *Handler) AdminUpdatePasswordLogin(c *gin.Context) {
|
||||
var req struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid input"})
|
||||
return
|
||||
}
|
||||
value := "false"
|
||||
if req.Enabled {
|
||||
value = "true"
|
||||
}
|
||||
var sc store.SystemConfig
|
||||
if err := h.db.Where("key = ?", "password_login_enabled").First(&sc).Error; err == nil {
|
||||
sc.Value = value
|
||||
h.db.Save(&sc)
|
||||
} else {
|
||||
sc = store.SystemConfig{Key: "password_login_enabled", Value: value}
|
||||
h.db.Create(&sc)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true})
|
||||
}
|
||||
Reference in New Issue
Block a user