settings.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package v1
  2. import (
  3. "net/http"
  4. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/types/backend"
  5. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/common"
  6. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/settings"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func (cb *ControllerBase) SettingsHandler(c *gin.Context) {
  10. var err error
  11. defer func() {
  12. // 统一的异常处理
  13. cb.ErrorProcess(c, "SettingsHandler", err)
  14. }()
  15. switch c.Request.Method {
  16. case "GET":
  17. {
  18. // 回复没有密码的 settings
  19. c.JSON(http.StatusOK, settings.Get().GetNoPasswordSettings())
  20. }
  21. case "PUT":
  22. {
  23. // 修改设置,这里不允许修改密码
  24. reqSetupInfo := settings.Settings{}
  25. err = c.ShouldBindJSON(&reqSetupInfo)
  26. if err != nil {
  27. return
  28. }
  29. // 需要去除 user 的 password 信息再保存,也就是继承之前的 password 即可
  30. nowPassword := settings.Get().UserInfo.Password
  31. reqSetupInfo.UserInfo.Password = nowPassword
  32. err = settings.SetFullNewSettings(&reqSetupInfo)
  33. if err != nil {
  34. return
  35. }
  36. // ----------------------------------------
  37. // 设置接口的 API TOKEN
  38. if settings.Get().ExperimentalFunction.ApiKeySettings.Enabled == true {
  39. common.SetApiToken(settings.Get().ExperimentalFunction.ApiKeySettings.Key)
  40. } else {
  41. common.SetApiToken("")
  42. }
  43. // ----------------------------------------
  44. c.JSON(http.StatusOK, backend.ReplyCommon{Message: "Settings Save Success"})
  45. // 回复完毕后,发送重启 http server 的信号
  46. cb.restartSignal <- 1
  47. }
  48. default:
  49. c.JSON(http.StatusNoContent, backend.ReplyCommon{Message: "Settings Request.Method Error"})
  50. }
  51. }