mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-11-09 18:53:43 +08:00
重新整理后台管理 API,新增系统管理页面
This commit is contained in:
67
server/handler_admin_apikey.go
Normal file
67
server/handler_admin_apikey.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"openai/types"
|
||||
"openai/utils"
|
||||
)
|
||||
|
||||
// AddApiKeyHandle 添加一个 API key
|
||||
func (s *Server) AddApiKeyHandle(c *gin.Context) {
|
||||
var data struct {
|
||||
ApiKey string `json:"api_key"`
|
||||
}
|
||||
err := json.NewDecoder(c.Request.Body).Decode(&data)
|
||||
if err != nil {
|
||||
logger.Errorf("Error decode json data: %s", err.Error())
|
||||
c.JSON(http.StatusBadRequest, nil)
|
||||
return
|
||||
}
|
||||
if len(data.ApiKey) > 20 {
|
||||
s.Config.Chat.ApiKeys = append(s.Config.Chat.ApiKeys, data.ApiKey)
|
||||
}
|
||||
|
||||
// 保存配置文件
|
||||
err = utils.SaveConfig(s.Config, s.ConfigPath)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Failed to save config file"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: s.Config.Chat.ApiKeys})
|
||||
}
|
||||
|
||||
// RemoveApiKeyHandle 移除一个 API key
|
||||
func (s *Server) RemoveApiKeyHandle(c *gin.Context) {
|
||||
var data struct {
|
||||
ApiKey string `json:"api_key"`
|
||||
}
|
||||
err := json.NewDecoder(c.Request.Body).Decode(&data)
|
||||
if err != nil {
|
||||
logger.Errorf("Error decode json data: %s", err.Error())
|
||||
c.JSON(http.StatusBadRequest, nil)
|
||||
return
|
||||
}
|
||||
|
||||
for i, v := range s.Config.Chat.ApiKeys {
|
||||
if v == data.ApiKey {
|
||||
s.Config.Chat.ApiKeys = append(s.Config.Chat.ApiKeys[:i], s.Config.Chat.ApiKeys[i+1:]...)
|
||||
}
|
||||
}
|
||||
|
||||
// 保存配置文件
|
||||
err = utils.SaveConfig(s.Config, s.ConfigPath)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Failed to save config file"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: s.Config.Chat.ApiKeys})
|
||||
}
|
||||
|
||||
// ListApiKeysHandle 获取 API key 列表
|
||||
func (s *Server) ListApiKeysHandle(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Success, Message: types.OkMsg, Data: s.Config.Chat.ApiKeys})
|
||||
}
|
||||
Reference in New Issue
Block a user