diff options
author | Julien Dessaux | 2021-09-07 17:32:47 +0200 |
---|---|---|
committer | Julien Dessaux | 2021-09-07 17:32:47 +0200 |
commit | cf7f851a5f47bab8a979135c834dae9ead63a042 (patch) | |
tree | dc88a7650ae4ae769afcd3ac35d3b10680a08d84 /internal | |
parent | Added navitia code to fetch train stops names (diff) | |
download | trains-cf7f851a5f47bab8a979135c834dae9ead63a042.tar.gz trains-cf7f851a5f47bab8a979135c834dae9ead63a042.tar.bz2 trains-cf7f851a5f47bab8a979135c834dae9ead63a042.zip |
Added logic to fetch train stops data if missing when starting the webserver
Diffstat (limited to 'internal')
-rw-r--r-- | internal/webui/webui.go | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/internal/webui/webui.go b/internal/webui/webui.go index 6ce5bb4..1a251f4 100644 --- a/internal/webui/webui.go +++ b/internal/webui/webui.go @@ -19,6 +19,20 @@ func Run(c *config.Config, dbEnv *database.DBEnv) { http.Handle("/login", handler{&e, loginHandler}) http.Handle("/static/", http.FileServer(http.FS(staticFS))) + if i, err := dbEnv.CountTrainStops(); err == nil && i == 0 { + log.Printf("No trains stops data found, updating...") + if trainStops, err := e.navitia.GetTrainStops(); err == nil { + log.Printf("Updated trains stops data from navitia api, got %d results", len(trainStops)) + if err = dbEnv.ReplaceAndImportTrainStops(trainStops); err != nil { + if dberr, ok := err.(*database.QueryError); ok { + log.Printf("%+v", dberr.Unwrap()) + } + } + } else { + log.Printf("Failed to get trains stops data from navitia api : %+v", err) + } + } + listenStr := c.Address + ":" + c.Port log.Printf("Starting webui on %s", listenStr) log.Fatal(http.ListenAndServe(listenStr, nil)) |