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/specificStop.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/specificStop.go | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/internal/webui/specificStop.go b/internal/webui/specificStop.go new file mode 100644 index 0000000..64dd04a --- /dev/null +++ b/internal/webui/specificStop.go @@ -0,0 +1,68 @@ +package webui + +import ( + "fmt" + "html/template" + "log" + "net/http" + "path" + "regexp" + + "git.adyxax.org/adyxax/trains/pkg/model" +) + +var validStopId = regexp.MustCompile(`^stop_area:[a-zA-Z]+:\d+$`) + +var specificStopTemplate = template.Must(template.New("specificStop").Funcs(funcMap).ParseFS(templatesFS, "html/base.html", "html/specificStop.html")) + +// The page template variable +type SpecificStopPage struct { + User *model.User + Stop string + Departures []model.Departure +} + +// The stop handler of the webui +func specificStopHandler(e *env, w http.ResponseWriter, r *http.Request) error { + if path.Dir(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: + id := path.Base(r.URL.Path) + if id == "" { + return newStatusError(http.StatusBadRequest, fmt.Errorf("No id in query string")) // TODO should we redirect to root page to chose a stop id? + } + if ok := validStopId.MatchString(id); !ok { + return newStatusError(http.StatusBadRequest, fmt.Errorf("Invalid stop id")) + } + stop, err := e.dbEnv.GetStop(id) + if err != nil { + return newStatusError(http.StatusBadRequest, fmt.Errorf("Stop id not found in database")) // TODO do better + } + if departures, err := e.navitia.GetDepartures(stop.Id); err != nil { + log.Printf("%s; data returned: %+v\n", err, departures) + return newStatusError(http.StatusInternalServerError, fmt.Errorf("Could not get departures")) + } else { + w.Header().Set("Cache-Control", "no-store, no-cache") + p := SpecificStopPage{ + User: user, + Stop: stop.Name, + Departures: departures, + } + err = specificStopTemplate.ExecuteTemplate(w, "specificStop.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 specificStopHandler")) + } +} |