http_server.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package http_server
  2. import (
  3. "fmt"
  4. "io/fs"
  5. "net/http"
  6. "pmail/config"
  7. "pmail/controllers"
  8. "pmail/controllers/email"
  9. "pmail/session"
  10. "time"
  11. )
  12. const HttpPort = 80
  13. // 这个服务是为了拦截http请求转发到https
  14. var httpServer *http.Server
  15. func HttpStop() {
  16. if httpServer != nil {
  17. httpServer.Close()
  18. }
  19. }
  20. func HttpStart() {
  21. mux := http.NewServeMux()
  22. if config.Instance.HttpsEnabled != 2 {
  23. mux.HandleFunc("/", controllers.Interceptor)
  24. } else {
  25. fe, err := fs.Sub(local, "dist")
  26. if err != nil {
  27. panic(err)
  28. }
  29. mux.Handle("/", http.FileServer(http.FS(fe)))
  30. mux.HandleFunc("/api/ping", contextIterceptor(controllers.Ping))
  31. mux.HandleFunc("/api/login", contextIterceptor(controllers.Login))
  32. mux.HandleFunc("/api/group", contextIterceptor(controllers.GetUserGroup))
  33. mux.HandleFunc("/api/email/list", contextIterceptor(email.EmailList))
  34. mux.HandleFunc("/api/email/detail", contextIterceptor(email.EmailDetail))
  35. mux.HandleFunc("/api/email/send", contextIterceptor(email.Send))
  36. mux.HandleFunc("/api/settings/modify_password", contextIterceptor(controllers.ModifyPassword))
  37. mux.HandleFunc("/attachments/", contextIterceptor(controllers.GetAttachments))
  38. mux.HandleFunc("/attachments/download/", contextIterceptor(controllers.Download))
  39. }
  40. httpServer = &http.Server{
  41. Addr: fmt.Sprintf(":%d", HttpPort),
  42. Handler: session.Instance.LoadAndSave(mux),
  43. ReadTimeout: time.Second * 60,
  44. WriteTimeout: time.Second * 60,
  45. }
  46. err := httpServer.ListenAndServe()
  47. if err != nil {
  48. panic(err)
  49. }
  50. }