an middleware
This commit is contained in:
parent
8cb4f1080a
commit
b71b350973
2
README
2
README
@ -40,9 +40,7 @@ gobyexample.com signups
|
|||||||
|
|
||||||
= topics
|
= topics
|
||||||
* web app
|
* web app
|
||||||
* middleware in general
|
|
||||||
* return status code
|
* return status code
|
||||||
* log requests
|
|
||||||
* serve static files
|
* serve static files
|
||||||
* force https
|
* force https
|
||||||
* force canonical host
|
* force canonical host
|
||||||
|
22
src/xx-http-server-middleware.go
Normal file
22
src/xx-http-server-middleware.go
Normal file
@ -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)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user