http.FileServer修改Header

定义一个函数包裹Handler,返回HandleFunc,然后通过ResponseWriter修改Header。

func changeHeaderThenServe(h http.Handler) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        // Set some header.
        w.Header().Add("Keep-Alive", "300")
        // Serve with the actual handler.
        h.ServeHTTP(w, r)
    }
}

通过FilServer访问静态资源。

func main() {
  h := http.FileServer(http.Dir("static/"))
  h = http.StripPrefix("/static/", h)
  http.HandleFunc("/static/", changeHeaderThenServe(h))
  http.ListenAndServe("localhost:80", nil)
}

参考: Google Groups

你可能感兴趣的:(http.FileServer修改Header)