From 4a2fb7e82d5d617298cb28b66485fc6f30c55781 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 21 Apr 2021 17:23:07 +0200 Subject: Reworked the webui package, added authentication feature and tests --- internal/webui/root.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 internal/webui/root.go (limited to 'internal/webui/root.go') diff --git a/internal/webui/root.go b/internal/webui/root.go new file mode 100644 index 0000000..c3a0ec5 --- /dev/null +++ b/internal/webui/root.go @@ -0,0 +1,58 @@ +package webui + +import ( + "fmt" + "html/template" + "log" + "net/http" + "time" + + "git.adyxax.org/adyxax/trains/pkg/model" +) + +var rootTemplate = template.Must(template.ParseFS(templatesFS, "html/base.html", "html/root.html")) + +// The page template variable +type Page struct { + User *model.User + Departures []Departure + Title string +} +type Departure struct { + DisplayName string + Arrival string + Odd bool +} + +// The root handler of the webui +func rootHandler(e *env, w http.ResponseWriter, r *http.Request) error { + if r.URL.Path == "/" { + var p Page + user, err := tryAndResumeSession(e, r) + if err != nil { + http.Redirect(w, r, "/login", http.StatusFound) + return nil + } + p.User = user + if d, err := e.navitia.GetDepartures(e.conf.TrainStop); err != nil { + log.Printf("%+v\n%s\n", d, err) + } else { + for i := 0; i < len(d.Departures); i++ { + t, err := time.Parse("20060102T150405", d.Departures[i].StopDateTime.ArrivalDateTime) + if err != nil { + panic(err) + } + p.Departures = append(p.Departures, Departure{d.Departures[i].DisplayInformations.Direction, t.Format("Mon, 02 Jan 2006 15:04:05"), i%2 == 1}) + } + w.Header().Set("Cache-Control", "no-store, no-cache") + } + p.Title = "Horaires des prochains trains à Crépieux la Pape" + err = rootTemplate.ExecuteTemplate(w, "root.html", p) + if err != nil { + return newStatusError(http.StatusInternalServerError, err) + } + return nil + } else { + return newStatusError(http.StatusNotFound, fmt.Errorf("Invalid path in rootHandler")) + } +} -- cgit v1.2.3