option.go 2.6 KB

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