chirpy_server/main.go

32 lines
799 B
Go

package main
import (
"net/http"
"sync/atomic"
)
type apiConfig struct {
fileserverHits atomic.Int32
}
func main() {
apiCfg := &apiConfig{}
mux := http.NewServeMux()
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) {
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
})
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)
server := &http.Server{
Addr: ":8080",
Handler: mux,
}
server.ListenAndServe()
}