如何实现Golang的http请求处理中间件
1个回答
展开全部
封装 http.FileServer 就可以了:
func OurLoggingHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request)) {
fmt.Println(*r.URL)
h.ServeHTTP(w ,r)
})
}
func main() {
fileHandler := http.FileServer(http.Dir("/tmp"))
wrappedHandler := OurLoggingHandler(fileHandler)
http.ListenAndServe(":8080", wrappedHandler)
}
func OurLoggingHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request)) {
fmt.Println(*r.URL)
h.ServeHTTP(w ,r)
})
}
func main() {
fileHandler := http.FileServer(http.Dir("/tmp"))
wrappedHandler := OurLoggingHandler(fileHandler)
http.ListenAndServe(":8080", wrappedHandler)
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询