chirpy_server/main.go

32 lines
799 B
Go
Raw Normal View History

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()
2024-10-16 21:34:04 +02:00
fsHandler := apiCfg.middlewareMetricsInc(http.StripPrefix("/app", http.FileServer(http.Dir("."))))
mux.Handle("/app/", fsHandler)
mux.HandleFunc("GET /api/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-16 21:34:04 +02:00
mux.Handle("GET /admin/metrics", http.HandlerFunc(apiCfg.serveMetrics))
mux.Handle("POST /admin/reset", http.HandlerFunc(apiCfg.serveReset))
mux.HandleFunc("POST /api/validate_chirp", decode)
2024-10-14 23:00:46 +02:00
2024-10-09 23:11:23 +02:00
server := &http.Server{
Addr: ":8080",
Handler: mux,
}
server.ListenAndServe()
}