mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-09-17 16:56:38 +08:00
68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
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})
|
|
}
|