http_server.go 1.5 KB

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