diff options
Diffstat (limited to '')
-rw-r--r-- | internal/webui/stop.go | 50 |
1 files changed, 50 insertions, 0 deletions
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")) + } +} |