2
0

option.go 3.4 KB

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