option.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package controller
  2. import (
  3. "encoding/json"
  4. "github.com/gin-gonic/gin"
  5. "message-pusher/common"
  6. "message-pusher/model"
  7. "net/http"
  8. "strings"
  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.Contains(k, "Token") || strings.Contains(k, "Secret") {
  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. if option.Key == "GitHubOAuthEnabled" && option.Value == "true" && common.GitHubClientId == "" {
  41. c.JSON(http.StatusOK, gin.H{
  42. "success": false,
  43. "message": "无法启用 GitHub OAuth,请先填入 GitHub Client ID 以及 GitHub Client Secret!",
  44. })
  45. return
  46. } else if option.Key == "WeChatAuthEnabled" && option.Value == "true" && common.WeChatServerAddress == "" {
  47. c.JSON(http.StatusOK, gin.H{
  48. "success": false,
  49. "message": "无法启用微信登录,请先填入微信登录相关配置信息!",
  50. })
  51. return
  52. } else if option.Key == "TurnstileCheckEnabled" && option.Value == "true" && common.TurnstileSiteKey == "" {
  53. c.JSON(http.StatusOK, gin.H{
  54. "success": false,
  55. "message": "无法启用 Turnstile 校验,请先填入 Turnstile 校验相关配置信息!",
  56. })
  57. return
  58. }
  59. err = model.UpdateOption(option.Key, option.Value)
  60. if err != nil {
  61. c.JSON(http.StatusOK, gin.H{
  62. "success": false,
  63. "message": err.Error(),
  64. })
  65. return
  66. }
  67. c.JSON(http.StatusOK, gin.H{
  68. "success": true,
  69. "message": "",
  70. })
  71. return
  72. }