Files
timer/timer.go
2024-04-08 00:12:48 +02:00

41 lines
936 B
Go

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)
}