优化web逻辑

This commit is contained in:
Alex Yang
2025-11-27 19:13:09 +08:00
parent 46acf4123a
commit 79eddf7fb2
4 changed files with 65 additions and 18 deletions

View File

@@ -86,8 +86,16 @@ func (s *Server) Start() error {
mux.HandleFunc("/ws/stats", s.handleWebSocketStats)
}
// 静态文件服务(可后续添加前端界面)
mux.Handle("/", http.FileServer(http.Dir("./static")))
// 自定义静态文件服务处理器,用于禁用浏览器缓存
fileServer := http.FileServer(http.Dir("./static"))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// 添加Cache-Control头禁用浏览器缓存
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "Thu, 01 Jan 1970 00:00:00 GMT")
// 使用StripPrefix处理路径
http.StripPrefix("/", fileServer).ServeHTTP(w, r)
})
s.server = &http.Server{
Addr: fmt.Sprintf("%s:%d", s.config.Host, s.config.Port),