diff options
author | Julien Dessaux | 2021-09-11 12:27:59 +0200 |
---|---|---|
committer | Julien Dessaux | 2021-09-11 12:27:59 +0200 |
commit | aff4790d22728d89e7e2dac8af262c92087b5b39 (patch) | |
tree | a9e4ccae95529835abb0b18e856f3f40b98c0f8e /internal/webui/stop.go | |
parent | Changed the way html pages title is set (diff) | |
download | trains-aff4790d22728d89e7e2dac8af262c92087b5b39.tar.gz trains-aff4790d22728d89e7e2dac8af262c92087b5b39.tar.bz2 trains-aff4790d22728d89e7e2dac8af262c92087b5b39.zip |
Piece evry last week changes together in a big rewrite of the webui
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")) + } +} |