request middleware handler for native http
go get -u github.com/colindev/go-http-middleware
type AccessMiddleware struct {}
func (am *AccessMiddleware) Wrap(handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
access := "............."
fmt.Println(access)
handler(w, r)
}
}
func main() {
wrapper := middleware.New(&AccessMiddleware{})
http.Handle("/", wrapper.WrapHandlerFunc(func(w http.ResponseWriter, r *http.Request){
// handler
}))
http.ListenAndServe(":8000", nil)
}
// middlerware 同上
type server struct {}
func (server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// do some thing
}
func main() {
wrapper := middleware.New(&AccessMiddleware{})
http.ListenAndServe(":8000", wrapper.WrapHandler(server{}))
}