现在看起来像这样
func cacheHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=1800")
h.ServeHTTP(w, r)
})
}
http.Handle("/", cacheHandler(http.FileServer(http.Dir("./template/index"))))
http.HandleFunc("/json", sendJSONHandler)
http.HandleFunc("/contact", contactHandler)
http.Handle("/static/", http.StripPrefix("/static/", cacheHandler(http.FileServer(http.Dir("./template/static")))))
http.ListenAndServe(":80", nil)
有没有办法一次将缓存标头设置给所有处理程序?
包装多路复用器
http.ListenAndServe(":80", cacheHandler(http.DefaultServeMux))
而不是单个处理程序。
请注意,ListendAndServe
使用http.DefaultServeMux
的处理程序时的处理程序参数是nil
。另外,将http.Handle
和http.HandleFunc
处理程序添加到http.DefaultServeMux
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句