package main import ( "fmt" "html/template" "log" "net/http" ) type handler = func(http.ResponseWriter, *http.Request) func generateHandler(file string, handler func(), data func() any) handler { tmpl := template.Must(template.ParseFiles(fmt.Sprintf("templates/%s", file))) return func(w http.ResponseWriter, r *http.Request) { handler() w.Header().Set("Content-Type", "text/html; charset=utf-8") tmpl.Execute(w, data()) } } func generateClick() handler { var clicks uint = 0 return generateHandler( "click.html", func() { clicks++ }, func() any { return clicks }, ) } func main() { http.Handle("/", http.FileServer(http.Dir("static"))) http.HandleFunc("/api/click1", generateClick()) http.HandleFunc("/api/click2", generateClick()) http.HandleFunc("/api/click3", generateClick()) log.Fatal(http.ListenAndServe(":3333", nil)) }