gobyexample/065-http-server-middleware.go
Mark McGranaghan 354a9d862f reorder
2012-09-21 07:54:20 -07:00

23 lines
504 B
Go

package main
import ("net/http"; "fmt")
func hello(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(res, "Hello wrapped world")
}
func wrapMiddleware(f http.HandlerFunc) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
fmt.Println("before")
f(res, req)
fmt.Println("after")
}
}
func main() {
handler := wrapMiddleware(hello)
http.HandleFunc("/", handler)
http.ListenAndServe(":5000", nil)
}