-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
46 lines (36 loc) · 867 Bytes
/
metrics.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
package main
import (
"fmt"
"net/http"
)
type apiConfig struct {
fileserverHits int
}
func (cfg *apiConfig) middlewareMetricsInc(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cfg.fileserverHits++
next.ServeHTTP(w, r)
})
}
func (cfg *apiConfig) resetMetrics(w http.ResponseWriter, r *http.Request) {
cfg.fileserverHits = 0
w.WriteHeader(200)
}
// displayMetrics with a template directly integrated
func (cfg *apiConfig) displayMetrics(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, `<html>
<body>
<h1>Welcome, Chirpy Admin</h1>
<p>Chirpy has been visited %d times!</p>
</body>
</html>`, cfg.fileserverHits)
}
/*
<html>
<body>
<h1>Welcome, Chirpy Admin</h1>
<p>Chirpy has been visited %d times!</p>
</body>
</html>
*/