48 lines
837 B
Go
48 lines
837 B
Go
package main
|
|
|
|
import (
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
type TemplateQuay struct {
|
|
Name string
|
|
Town string
|
|
Departures []Departure
|
|
}
|
|
|
|
type TemplateContext struct {
|
|
Quays []TemplateQuay
|
|
}
|
|
|
|
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")
|
|
tpl, _ = tpl.ParseFiles("index.html")
|
|
|
|
_ = tpl.Execute(w, ctxt)
|
|
}
|