option.go 1.9 KB

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