Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
f0183785
authored
Sep 03, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(option): enhance UpdateOption to handle various value types and improve validation
parent
1bbabda0
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
11 deletions
+33
-11
.env.example
+0
-2
common/utils.go
+9
-1
controller/option.go
+24
-8
No files found.
.env.example
View file @
f0183785
...
...
@@ -56,8 +56,6 @@
# SESSION_SECRET=random_string
# 其他配置
# 渠道测试频率(单位:秒)
# CHANNEL_TEST_FREQUENCY=10
# 生成默认token
# GENERATE_DEFAULT_TOKEN=false
# Cohere 安全设置
...
...
common/utils.go
View file @
f0183785
...
...
@@ -123,8 +123,16 @@ func Interface2String(inter interface{}) string {
return
fmt
.
Sprintf
(
"%d"
,
inter
.
(
int
))
case
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
{}
{
...
...
controller/option.go
View file @
f0183785
...
...
@@ -2,6 +2,7 @@ package controller
import
(
"encoding/json"
"fmt"
"net/http"
"one-api/common"
"one-api/model"
...
...
@@ -35,8 +36,13 @@ func GetOptions(c *gin.Context) {
return
}
type
OptionUpdateRequest
struct
{
Key
string
`json:"key"`
Value
any
`json:"value"`
}
func
UpdateOption
(
c
*
gin
.
Context
)
{
var
option
model
.
Option
var
option
OptionUpdateRequest
err
:=
json
.
NewDecoder
(
c
.
Request
.
Body
)
.
Decode
(
&
option
)
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusBadRequest
,
gin
.
H
{
...
...
@@ -45,6 +51,16 @@ func UpdateOption(c *gin.Context) {
})
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
{
case
"GitHubOAuthEnabled"
:
if
option
.
Value
==
"true"
&&
common
.
GitHubClientId
==
""
{
...
...
@@ -104,7 +120,7 @@ func UpdateOption(c *gin.Context) {
return
}
case
"GroupRatio"
:
err
=
ratio_setting
.
CheckGroupRatio
(
option
.
Value
)
err
=
ratio_setting
.
CheckGroupRatio
(
option
.
Value
.
(
string
)
)
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
...
...
@@ -113,7 +129,7 @@ func UpdateOption(c *gin.Context) {
return
}
case
"ModelRequestRateLimitGroup"
:
err
=
setting
.
CheckModelRequestRateLimitGroup
(
option
.
Value
)
err
=
setting
.
CheckModelRequestRateLimitGroup
(
option
.
Value
.
(
string
)
)
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
...
...
@@ -122,7 +138,7 @@ func UpdateOption(c *gin.Context) {
return
}
case
"console_setting.api_info"
:
err
=
console_setting
.
ValidateConsoleSettings
(
option
.
Value
,
"ApiInfo"
)
err
=
console_setting
.
ValidateConsoleSettings
(
option
.
Value
.
(
string
)
,
"ApiInfo"
)
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
...
...
@@ -131,7 +147,7 @@ func UpdateOption(c *gin.Context) {
return
}
case
"console_setting.announcements"
:
err
=
console_setting
.
ValidateConsoleSettings
(
option
.
Value
,
"Announcements"
)
err
=
console_setting
.
ValidateConsoleSettings
(
option
.
Value
.
(
string
)
,
"Announcements"
)
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
...
...
@@ -140,7 +156,7 @@ func UpdateOption(c *gin.Context) {
return
}
case
"console_setting.faq"
:
err
=
console_setting
.
ValidateConsoleSettings
(
option
.
Value
,
"FAQ"
)
err
=
console_setting
.
ValidateConsoleSettings
(
option
.
Value
.
(
string
)
,
"FAQ"
)
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
...
...
@@ -149,7 +165,7 @@ func UpdateOption(c *gin.Context) {
return
}
case
"console_setting.uptime_kuma_groups"
:
err
=
console_setting
.
ValidateConsoleSettings
(
option
.
Value
,
"UptimeKumaGroups"
)
err
=
console_setting
.
ValidateConsoleSettings
(
option
.
Value
.
(
string
)
,
"UptimeKumaGroups"
)
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
...
...
@@ -158,7 +174,7 @@ func UpdateOption(c *gin.Context) {
return
}
}
err
=
model
.
UpdateOption
(
option
.
Key
,
option
.
Value
)
err
=
model
.
UpdateOption
(
option
.
Key
,
option
.
Value
.
(
string
)
)
if
err
!=
nil
{
common
.
ApiError
(
c
,
err
)
return
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment