-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
32 lines (28 loc) · 904 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
package main
import (
"fmt"
"net/http"
)
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) metricsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(fmt.Sprintf("<html>\n\n<body>\n <h1>Welcome, Chirpy Admin</h1>\n <p>Chirpy has been visited %d times!</p>\n</body>\n\n</html>", cfg.fileServerHits)))
if err != nil {
return
}
}
func (cfg *apiConfig) resetMetricsHandler(w http.ResponseWriter, r *http.Request) {
cfg.fileServerHits = 0
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("Metrics reset"))
if err != nil {
return
}
}