settings.go 1.7 KB

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