chirpy_server/metrics.go

27 lines
579 B
Go
Raw Normal View History

2024-10-14 23:00:46 +02:00
package main
import (
"fmt"
"net/http"
)
func (cfg *apiConfig) middlewareMetricsInc(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) serveMetrics(w http.ResponseWriter, r *http.Request) {
2024-10-16 21:34:04 +02:00
w.Header().Add("Content-Type", "text/html")
2024-10-14 23:00:46 +02:00
hits := cfg.fileserverHits.Load()
2024-10-16 21:34:04 +02:00
template := `
<html>
<body>
<h1>Welcome, Chirpy Admin</h1>
<p>Chirpy has been visited %d times!</p>
</body>
</html>`
fmt.Fprintf(w, template, hits)
2024-10-14 23:00:46 +02:00
}