option.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package controller
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "one-api/common"
  6. "one-api/model"
  7. "one-api/setting"
  8. "one-api/setting/console_setting"
  9. "one-api/setting/system_setting"
  10. "strings"
  11. "github.com/gin-gonic/gin"
  12. )
  13. func GetOptions(c *gin.Context) {
  14. var options []*model.Option
  15. common.OptionMapRWMutex.Lock()
  16. for k, v := range common.OptionMap {
  17. if strings.HasSuffix(k, "Token") || strings.HasSuffix(k, "Secret") || strings.HasSuffix(k, "Key") {
  18. continue
  19. }
  20. options = append(options, &model.Option{
  21. Key: k,
  22. Value: common.Interface2String(v),
  23. })
  24. }
  25. common.OptionMapRWMutex.Unlock()
  26. c.JSON(http.StatusOK, gin.H{
  27. "success": true,
  28. "message": "",
  29. "data": options,
  30. })
  31. return
  32. }
  33. func UpdateOption(c *gin.Context) {
  34. var option model.Option
  35. err := json.NewDecoder(c.Request.Body).Decode(&option)
  36. if err != nil {
  37. c.JSON(http.StatusBadRequest, gin.H{
  38. "success": false,
  39. "message": "无效的参数",
  40. })
  41. return
  42. }
  43. switch option.Key {
  44. case "GitHubOAuthEnabled":
  45. if option.Value == "true" && common.GitHubClientId == "" {
  46. c.JSON(http.StatusOK, gin.H{
  47. "success": false,
  48. "message": "无法启用 GitHub OAuth,请先填入 GitHub Client Id 以及 GitHub Client Secret!",
  49. })
  50. return
  51. }
  52. case "oidc.enabled":
  53. if option.Value == "true" && system_setting.GetOIDCSettings().ClientId == "" {
  54. c.JSON(http.StatusOK, gin.H{
  55. "success": false,
  56. "message": "无法启用 OIDC 登录,请先填入 OIDC Client Id 以及 OIDC Client Secret!",
  57. })
  58. return
  59. }
  60. case "LinuxDOOAuthEnabled":
  61. if option.Value == "true" && common.LinuxDOClientId == "" {
  62. c.JSON(http.StatusOK, gin.H{
  63. "success": false,
  64. "message": "无法启用 LinuxDO OAuth,请先填入 LinuxDO Client Id 以及 LinuxDO Client Secret!",
  65. })
  66. return
  67. }
  68. case "EmailDomainRestrictionEnabled":
  69. if option.Value == "true" && len(common.EmailDomainWhitelist) == 0 {
  70. c.JSON(http.StatusOK, gin.H{
  71. "success": false,
  72. "message": "无法启用邮箱域名限制,请先填入限制的邮箱域名!",
  73. })
  74. return
  75. }
  76. case "WeChatAuthEnabled":
  77. if option.Value == "true" && common.WeChatServerAddress == "" {
  78. c.JSON(http.StatusOK, gin.H{
  79. "success": false,
  80. "message": "无法启用微信登录,请先填入微信登录相关配置信息!",
  81. })
  82. return
  83. }
  84. case "TurnstileCheckEnabled":
  85. if option.Value == "true" && common.TurnstileSiteKey == "" {
  86. c.JSON(http.StatusOK, gin.H{
  87. "success": false,
  88. "message": "无法启用 Turnstile 校验,请先填入 Turnstile 校验相关配置信息!",
  89. })
  90. return
  91. }
  92. case "TelegramOAuthEnabled":
  93. if option.Value == "true" && common.TelegramBotToken == "" {
  94. c.JSON(http.StatusOK, gin.H{
  95. "success": false,
  96. "message": "无法启用 Telegram OAuth,请先填入 Telegram Bot Token!",
  97. })
  98. return
  99. }
  100. case "GroupRatio":
  101. err = setting.CheckGroupRatio(option.Value)
  102. if err != nil {
  103. c.JSON(http.StatusOK, gin.H{
  104. "success": false,
  105. "message": err.Error(),
  106. })
  107. return
  108. }
  109. case "ModelRequestRateLimitGroup":
  110. err = setting.CheckModelRequestRateLimitGroup(option.Value)
  111. if err != nil {
  112. c.JSON(http.StatusOK, gin.H{
  113. "success": false,
  114. "message": err.Error(),
  115. })
  116. return
  117. }
  118. case "console_setting.api_info":
  119. err = console_setting.ValidateApiInfo(option.Value)
  120. if err != nil {
  121. c.JSON(http.StatusOK, gin.H{
  122. "success": false,
  123. "message": err.Error(),
  124. })
  125. return
  126. }
  127. case "console_setting.announcements":
  128. err = console_setting.ValidateConsoleSettings(option.Value, "Announcements")
  129. if err != nil {
  130. c.JSON(http.StatusOK, gin.H{
  131. "success": false,
  132. "message": err.Error(),
  133. })
  134. return
  135. }
  136. case "console_setting.faq":
  137. err = console_setting.ValidateConsoleSettings(option.Value, "FAQ")
  138. if err != nil {
  139. c.JSON(http.StatusOK, gin.H{
  140. "success": false,
  141. "message": err.Error(),
  142. })
  143. return
  144. }
  145. }
  146. err = model.UpdateOption(option.Key, option.Value)
  147. if err != nil {
  148. c.JSON(http.StatusOK, gin.H{
  149. "success": false,
  150. "message": err.Error(),
  151. })
  152. return
  153. }
  154. c.JSON(http.StatusOK, gin.H{
  155. "success": true,
  156. "message": "",
  157. })
  158. return
  159. }