package main
import (
"html/template"
"log"
"net/http"
"time"
)
type TemplateQuay struct {
Name string
Town string
Departures []Departure
}
type TemplateContext struct {
Quays []TemplateQuay
}
func formatTime(t time.Time) string {
return t.Format("15:04")
}
var tplFunctions = template.FuncMap{"formatTime": formatTime}
func serveHttp() {
http.HandleFunc("/", handleIndex)
log.Println("Serving on port 4444")
err := http.ListenAndServe(":4444", nil)
if err != nil {
log.Fatal(err)
}
}
func handleIndex(w http.ResponseWriter, r *http.Request) {
var ctxt = TemplateContext{}
for _, quay := range Quays {
departures, err := getDepartures(quay)
if err != nil {
log.Printf("Error retrieving departures for quay '%s'", quay.ID)
continue
}
ctxt.Quays = append(ctxt.Quays, TemplateQuay{quay.Name, quay.Town, departures})
}
tpl := template.New("index.html").Funcs(tplFunctions)
tpl, _ = tpl.ParseFiles("index.html")
_ = tpl.Execute(w, ctxt)
}