Want to contribute? Fork me on Codeberg.org!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

37 lines
860 B

2 years ago
package main
import (
"fmt"
"html/template"
2 years ago
"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())
}
}
2 years ago
func generateClick() handler {
var clicks uint = 0
return generateHandler(
"click.html",
func() { clicks++ },
func() any { return clicks },
)
}
2 years ago
func main() {
http.Handle("/", http.FileServer(http.Dir("static")))
http.HandleFunc("/api/click1", generateClick())
http.HandleFunc("/api/click2", generateClick())
http.HandleFunc("/api/click3", generateClick())
2 years ago
log.Fatal(http.ListenAndServe(":3333", nil))
2 years ago
}