2
0

option.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "one-api/common"
  7. "one-api/model"
  8. "one-api/setting"
  9. "one-api/setting/console_setting"
  10. "one-api/setting/ratio_setting"
  11. "one-api/setting/system_setting"
  12. "strings"
  13. "github.com/gin-gonic/gin"
  14. )
  15. func GetOptions(c *gin.Context) {
  16. var options []*model.Option
  17. common.OptionMapRWMutex.Lock()
  18. for k, v := range common.OptionMap {
  19. if strings.HasSuffix(k, "Token") || strings.HasSuffix(k, "Secret") || strings.HasSuffix(k, "Key") {
  20. continue
  21. }
  22. options = append(options, &model.Option{
  23. Key: k,
  24. Value: common.Interface2String(v),
  25. })
  26. }
  27. common.OptionMapRWMutex.Unlock()
  28. c.JSON(http.StatusOK, gin.H{
  29. "success": true,
  30. "message": "",
  31. "data": options,
  32. })
  33. return
  34. }
  35. type OptionUpdateRequest struct {
  36. Key string `json:"key"`
  37. Value any `json:"value"`
  38. }
  39. func UpdateOption(c *gin.Context) {
  40. var option OptionUpdateRequest
  41. err := json.NewDecoder(c.Request.Body).Decode(&option)
  42. if err != nil {
  43. c.JSON(http.StatusBadRequest, gin.H{
  44. "success": false,
  45. "message": "无效的参数",
  46. })
  47. return
  48. }
  49. switch option.Value.(type) {
  50. case bool:
  51. option.Value = common.Interface2String(option.Value.(bool))
  52. case float64:
  53. option.Value = common.Interface2String(option.Value.(float64))
  54. case int:
  55. option.Value = common.Interface2String(option.Value.(int))
  56. default:
  57. option.Value = fmt.Sprintf("%v", option.Value)
  58. }
  59. switch option.Key {
  60. case "GitHubOAuthEnabled":
  61. if option.Value == "true" && common.GitHubClientId == "" {
  62. c.JSON(http.StatusOK, gin.H{
  63. "success": false,
  64. "message": "无法启用 GitHub OAuth,请先填入 GitHub Client Id 以及 GitHub Client Secret!",
  65. })
  66. return
  67. }
  68. case "oidc.enabled":
  69. if option.Value == "true" && system_setting.GetOIDCSettings().ClientId == "" {
  70. c.JSON(http.StatusOK, gin.H{
  71. "success": false,
  72. "message": "无法启用 OIDC 登录,请先填入 OIDC Client Id 以及 OIDC Client Secret!",
  73. })
  74. return
  75. }
  76. case "LinuxDOOAuthEnabled":
  77. if option.Value == "true" && common.LinuxDOClientId == "" {
  78. c.JSON(http.StatusOK, gin.H{
  79. "success": false,
  80. "message": "无法启用 LinuxDO OAuth,请先填入 LinuxDO Client Id 以及 LinuxDO Client Secret!",
  81. })
  82. return
  83. }
  84. case "EmailDomainRestrictionEnabled":
  85. if option.Value == "true" && len(common.EmailDomainWhitelist) == 0 {
  86. c.JSON(http.StatusOK, gin.H{
  87. "success": false,
  88. "message": "无法启用邮箱域名限制,请先填入限制的邮箱域名!",
  89. })
  90. return
  91. }
  92. case "WeChatAuthEnabled":
  93. if option.Value == "true" && common.WeChatServerAddress == "" {
  94. c.JSON(http.StatusOK, gin.H{
  95. "success": false,
  96. "message": "无法启用微信登录,请先填入微信登录相关配置信息!",
  97. })
  98. return
  99. }
  100. case "TurnstileCheckEnabled":
  101. if option.Value == "true" && common.TurnstileSiteKey == "" {
  102. c.JSON(http.StatusOK, gin.H{
  103. "success": false,
  104. "message": "无法启用 Turnstile 校验,请先填入 Turnstile 校验相关配置信息!",
  105. })
  106. return
  107. }
  108. case "TelegramOAuthEnabled":
  109. if option.Value == "true" && common.TelegramBotToken == "" {
  110. c.JSON(http.StatusOK, gin.H{
  111. "success": false,
  112. "message": "无法启用 Telegram OAuth,请先填入 Telegram Bot Token!",
  113. })
  114. return
  115. }
  116. case "GroupRatio":
  117. err = ratio_setting.CheckGroupRatio(option.Value.(string))
  118. if err != nil {
  119. c.JSON(http.StatusOK, gin.H{
  120. "success": false,
  121. "message": err.Error(),
  122. })
  123. return
  124. }
  125. case "ImageRatio":
  126. err = ratio_setting.UpdateImageRatioByJSONString(option.Value.(string))
  127. if err != nil {
  128. c.JSON(http.StatusOK, gin.H{
  129. "success": false,
  130. "message": "图片倍率设置失败: " + err.Error(),
  131. })
  132. return
  133. }
  134. case "AudioRatio":
  135. err = ratio_setting.UpdateAudioRatioByJSONString(option.Value.(string))
  136. if err != nil {
  137. c.JSON(http.StatusOK, gin.H{
  138. "success": false,
  139. "message": "音频倍率设置失败: " + err.Error(),
  140. })
  141. return
  142. }
  143. case "AudioCompletionRatio":
  144. err = ratio_setting.UpdateAudioCompletionRatioByJSONString(option.Value.(string))
  145. if err != nil {
  146. c.JSON(http.StatusOK, gin.H{
  147. "success": false,
  148. "message": "音频补全倍率设置失败: " + err.Error(),
  149. })
  150. return
  151. }
  152. case "ModelRequestRateLimitGroup":
  153. err = setting.CheckModelRequestRateLimitGroup(option.Value.(string))
  154. if err != nil {
  155. c.JSON(http.StatusOK, gin.H{
  156. "success": false,
  157. "message": err.Error(),
  158. })
  159. return
  160. }
  161. case "console_setting.api_info":
  162. err = console_setting.ValidateConsoleSettings(option.Value.(string), "ApiInfo")
  163. if err != nil {
  164. c.JSON(http.StatusOK, gin.H{
  165. "success": false,
  166. "message": err.Error(),
  167. })
  168. return
  169. }
  170. case "console_setting.announcements":
  171. err = console_setting.ValidateConsoleSettings(option.Value.(string), "Announcements")
  172. if err != nil {
  173. c.JSON(http.StatusOK, gin.H{
  174. "success": false,
  175. "message": err.Error(),
  176. })
  177. return
  178. }
  179. case "console_setting.faq":
  180. err = console_setting.ValidateConsoleSettings(option.Value.(string), "FAQ")
  181. if err != nil {
  182. c.JSON(http.StatusOK, gin.H{
  183. "success": false,
  184. "message": err.Error(),
  185. })
  186. return
  187. }
  188. case "console_setting.uptime_kuma_groups":
  189. err = console_setting.ValidateConsoleSettings(option.Value.(string), "UptimeKumaGroups")
  190. if err != nil {
  191. c.JSON(http.StatusOK, gin.H{
  192. "success": false,
  193. "message": err.Error(),
  194. })
  195. return
  196. }
  197. }
  198. err = model.UpdateOption(option.Key, option.Value.(string))
  199. if err != nil {
  200. common.ApiError(c, err)
  201. return
  202. }
  203. c.JSON(http.StatusOK, gin.H{
  204. "success": true,
  205. "message": "",
  206. })
  207. return
  208. }