diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-04-08 00:12:48 +0200 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-04-08 00:12:48 +0200 |
commit | 01e96380a4e48b5d338c71fad690382124195b17 (patch) | |
tree | dc40f7d639fda7c77439bd935d497503d241cfe6 /timer.go |
Initial commit
Diffstat (limited to 'timer.go')
-rw-r--r-- | timer.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/timer.go b/timer.go new file mode 100644 index 0000000..fe52bb0 --- /dev/null +++ b/timer.go @@ -0,0 +1,40 @@ +package main + +import ( + "log" + "fmt" + "net/http" + "html/template" +) + +type myServer struct { + mainTemplate *template.Template +} + +func (server *myServer) HandleMain(w http.ResponseWriter, r *http.Request) { + server.mainTemplate.Execute(w, template.HTML(`<h1><a href="/timer/test">Hello, world!</a></h1>`)) +} + +func (server *myServer) HandleTimer(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "<p>Timer %s</p>", r.PathValue("timer_id")) +} + +func main() { + log.Println("Hello") + + tpl, err := template.ParseFiles("template/main.tpl.html") + if err != nil { + log.Fatalln(err) + } + + myServer := myServer{ mainTemplate: tpl } + + fs := http.FileServer(http.Dir("static/")) + http.Handle("/static/", http.StripPrefix("/static/", fs)) + + http.HandleFunc("/", myServer.HandleMain) + http.HandleFunc("/timer/{timer_id}", myServer.HandleTimer) + + http.ListenAndServe("0.0.0.0:80", nil) +} + |