https_server.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package http_server
  2. import (
  3. "embed"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/Jinnrry/pmail/config"
  7. "github.com/Jinnrry/pmail/controllers"
  8. "github.com/Jinnrry/pmail/dto/response"
  9. "github.com/Jinnrry/pmail/i18n"
  10. "github.com/Jinnrry/pmail/models"
  11. "github.com/Jinnrry/pmail/session"
  12. "github.com/Jinnrry/pmail/utils/context"
  13. "github.com/Jinnrry/pmail/utils/id"
  14. olog "log"
  15. "net/http"
  16. "time"
  17. log "github.com/sirupsen/logrus"
  18. "github.com/spf13/cast"
  19. )
  20. //go:embed dist/*
  21. var local embed.FS
  22. var httpsServer *http.Server
  23. type nullWrite struct {
  24. }
  25. func (w *nullWrite) Write(p []byte) (int, error) {
  26. return len(p), nil
  27. }
  28. func HttpsStart() {
  29. mux := http.NewServeMux()
  30. router(mux)
  31. // go http server会打一堆没用的日志,写一个空的日志处理器,屏蔽掉日志输出
  32. nullLog := olog.New(&nullWrite{}, "", olog.Ldate)
  33. HttpsPort := 443
  34. if config.Instance.HttpsPort > 0 {
  35. HttpsPort = config.Instance.HttpsPort
  36. }
  37. if config.Instance.HttpsEnabled != 2 {
  38. log.Infof("Https Server Start On Port :%d", HttpsPort)
  39. httpsServer = &http.Server{
  40. Addr: fmt.Sprintf(":%d", HttpsPort),
  41. Handler: session.Instance.LoadAndSave(mux),
  42. ReadTimeout: time.Second * 90,
  43. WriteTimeout: time.Second * 90,
  44. ErrorLog: nullLog,
  45. }
  46. err := httpsServer.ListenAndServeTLS(config.Instance.SSLPublicKeyPath, config.Instance.SSLPrivateKeyPath)
  47. if err != nil {
  48. panic(err)
  49. }
  50. }
  51. }
  52. func HttpsStop() {
  53. if httpsServer != nil {
  54. httpsServer.Close()
  55. }
  56. }
  57. // 注入context
  58. func contextIterceptor(h controllers.HandlerFunc) http.HandlerFunc {
  59. return func(w http.ResponseWriter, r *http.Request) {
  60. if w.Header().Get("Content-Type") == "" {
  61. w.Header().Set("Content-Type", "application/json")
  62. }
  63. ctx := &context.Context{}
  64. ctx.Context = r.Context()
  65. ctx.SetValue(context.LogID, id.GenLogID())
  66. lang := r.Header.Get("Lang")
  67. if lang == "" {
  68. lang = "en"
  69. }
  70. ctx.Lang = lang
  71. if config.IsInit {
  72. user := cast.ToString(session.Instance.Get(ctx, "user"))
  73. var userInfo *models.User
  74. if user != "" {
  75. _ = json.Unmarshal([]byte(user), &userInfo)
  76. }
  77. if userInfo != nil && userInfo.ID > 0 {
  78. ctx.UserID = userInfo.ID
  79. ctx.UserName = userInfo.Name
  80. ctx.UserAccount = userInfo.Account
  81. ctx.IsAdmin = userInfo.IsAdmin == 1
  82. }
  83. if ctx.UserID == 0 {
  84. if r.URL.Path != "/api/ping" && r.URL.Path != "/api/login" {
  85. response.NewErrorResponse(response.NeedLogin, i18n.GetText(ctx.Lang, "login_exp"), "").FPrint(w)
  86. return
  87. }
  88. }
  89. } else if r.URL.Path != "/api/setup" {
  90. response.NewErrorResponse(response.NeedSetup, "", "").FPrint(w)
  91. return
  92. }
  93. h(ctx, w, r)
  94. }
  95. }