chirpy_server/main.go
2024-10-14 23:00:46 +02:00

32 lines
834 B
Go

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