gobyexample/088-http-server-middleware/http-server-middleware.go
Mark McGranaghan ebe6383f98 renumber again
2012-09-23 13:29:28 -07:00

26 lines
538 B
Go

// ## HTTP Server Middleware
package main
import "net/http"
import "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)
}