forked from ardanlabs/gotraining
/
main.go
74 lines (59 loc) · 1.76 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Sample program to show how to apply middleware.
package main
import (
"log"
"net/http"
"time"
)
// fooHeader returns a handler function that will set
// the `foo` header key. Then call the provided
// handler function.
func fooHeader(hf http.HandlerFunc) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
// Set the `foo` header key.
res.Header().Set("foo", "bar")
// Call the handler that was provided.
hf(res, req)
}
}
// logger returns a handler function that will log info about
// the request. Then it calls the provided handler function.
func logger(hf http.HandlerFunc) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
// Get the current time.
start := time.Now()
// Once the handler call proceeding this defer
// is complete, log how long the request took.
defer func() {
d := time.Now().Sub(start)
log.Printf("%s %s %s", req.Method, req.URL.Path, d)
}()
// Call the handler that was provided.
hf(res, req)
}
}
// App loads the API and the middleware.
func App() http.Handler {
// Create a new mux for handling routes.
m := http.NewServeMux()
// For the root route, the logger handler first calls into the
// fooHeader handler which first calls into the Hello World handler.
// This chain of calls happen on the processing of the route.
m.HandleFunc("/",
logger(
fooHeader(
func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("Hello World"))
},
),
),
)
return m
}
func main() {
// Start the http server to handle the request for
// both versions of the API.
log.Fatal(http.ListenAndServe(":3000", App()))
}