2024-10-09 23:11:23 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-10-14 23:00:46 +02:00
|
|
|
"sync/atomic"
|
2024-10-09 23:11:23 +02:00
|
|
|
)
|
|
|
|
|
2024-10-14 23:00:46 +02:00
|
|
|
type apiConfig struct {
|
|
|
|
fileserverHits atomic.Int32
|
|
|
|
}
|
|
|
|
|
2024-10-09 23:11:23 +02:00
|
|
|
func main() {
|
2024-10-14 23:00:46 +02:00
|
|
|
apiCfg := &apiConfig{}
|
2024-10-09 23:11:23 +02:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
fileServer := http.FileServer(http.Dir("."))
|
2024-10-14 23:00:46 +02:00
|
|
|
mux.Handle("/app", apiCfg.middlewareMetricsInc(http.StripPrefix("/app", fileServer)))
|
2024-10-09 23:11:23 +02:00
|
|
|
mux.Handle("/app/assets/", http.StripPrefix("/app/assets/", http.FileServer(http.Dir("./assets/"))))
|
2024-10-14 23:00:46 +02:00
|
|
|
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, req *http.Request) {
|
2024-10-09 23:11:23 +02:00
|
|
|
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte("OK"))
|
|
|
|
})
|
2024-10-14 23:00:46 +02:00
|
|
|
mux.Handle("GET /metrics", http.HandlerFunc(apiCfg.serveMetrics))
|
|
|
|
mux.Handle("POST /reset", http.HandlerFunc(apiCfg.serveReset))
|
|
|
|
|
2024-10-09 23:11:23 +02:00
|
|
|
server := &http.Server{
|
|
|
|
Addr: ":8080",
|
|
|
|
Handler: mux,
|
|
|
|
}
|
|
|
|
server.ListenAndServe()
|
|
|
|
}
|