option.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package controller
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "one-api/common"
  6. "one-api/model"
  7. "one-api/setting"
  8. "strings"
  9. "github.com/gin-gonic/gin"
  10. )
  11. func GetOptions(c *gin.Context) {
  12. var options []*model.Option
  13. common.OptionMapRWMutex.Lock()
  14. for k, v := range common.OptionMap {
  15. if strings.HasSuffix(k, "Token") || strings.HasSuffix(k, "Secret") || strings.HasSuffix(k, "Key") {
  16. continue
  17. }
  18. options = append(options, &model.Option{
  19. Key: k,
  20. Value: common.Interface2String(v),
  21. })
  22. }
  23. common.OptionMapRWMutex.Unlock()
  24. c.JSON(http.StatusOK, gin.H{
  25. "success": true,
  26. "message": "",
  27. "data": options,
  28. })
  29. return
  30. }
  31. func UpdateOption(c *gin.Context) {
  32. var option model.Option
  33. err := json.NewDecoder(c.Request.Body).Decode(&option)
  34. if err != nil {
  35. c.JSON(http.StatusBadRequest, gin.H{
  36. "success": false,
  37. "message": "无效的参数",
  38. })
  39. return
  40. }
  41. switch option.Key {
  42. case "GitHubOAuthEnabled":
  43. if option.Value == "true" && common.GitHubClientId == "" {
  44. c.JSON(http.StatusOK, gin.H{
  45. "success": false,
  46. "message": "无法启用 GitHub OAuth,请先填入 GitHub Client Id 以及 GitHub Client Secret!",
  47. })
  48. return
  49. }
  50. case "LinuxDOOAuthEnabled":
  51. if option.Value == "true" && common.LinuxDOClientId == "" {
  52. c.JSON(http.StatusOK, gin.H{
  53. "success": false,
  54. "message": "无法启用 LinuxDO OAuth,请先填入 LinuxDO Client Id 以及 LinuxDO Client Secret!",
  55. })
  56. return
  57. }
  58. case "EmailDomainRestrictionEnabled":
  59. if option.Value == "true" && len(common.EmailDomainWhitelist) == 0 {
  60. c.JSON(http.StatusOK, gin.H{
  61. "success": false,
  62. "message": "无法启用邮箱域名限制,请先填入限制的邮箱域名!",
  63. })
  64. return
  65. }
  66. case "WeChatAuthEnabled":
  67. if option.Value == "true" && common.WeChatServerAddress == "" {
  68. c.JSON(http.StatusOK, gin.H{
  69. "success": false,
  70. "message": "无法启用微信登录,请先填入微信登录相关配置信息!",
  71. })
  72. return
  73. }
  74. case "TurnstileCheckEnabled":
  75. if option.Value == "true" && common.TurnstileSiteKey == "" {
  76. c.JSON(http.StatusOK, gin.H{
  77. "success": false,
  78. "message": "无法启用 Turnstile 校验,请先填入 Turnstile 校验相关配置信息!",
  79. })
  80. return
  81. }
  82. case "GroupRatio":
  83. err = setting.CheckGroupRatio(option.Value)
  84. if err != nil {
  85. c.JSON(http.StatusOK, gin.H{
  86. "success": false,
  87. "message": err.Error(),
  88. })
  89. return
  90. }
  91. }
  92. err = model.UpdateOption(option.Key, option.Value)
  93. if err != nil {
  94. c.JSON(http.StatusOK, gin.H{
  95. "success": false,
  96. "message": err.Error(),
  97. })
  98. return
  99. }
  100. c.JSON(http.StatusOK, gin.H{
  101. "success": true,
  102. "message": "",
  103. })
  104. return
  105. }