chirpy_server/internal/handler/metrics.go

28 lines
617 B
Go

package handler
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.Add(1)
next.ServeHTTP(w, r)
})
}
func (cfg *APIConfig) Metrics(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
hits := cfg.FileserverHits.Load()
template := `
<html>
<body>
<h1>Welcome, Chirpy Admin</h1>
<p>Chirpy has been visited %d times!</p>
</body>
</html>`
w.Write([]byte(fmt.Sprintf(template, hits)))
}