main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package http_server
  2. import (
  3. "bytes"
  4. "embed"
  5. "encoding/hex"
  6. "encoding/json"
  7. "fmt"
  8. log "github.com/sirupsen/logrus"
  9. "github.com/spf13/cast"
  10. "io/fs"
  11. "math/rand"
  12. "net"
  13. "net/http"
  14. "os"
  15. "pmail/controllers"
  16. "pmail/controllers/email"
  17. "pmail/dto"
  18. "pmail/dto/response"
  19. "pmail/session"
  20. "time"
  21. )
  22. //go:embed dist/*
  23. var local embed.FS
  24. var ip string
  25. const HttpPort = 80
  26. func Start() {
  27. log.Infof("Http Server Start at :%d", HttpPort)
  28. mux := http.NewServeMux()
  29. fe, err := fs.Sub(local, "dist")
  30. if err != nil {
  31. panic(err)
  32. }
  33. mux.Handle("/", http.FileServer(http.FS(fe)))
  34. mux.HandleFunc("/api/ping", contextIterceptor(controllers.Ping))
  35. mux.HandleFunc("/api/login", contextIterceptor(controllers.Login))
  36. mux.HandleFunc("/api/group", contextIterceptor(controllers.GetUserGroup))
  37. mux.HandleFunc("/api/email/list", contextIterceptor(email.EmailList))
  38. mux.HandleFunc("/api/email/detail", contextIterceptor(email.EmailDetail))
  39. mux.HandleFunc("/api/email/send", contextIterceptor(email.Send))
  40. mux.HandleFunc("/api/settings/modify_password", contextIterceptor(controllers.ModifyPassword))
  41. mux.HandleFunc("/attachments/", contextIterceptor(controllers.GetAttachments))
  42. mux.HandleFunc("/attachments/download/", contextIterceptor(controllers.Download))
  43. server := &http.Server{
  44. Addr: fmt.Sprintf(":%d", HttpPort),
  45. Handler: session.Instance.LoadAndSave(mux),
  46. ReadTimeout: time.Second * 60,
  47. WriteTimeout: time.Second * 60,
  48. }
  49. //err := server.ListenAndServeTLS( "config/ssl/public.crt", "config/ssl/private.key", nil)
  50. err = server.ListenAndServe()
  51. if err != nil {
  52. panic(err)
  53. }
  54. }
  55. func getLocalIP() string {
  56. ip := "127.0.0.1"
  57. addrs, err := net.InterfaceAddrs()
  58. if err != nil {
  59. return ip
  60. }
  61. for _, a := range addrs {
  62. if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
  63. if ipnet.IP.To4() != nil {
  64. ip = ipnet.IP.String()
  65. break
  66. }
  67. }
  68. }
  69. return ip
  70. }
  71. func genLogID() string {
  72. r := rand.New(rand.NewSource(time.Now().UnixMicro()))
  73. if ip == "" {
  74. ip = getLocalIP()
  75. }
  76. now := time.Now()
  77. timestamp := uint32(now.Unix())
  78. timeNano := now.UnixNano()
  79. pid := os.Getpid()
  80. b := bytes.Buffer{}
  81. b.WriteString(hex.EncodeToString(net.ParseIP(ip).To4()))
  82. b.WriteString(fmt.Sprintf("%x", timestamp&0xffffffff))
  83. b.WriteString(fmt.Sprintf("%04x", timeNano&0xffff))
  84. b.WriteString(fmt.Sprintf("%04x", pid&0xffff))
  85. b.WriteString(fmt.Sprintf("%06x", r.Int31n(1<<24)))
  86. b.WriteString("b0")
  87. return b.String()
  88. }
  89. // 注入context
  90. func contextIterceptor(h controllers.HandlerFunc) http.HandlerFunc {
  91. return func(w http.ResponseWriter, r *http.Request) {
  92. if w.Header().Get("Content-Type") == "" {
  93. w.Header().Set("Content-Type", "application/json")
  94. }
  95. ctx := &dto.Context{}
  96. ctx.Context = r.Context()
  97. ctx.SetValue(dto.LogID, genLogID())
  98. lang := r.Header.Get("Lang")
  99. if lang == "" {
  100. lang = "en"
  101. }
  102. ctx.Lang = lang
  103. user := cast.ToString(session.Instance.Get(ctx, "user"))
  104. if user != "" {
  105. _ = json.Unmarshal([]byte(user), &ctx.UserInfo)
  106. }
  107. if ctx.UserInfo == nil || ctx.UserInfo.ID == 0 {
  108. if r.URL.Path != "/api/ping" && r.URL.Path != "/api/login" {
  109. response.NewErrorResponse(response.NeedLogin, "登陆已失效!", "").FPrint(w)
  110. return
  111. }
  112. }
  113. h(ctx, w, r)
  114. }
  115. }