Initial commit
This commit is contained in:
40
timer.go
Normal file
40
timer.go
Normal file
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user