feat(option): enhance UpdateOption to handle various value types and improve validation

This commit is contained in:
CaIon
2025-09-03 14:30:25 +08:00
parent 56fc3441da
commit c6cf1b98f8
3 changed files with 33 additions and 11 deletions

View File

@@ -56,8 +56,6 @@
# SESSION_SECRET=random_string # SESSION_SECRET=random_string
# 其他配置 # 其他配置
# 渠道测试频率(单位:秒)
# CHANNEL_TEST_FREQUENCY=10
# 生成默认token # 生成默认token
# GENERATE_DEFAULT_TOKEN=false # GENERATE_DEFAULT_TOKEN=false
# Cohere 安全设置 # Cohere 安全设置

View File

@@ -123,8 +123,16 @@ func Interface2String(inter interface{}) string {
return fmt.Sprintf("%d", inter.(int)) return fmt.Sprintf("%d", inter.(int))
case float64: case float64:
return fmt.Sprintf("%f", inter.(float64)) return fmt.Sprintf("%f", inter.(float64))
case bool:
if inter.(bool) {
return "true"
} else {
return "false"
}
case nil:
return ""
} }
return "Not Implemented" return fmt.Sprintf("%v", inter)
} }
func UnescapeHTML(x string) interface{} { func UnescapeHTML(x string) interface{} {

View File

@@ -2,6 +2,7 @@ package controller
import ( import (
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"one-api/common" "one-api/common"
"one-api/model" "one-api/model"
@@ -35,8 +36,13 @@ func GetOptions(c *gin.Context) {
return return
} }
type OptionUpdateRequest struct {
Key string `json:"key"`
Value any `json:"value"`
}
func UpdateOption(c *gin.Context) { func UpdateOption(c *gin.Context) {
var option model.Option var option OptionUpdateRequest
err := json.NewDecoder(c.Request.Body).Decode(&option) err := json.NewDecoder(c.Request.Body).Decode(&option)
if err != nil { if err != nil {
c.JSON(http.StatusBadRequest, gin.H{ c.JSON(http.StatusBadRequest, gin.H{
@@ -45,6 +51,16 @@ func UpdateOption(c *gin.Context) {
}) })
return return
} }
switch option.Value.(type) {
case bool:
option.Value = common.Interface2String(option.Value.(bool))
case float64:
option.Value = common.Interface2String(option.Value.(float64))
case int:
option.Value = common.Interface2String(option.Value.(int))
default:
option.Value = fmt.Sprintf("%v", option.Value)
}
switch option.Key { switch option.Key {
case "GitHubOAuthEnabled": case "GitHubOAuthEnabled":
if option.Value == "true" && common.GitHubClientId == "" { if option.Value == "true" && common.GitHubClientId == "" {
@@ -104,7 +120,7 @@ func UpdateOption(c *gin.Context) {
return return
} }
case "GroupRatio": case "GroupRatio":
err = ratio_setting.CheckGroupRatio(option.Value) err = ratio_setting.CheckGroupRatio(option.Value.(string))
if err != nil { if err != nil {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"success": false, "success": false,
@@ -113,7 +129,7 @@ func UpdateOption(c *gin.Context) {
return return
} }
case "ModelRequestRateLimitGroup": case "ModelRequestRateLimitGroup":
err = setting.CheckModelRequestRateLimitGroup(option.Value) err = setting.CheckModelRequestRateLimitGroup(option.Value.(string))
if err != nil { if err != nil {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"success": false, "success": false,
@@ -122,7 +138,7 @@ func UpdateOption(c *gin.Context) {
return return
} }
case "console_setting.api_info": case "console_setting.api_info":
err = console_setting.ValidateConsoleSettings(option.Value, "ApiInfo") err = console_setting.ValidateConsoleSettings(option.Value.(string), "ApiInfo")
if err != nil { if err != nil {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"success": false, "success": false,
@@ -131,7 +147,7 @@ func UpdateOption(c *gin.Context) {
return return
} }
case "console_setting.announcements": case "console_setting.announcements":
err = console_setting.ValidateConsoleSettings(option.Value, "Announcements") err = console_setting.ValidateConsoleSettings(option.Value.(string), "Announcements")
if err != nil { if err != nil {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"success": false, "success": false,
@@ -140,7 +156,7 @@ func UpdateOption(c *gin.Context) {
return return
} }
case "console_setting.faq": case "console_setting.faq":
err = console_setting.ValidateConsoleSettings(option.Value, "FAQ") err = console_setting.ValidateConsoleSettings(option.Value.(string), "FAQ")
if err != nil { if err != nil {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"success": false, "success": false,
@@ -149,7 +165,7 @@ func UpdateOption(c *gin.Context) {
return return
} }
case "console_setting.uptime_kuma_groups": case "console_setting.uptime_kuma_groups":
err = console_setting.ValidateConsoleSettings(option.Value, "UptimeKumaGroups") err = console_setting.ValidateConsoleSettings(option.Value.(string), "UptimeKumaGroups")
if err != nil { if err != nil {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"success": false, "success": false,
@@ -158,7 +174,7 @@ func UpdateOption(c *gin.Context) {
return return
} }
} }
err = model.UpdateOption(option.Key, option.Value) err = model.UpdateOption(option.Key, option.Value.(string))
if err != nil { if err != nil {
common.ApiError(c, err) common.ApiError(c, err)
return return