From ae7b2f170aa0e9a8414acd0899ccfc08e69fd74f Mon Sep 17 00:00:00 2001 From: Paul Brinkmeier Date: Fri, 25 Apr 2025 12:04:47 +0200 Subject: [PATCH 1/3] Add EFAClient interface --- .gitignore | 1 + main.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 5e7cfbf..de8e544 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ result vrnp +main *.swp diff --git a/main.go b/main.go index 24e6c8d..44a2b9b 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import "net/http" import "net/url" import "os" import "slices" +import "strings" import "strconv" import "time" @@ -43,11 +44,23 @@ type DMDateTime struct { Minute string `json:"minute"` } -func FetchDepartures(stopId string) (DMResponse, error) { +type EFAClient interface { + GetName() string + BuildRequest(string) (*http.Request, error) +} + +type VRNEFAClient struct { +} + +func (c VRNEFAClient) GetName() string { + return "VRN" +} + +func (c VRNEFAClient) BuildRequest(stopId string) (*http.Request, error) { // Create request object req, err := http.NewRequest("GET", "https://www.vrn.de/mngvrn/XML_DM_REQUEST", nil) if err != nil { - return DMResponse{}, err + return nil, err } // Configure our request @@ -65,6 +78,45 @@ func FetchDepartures(stopId string) (DMResponse, error) { query.Set("useRealtime", "1") req.URL.RawQuery = query.Encode() + return req, nil +} + +type KVVEFAClient struct { +} + +func (c KVVEFAClient) GetName() string { + return "KVV" +} + +func (c KVVEFAClient) BuildRequest(stopId string) (*http.Request, error) { + form := url.Values{} + form.Set("action", "XSLT_DM_REQUEST") + form.Set("name_dm", stopId) + form.Set("type_dm", "stop") + form.Set("useRealtime", "1") + form.Set("limit", "10") + form.Set("mode", "direct") + form.Set("outputFormat", "json") + body := strings.NewReader(form.Encode()) + + req, err := http.NewRequest("POST", "https://www.kvv.de/tunnelEfaDirect.php", body) + if err != nil { + return nil, err + } + + req.Header.Set("User-Agent", "coolio/1.0") + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + return req, nil +} + +func FetchDepartures(stopId string) (DMResponse, error) { + var c EFAClient = KVVEFAClient{} + req, err := c.BuildRequest(stopId) + if err != nil { + return DMResponse{}, err + } + // Send the request, wait max 10 seconds client := http.Client{ Timeout: 10 * time.Second, From dcc3cfa44f0173c58f9714c4c631f2ee3eb3a471 Mon Sep 17 00:00:00 2001 From: Paul Brinkmeier Date: Fri, 25 Apr 2025 12:30:19 +0200 Subject: [PATCH 2/3] Send requests to different servers using round-robin --- main.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 44a2b9b..967b8e2 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import "os" import "slices" import "strings" import "strconv" +import "sync/atomic" import "time" // JSON unmarshaling types for departure monitor API @@ -110,8 +111,7 @@ func (c KVVEFAClient) BuildRequest(stopId string) (*http.Request, error) { return req, nil } -func FetchDepartures(stopId string) (DMResponse, error) { - var c EFAClient = KVVEFAClient{} +func FetchDepartures(c EFAClient, stopId string) (DMResponse, error) { req, err := c.BuildRequest(stopId) if err != nil { return DMResponse{}, err @@ -203,6 +203,13 @@ func main() { panic("Required environment variable VRNP_PASSWORD is not set") } + // Use round-robin to send incoming requests to different servers + var efaClient atomic.Uint64 + efaClients := []EFAClient{ + VRNEFAClient{}, + KVVEFAClient{}, + } + http.HandleFunc("/departures", func(w http.ResponseWriter, r *http.Request) { user, pass, ok := r.BasicAuth() if !(ok && user == "admin" && pass == password) { @@ -224,7 +231,8 @@ func main() { platform = &query["platform"][0] } - ds, err := FetchDepartures(stopId) + c := efaClients[(efaClient.Add(1) - 1) % uint64(len(efaClients))] + ds, err := FetchDepartures(c, stopId) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return From 740b654be6ac3e61fa0911ad05fecfc69733ba8c Mon Sep 17 00:00:00 2001 From: Paul Brinkmeier Date: Fri, 25 Apr 2025 13:37:01 +0200 Subject: [PATCH 3/3] Add bwegt efa client --- main.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/main.go b/main.go index 967b8e2..5e08305 100644 --- a/main.go +++ b/main.go @@ -111,6 +111,38 @@ func (c KVVEFAClient) BuildRequest(stopId string) (*http.Request, error) { return req, nil } +type BwegtEFAClient struct { +} + +func (c BwegtEFAClient) GetName() string { + return "bwegt" +} + +func (c BwegtEFAClient) BuildRequest(stopId string) (*http.Request, error) { + // Create request object + req, err := http.NewRequest("GET", "https://www.bwegt.de/bwegt-efa/XML_DM_REQUEST", nil) + if err != nil { + return nil, err + } + + // Configure our request + query := url.Values{} + query.Set("coordOutputFormat", "EPSG:4326") + query.Set("depType", "stopEvents") + query.Set("includeCompleteStopSeq", "0") + query.Set("limit", "10") + query.Set("locationServerActive", "0") + query.Set("mode", "direct") + query.Set("name_dm", stopId) + query.Set("outputFormat", "json") + query.Set("type_dm", "stop") + query.Set("useOnlyStops", "1") + query.Set("useRealtime", "1") + req.URL.RawQuery = query.Encode() + + return req, nil +} + func FetchDepartures(c EFAClient, stopId string) (DMResponse, error) { req, err := c.BuildRequest(stopId) if err != nil { @@ -206,6 +238,7 @@ func main() { // Use round-robin to send incoming requests to different servers var efaClient atomic.Uint64 efaClients := []EFAClient{ + BwegtEFAClient{}, VRNEFAClient{}, KVVEFAClient{}, }