package main import ( "context" "log" "net/http" "github.com/a-h/templ" ) var clicks uint = 0 func generateHandler(template func() templ.Component, handler func()) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") template().Render(context.Background(), w) handler() } } func main() { http.Handle("/", http.FileServer(http.Dir("static"))) http.HandleFunc("/api/click", generateHandler( func() templ.Component { return Click(clicks) }, func() { clicks++ }, )) log.Fatal(http.ListenAndServe(":3333", nil)) }