-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
37 lines (32 loc) · 850 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
package main
import (
"fmt"
"net/http"
)
func (cfg *apiConfig) middlewareMetricInc(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
cfg.fileserverHits.Add(1)
next.ServeHTTP(w, req)
})
}
func (cfg *apiConfig) handlerMetric(w http.ResponseWriter, req *http.Request) {
result := fmt.Sprintf(`
<html>
<body>
<h1>Welcome, Chirpy Admin</h1>
<p>Chirpy has been visited %d times!</p>
</body>
</html>
`, cfg.fileserverHits.Load())
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(200)
w.Write([]byte(result))
}
func (cfg *apiConfig) handlerMetricReset(w http.ResponseWriter, req *http.Request) {
if cfg.platform != "dev" {
respondWithError(w, http.StatusForbidden, "not a dev")
return
}
cfg.db.DeleteAllUsers(req.Context())
cfg.fileserverHits.Store(0)
}