Timeout after 10 seconds on FetchDepartures
Also apply gofmt
This commit is contained in:
parent
330e5923af
commit
3fe42e1ff0
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
- [ ] Use timeout for fetching departures
|
- [x] Use timeout for fetching departures
|
||||||
- [ ] Write ESP8266 client
|
- [ ] Write ESP8266 client
|
||||||
- [ ] Add basic auth
|
- [ ] Add basic auth
|
||||||
- [ ] Create Nix package
|
- [ ] Create Nix package
|
||||||
|
12
main.go
12
main.go
@ -7,6 +7,7 @@ import "net/http"
|
|||||||
import "net/url"
|
import "net/url"
|
||||||
import "slices"
|
import "slices"
|
||||||
import "strconv"
|
import "strconv"
|
||||||
|
import "time"
|
||||||
|
|
||||||
type DMResponse struct {
|
type DMResponse struct {
|
||||||
Departures []DMDeparture `json:"departureList"`
|
Departures []DMDeparture `json:"departureList"`
|
||||||
@ -38,7 +39,6 @@ type DMDateTime struct {
|
|||||||
Minute string `json:"minute"`
|
Minute string `json:"minute"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Use different client (with timeout)
|
|
||||||
func FetchDepartures(stopId string) (*DMResponse, error) {
|
func FetchDepartures(stopId string) (*DMResponse, error) {
|
||||||
// Create request object
|
// Create request object
|
||||||
req, err := http.NewRequest("GET", "https://www.vrn.de/mngvrn/XML_DM_REQUEST", nil)
|
req, err := http.NewRequest("GET", "https://www.vrn.de/mngvrn/XML_DM_REQUEST", nil)
|
||||||
@ -61,8 +61,11 @@ func FetchDepartures(stopId string) (*DMResponse, error) {
|
|||||||
query.Set("useRealtime", "1")
|
query.Set("useRealtime", "1")
|
||||||
req.URL.RawQuery = query.Encode()
|
req.URL.RawQuery = query.Encode()
|
||||||
|
|
||||||
// Send the request
|
// Send the request, wait max 10 seconds
|
||||||
res, err := http.DefaultClient.Do(req)
|
client := http.Client{
|
||||||
|
Timeout: 10 * time.Second,
|
||||||
|
}
|
||||||
|
res, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -92,7 +95,6 @@ func main() {
|
|||||||
platform = &query["platform"][0]
|
platform = &query["platform"][0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ds, err := FetchDepartures(stopId)
|
ds, err := FetchDepartures(stopId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
@ -108,7 +110,7 @@ func main() {
|
|||||||
|
|
||||||
direction := d.ServingLine.Direction
|
direction := d.ServingLine.Direction
|
||||||
if len(d.OnwardStopSeq) != 0 {
|
if len(d.OnwardStopSeq) != 0 {
|
||||||
last := d.OnwardStopSeq[len(d.OnwardStopSeq) - 1]
|
last := d.OnwardStopSeq[len(d.OnwardStopSeq)-1]
|
||||||
if slices.Contains([]string{"5", "6"}, last.PlaceID) {
|
if slices.Contains([]string{"5", "6"}, last.PlaceID) {
|
||||||
direction = last.NameWO
|
direction = last.NameWO
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user