feat: initial commit
This commit is contained in:
61
site/handler.go
Normal file
61
site/handler.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package site
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type counter struct {
|
||||
hits map[string]int
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
type handler struct {
|
||||
logger *slog.Logger
|
||||
requestsCounter *counter
|
||||
}
|
||||
|
||||
func newHandler(logger *slog.Logger) *handler {
|
||||
return &handler{
|
||||
logger: logger,
|
||||
requestsCounter: &counter{hits: make(map[string]int)},
|
||||
}
|
||||
}
|
||||
|
||||
func (h *handler) LoggingMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
h.logger.Debug("Received request", "path", r.URL.Path)
|
||||
next.ServeHTTP(w, r)
|
||||
h.logger.Debug("Finished Request", "path", r.URL.Path)
|
||||
})
|
||||
}
|
||||
|
||||
func (h *handler) RequestsCounterMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
h.requestsCounter.mu.Lock()
|
||||
h.requestsCounter.hits[r.URL.Path] += 1
|
||||
h.requestsCounter.mu.Unlock()
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func (h *handler) Ping(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "Pong")
|
||||
}
|
||||
|
||||
func (h *handler) Metrics(w http.ResponseWriter, r *http.Request) {
|
||||
h.requestsCounter.mu.Lock()
|
||||
defer h.requestsCounter.mu.Unlock()
|
||||
for path, hits := range h.requestsCounter.hits {
|
||||
fmt.Fprintf(w, "http_requests_total{handler=\"%s\"} %d\n", path, hits)
|
||||
}
|
||||
}
|
||||
func (h *handler) Healthz(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "Ok")
|
||||
}
|
||||
|
||||
func (h *handler) NotFound(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Page Not Found", http.StatusNotFound)
|
||||
}
|
||||
Reference in New Issue
Block a user