option.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "EmailDomainRestrictionEnabled":
  50. if option.Value == "true" && len(common.EmailDomainWhitelist) == 0 {
  51. c.JSON(http.StatusOK, gin.H{
  52. "success": false,
  53. "message": "无法启用邮箱域名限制,请先填入限制的邮箱域名!",
  54. })
  55. return
  56. }
  57. case "WeChatAuthEnabled":
  58. if option.Value == "true" && common.WeChatServerAddress == "" {
  59. c.JSON(http.StatusOK, gin.H{
  60. "success": false,
  61. "message": "无法启用微信登录,请先填入微信登录相关配置信息!",
  62. })
  63. return
  64. }
  65. case "TurnstileCheckEnabled":
  66. if option.Value == "true" && common.TurnstileSiteKey == "" {
  67. c.JSON(http.StatusOK, gin.H{
  68. "success": false,
  69. "message": "无法启用 Turnstile 校验,请先填入 Turnstile 校验相关配置信息!",
  70. })
  71. return
  72. }
  73. }
  74. err = model.UpdateOption(option.Key, option.Value)
  75. if err != nil {
  76. c.JSON(http.StatusOK, gin.H{
  77. "success": false,
  78. "message": err.Error(),
  79. })
  80. return
  81. }
  82. c.JSON(http.StatusOK, gin.H{
  83. "success": true,
  84. "message": "",
  85. })
  86. return
  87. }