diff options
author | Julien Dessaux | 2021-05-04 11:59:13 +0200 |
---|---|---|
committer | Julien Dessaux | 2021-05-04 12:03:18 +0200 |
commit | dc4fea013c5f89054ae348cab2935eebe28a4712 (patch) | |
tree | bf3455ab4e3953489495c5621a40c108d4dba5e3 | |
parent | Updated dependencies (diff) | |
download | trains-dc4fea013c5f89054ae348cab2935eebe28a4712.tar.gz trains-dc4fea013c5f89054ae348cab2935eebe28a4712.tar.bz2 trains-dc4fea013c5f89054ae348cab2935eebe28a4712.zip |
Cleaned the navitia - webui calls
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 4 | ||||
-rw-r--r-- | internal/webui/root.go | 29 | ||||
-rw-r--r-- | pkg/model/departures.go | 6 | ||||
-rw-r--r-- | pkg/navitia_api_client/departures.go | 16 | ||||
-rw-r--r-- | pkg/navitia_api_client/departures_test.go | 8 |
6 files changed, 36 insertions, 29 deletions
@@ -9,7 +9,7 @@ require ( github.com/kr/pretty v0.2.1 // indirect github.com/mattn/go-sqlite3 v1.14.7 github.com/stretchr/testify v1.7.0 - golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b + golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) @@ -17,8 +17,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg= -golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e h1:8foAy0aoO5GkqCvAEJ4VC4P3zksTg4X4aJCDpZzmgQI= +golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= diff --git a/internal/webui/root.go b/internal/webui/root.go index c3a0ec5..ab6fdcd 100644 --- a/internal/webui/root.go +++ b/internal/webui/root.go @@ -5,7 +5,6 @@ import ( "html/template" "log" "net/http" - "time" "git.adyxax.org/adyxax/trains/pkg/model" ) @@ -15,38 +14,30 @@ var rootTemplate = template.Must(template.ParseFS(templatesFS, "html/base.html", // The page template variable type Page struct { User *model.User - Departures []Departure + Departures []model.Departure Title string } -type Departure struct { - DisplayName string - Arrival string - Odd bool -} // The root handler of the webui func rootHandler(e *env, w http.ResponseWriter, r *http.Request) error { if r.URL.Path == "/" { - var p Page user, err := tryAndResumeSession(e, r) if err != nil { http.Redirect(w, r, "/login", http.StatusFound) return nil } - p.User = user - if d, err := e.navitia.GetDepartures(e.conf.TrainStop); err != nil { - log.Printf("%+v\n%s\n", d, err) + var departures []model.Departure + if departures, err := e.navitia.GetDepartures(e.conf.TrainStop); err != nil { + log.Printf("%s; data returned: %+v\n", err, departures) + return newStatusError(http.StatusInternalServerError, fmt.Errorf("Could not get departures")) } else { - for i := 0; i < len(d.Departures); i++ { - t, err := time.Parse("20060102T150405", d.Departures[i].StopDateTime.ArrivalDateTime) - if err != nil { - panic(err) - } - p.Departures = append(p.Departures, Departure{d.Departures[i].DisplayInformations.Direction, t.Format("Mon, 02 Jan 2006 15:04:05"), i%2 == 1}) - } w.Header().Set("Cache-Control", "no-store, no-cache") } - p.Title = "Horaires des prochains trains à Crépieux la Pape" + p := Page{ + User: user, + Departures: departures, + Title: "Horaires des prochains trains à Crépieux la Pape", + } err = rootTemplate.ExecuteTemplate(w, "root.html", p) if err != nil { return newStatusError(http.StatusInternalServerError, err) diff --git a/pkg/model/departures.go b/pkg/model/departures.go new file mode 100644 index 0000000..7359d1b --- /dev/null +++ b/pkg/model/departures.go @@ -0,0 +1,6 @@ +package model + +type Departure struct { + Direction string + Arrival string +} diff --git a/pkg/navitia_api_client/departures.go b/pkg/navitia_api_client/departures.go index cd75191..3b7053d 100644 --- a/pkg/navitia_api_client/departures.go +++ b/pkg/navitia_api_client/departures.go @@ -5,6 +5,8 @@ import ( "fmt" "net/http" "time" + + "git.adyxax.org/adyxax/trains/pkg/model" ) type DeparturesResponse struct { @@ -43,14 +45,14 @@ type DeparturesResponse struct { } `json:"context"` } -func (c *Client) GetDepartures(trainStop string) (departures *DeparturesResponse, err error) { +func (c *Client) GetDepartures(trainStop string) (departures []model.Departure, err error) { request := fmt.Sprintf("%s/coverage/sncf/stop_areas/%s/departures", c.baseURL, trainStop) start := time.Now() c.mutex.Lock() defer c.mutex.Unlock() if cachedResult, ok := c.cache[request]; ok { if start.Sub(cachedResult.ts) < 60*1000*1000*1000 { - return cachedResult.result.(*DeparturesResponse), nil + return cachedResult.result.([]model.Departure), nil } } req, err := http.NewRequest("GET", request, nil) @@ -62,9 +64,17 @@ func (c *Client) GetDepartures(trainStop string) (departures *DeparturesResponse return nil, err } defer resp.Body.Close() - if err = json.NewDecoder(resp.Body).Decode(&departures); err != nil { + var data DeparturesResponse + if err = json.NewDecoder(resp.Body).Decode(&data); err != nil { return nil, err } + for i := 0; i < len(data.Departures); i++ { + t, err := time.Parse("20060102T150405", data.Departures[i].StopDateTime.ArrivalDateTime) + if err != nil { // TODO fail better + panic(err) + } + departures = append(departures, model.Departure{data.Departures[i].DisplayInformations.Direction, t.Format("Mon, 02 Jan 2006 15:04:05")}) + } c.cache[request] = cachedResult{ ts: start, result: departures, diff --git a/pkg/navitia_api_client/departures_test.go b/pkg/navitia_api_client/departures_test.go index e7d771f..9b2f52c 100644 --- a/pkg/navitia_api_client/departures_test.go +++ b/pkg/navitia_api_client/departures_test.go @@ -42,8 +42,8 @@ func TestGetDepartures(t *testing.T) { if err != nil { t.Fatalf("could not get normal-crepieux departures : %s", err) } - if len(departures.Departures) != 10 { - t.Fatalf("did not decode normal-crepieux departures properly, got %d departures when expected 10", len(departures.Departures)) + if len(departures) != 10 { + t.Fatalf("did not decode normal-crepieux departures properly, got %d departures when expected 10", len(departures)) } // test the cache (assuming the test takes less than 60 seconds (and it really should) it will be accurate) ts.Close() @@ -51,7 +51,7 @@ func TestGetDepartures(t *testing.T) { if err != nil { t.Fatalf("could not get normal-crepieux departures : %s", err) } - if len(departures.Departures) != 10 { - t.Fatalf("did not decode normal-crepieux departures properly, got %d departures when expected 10", len(departures.Departures)) + if len(departures) != 10 { + t.Fatalf("did not decode normal-crepieux departures properly, got %d departures when expected 10", len(departures)) } } |