diff --git a/README b/README index ef5be7a..07a9e5f 100644 --- a/README +++ b/README @@ -40,9 +40,7 @@ gobyexample.com signups = topics * web app - * middleware in general * return status code - * log requests * serve static files * force https * force canonical host diff --git a/src/xx-http-server-middleware.go b/src/xx-http-server-middleware.go new file mode 100644 index 0000000..db999b9 --- /dev/null +++ b/src/xx-http-server-middleware.go @@ -0,0 +1,22 @@ +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) +}