chirpy_server/metrics.go

27 lines
579 B
Go

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) {
w.Header().Add("Content-Type", "text/html")
hits := cfg.fileserverHits.Load()
template := `
<html>
<body>
<h1>Welcome, Chirpy Admin</h1>
<p>Chirpy has been visited %d times!</p>
</body>
</html>`
fmt.Fprintf(w, template, hits)
}