summaryrefslogtreecommitdiff
path: root/timer.go
blob: fe52bb08a83435bc487ac7d1d3aea85cac0f3946 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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)
}