From aff4790d22728d89e7e2dac8af262c92087b5b39 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 11 Sep 2021 12:27:59 +0200 Subject: Piece evry last week changes together in a big rewrite of the webui --- internal/webui/stop.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 internal/webui/stop.go (limited to 'internal/webui/stop.go') diff --git a/internal/webui/stop.go b/internal/webui/stop.go new file mode 100644 index 0000000..68d592e --- /dev/null +++ b/internal/webui/stop.go @@ -0,0 +1,50 @@ +package webui + +import ( + "fmt" + "html/template" + "net/http" + + "git.adyxax.org/adyxax/trains/pkg/model" +) + +var stopTemplate = template.Must(template.New("stop").Funcs(funcMap).ParseFS(templatesFS, "html/base.html", "html/stop.html")) + +// The page template variable +type StopPage struct { + User *model.User + Stops []model.Stop +} + +// The stop handler of the webui +func stopHandler(e *env, w http.ResponseWriter, r *http.Request) error { + if r.URL.Path == "/stop" { + user, err := tryAndResumeSession(e, r) + if err != nil { + http.Redirect(w, r, "/login", http.StatusFound) + return nil + } + switch r.Method { + case http.MethodGet: + stops, err := e.dbEnv.GetStops() + if err != nil { + return newStatusError(http.StatusInternalServerError, fmt.Errorf("Could not get train stops")) + } else { + w.Header().Set("Cache-Control", "no-store, no-cache") + } + p := StopPage{ + User: user, + Stops: stops, + } + err = stopTemplate.ExecuteTemplate(w, "stop.html", p) + if err != nil { + return newStatusError(http.StatusInternalServerError, err) + } + return nil + default: + return newStatusError(http.StatusMethodNotAllowed, fmt.Errorf(http.StatusText(http.StatusMethodNotAllowed))) + } + } else { + return newStatusError(http.StatusNotFound, fmt.Errorf("Invalid path in stopHandler")) + } +} -- cgit v1.2.3