ai-gateway/controller/option.go

101 lines
2.4 KiB
Go
Raw Normal View History

2023-04-22 12:39:27 +00:00
package controller
import (
"encoding/json"
2024-01-28 11:38:58 +00:00
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/helper"
"github.com/songquanpeng/one-api/model"
2023-04-22 12:39:27 +00:00
"net/http"
"strings"
"github.com/gin-gonic/gin"
2023-04-22 12:39:27 +00:00
)
func GetOptions(c *gin.Context) {
var options []*model.Option
config.OptionMapRWMutex.Lock()
for k, v := range config.OptionMap {
if strings.HasSuffix(k, "Token") || strings.HasSuffix(k, "Secret") {
2023-04-22 12:39:27 +00:00
continue
}
options = append(options, &model.Option{
Key: k,
Value: helper.Interface2String(v),
2023-04-22 12:39:27 +00:00
})
}
config.OptionMapRWMutex.Unlock()
2023-04-22 12:39:27 +00:00
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": options,
})
return
}
func UpdateOption(c *gin.Context) {
var option model.Option
err := json.NewDecoder(c.Request.Body).Decode(&option)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": "无效的参数",
})
return
}
switch option.Key {
2024-01-07 10:44:26 +00:00
case "Theme":
if !config.ValidThemes[option.Value] {
2024-01-07 10:44:26 +00:00
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无效的主题",
})
return
}
2023-04-22 12:39:27 +00:00
case "GitHubOAuthEnabled":
if option.Value == "true" && config.GitHubClientId == "" {
2023-04-22 12:39:27 +00:00
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无法启用 GitHub OAuth请先填入 GitHub Client Id 以及 GitHub Client Secret",
2023-04-22 12:39:27 +00:00
})
return
}
case "EmailDomainRestrictionEnabled":
if option.Value == "true" && len(config.EmailDomainWhitelist) == 0 {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无法启用邮箱域名限制,请先填入限制的邮箱域名!",
})
return
}
2023-04-22 12:39:27 +00:00
case "WeChatAuthEnabled":
if option.Value == "true" && config.WeChatServerAddress == "" {
2023-04-22 12:39:27 +00:00
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无法启用微信登录,请先填入微信登录相关配置信息!",
})
return
}
case "TurnstileCheckEnabled":
if option.Value == "true" && config.TurnstileSiteKey == "" {
2023-04-22 12:39:27 +00:00
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无法启用 Turnstile 校验,请先填入 Turnstile 校验相关配置信息!",
})
return
}
}
err = model.UpdateOption(option.Key, option.Value)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
})
return
}