web_ui.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package main
  2. import (
  3. "compress/gzip"
  4. "context"
  5. "embed"
  6. "io/fs"
  7. "net/http"
  8. "strings"
  9. "time"
  10. )
  11. //go:embed web/*
  12. var webRootFS embed.FS
  13. type WebUI struct {
  14. fs fs.FS
  15. mux *http.ServeMux
  16. server *http.Server
  17. }
  18. type gzipResponseWriter struct {
  19. http.ResponseWriter
  20. writer *gzip.Writer
  21. }
  22. func (w gzipResponseWriter) Write(b []byte) (int, error) {
  23. return w.writer.Write(b)
  24. }
  25. func gzipHandler(h http.Handler) http.Handler {
  26. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  27. if strings.HasPrefix(r.URL.Path, "/api/") {
  28. w.Header().Set("Cache-Control", "no-store")
  29. }
  30. if r.Method == http.MethodHead ||
  31. !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  32. h.ServeHTTP(w, r)
  33. return
  34. }
  35. path := strings.ToLower(r.URL.Path)
  36. if !(strings.HasSuffix(path, ".html") || strings.HasSuffix(path, ".js") ||
  37. strings.HasSuffix(path, ".css")) {
  38. h.ServeHTTP(w, r)
  39. return
  40. }
  41. gz, err := gzip.NewWriterLevel(w, gzip.DefaultCompression)
  42. if err != nil {
  43. h.ServeHTTP(w, r)
  44. return
  45. }
  46. defer gz.Close()
  47. w.Header().Set("Content-Encoding", "gzip")
  48. w.Header().Set("Vary", "Accept-Encoding")
  49. w.Header().Del("Content-Length")
  50. h.ServeHTTP(&gzipResponseWriter{ResponseWriter: w, writer: gz}, r)
  51. })
  52. }
  53. func staticHandler(h http.Handler) http.Handler {
  54. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  55. path := r.URL.Path
  56. if strings.HasPrefix(path, "/static/") {
  57. w.Header().Set("Cache-Control", "public, max-age=86400")
  58. }
  59. h.ServeHTTP(w, r)
  60. })
  61. }
  62. func NewWebUI(addr string) (*WebUI, error) {
  63. sub, err := fs.Sub(webRootFS, "web")
  64. if err != nil {
  65. return nil, err
  66. }
  67. mux := http.NewServeMux()
  68. static := http.FileServer(http.FS(sub))
  69. mux.Handle(
  70. "/static/",
  71. staticHandler(static),
  72. )
  73. registerRoutes(mux)
  74. return &WebUI{
  75. fs: sub,
  76. mux: mux,
  77. server: &http.Server{
  78. Addr: addr,
  79. Handler: gzipHandler(mux),
  80. // 只读请求头阶段设时限: 移动网络掉线/半开连接不会长期占住 goroutine。
  81. // 不设 ReadTimeout/WriteTimeout —— 日志流(SSE)与 SSH 终端(WebSocket)是长连接,
  82. // 全局写超时会把它们按时掐断。
  83. ReadHeaderTimeout: 10 * time.Second,
  84. IdleTimeout: 120 * time.Second,
  85. },
  86. }, nil
  87. }
  88. func (ui *WebUI) Start() error {
  89. startSessionCleaner() // session过期自动回收
  90. ui.mux.HandleFunc("/", ui.rootHandler)
  91. return ui.server.ListenAndServe()
  92. }
  93. func (ui *WebUI) Stop() error {
  94. if ui.server == nil {
  95. return nil
  96. }
  97. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  98. defer cancel()
  99. err := ui.server.Shutdown(ctx)
  100. if err != nil {
  101. _ = ui.server.Close()
  102. }
  103. return err
  104. }