Compare commits
	
		
			No commits in common. "d23be3062a79c3639c1b468d0885bfba7f4426f7" and "fad4da3f6b6bffecf6fa545966d3c77f5630089b" have entirely different histories.
		
	
	
		
			d23be3062a
			...
			fad4da3f6b
		
	
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @ -1,4 +1,3 @@ | |||||||
| result | result | ||||||
| vrnp | vrnp | ||||||
| main |  | ||||||
| *.swp | *.swp | ||||||
|  | |||||||
							
								
								
									
										127
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										127
									
								
								main.go
									
									
									
									
									
								
							| @ -8,9 +8,7 @@ import "net/http" | |||||||
| import "net/url" | import "net/url" | ||||||
| import "os" | import "os" | ||||||
| import "slices" | import "slices" | ||||||
| import "strings" |  | ||||||
| import "strconv" | import "strconv" | ||||||
| import "sync/atomic" |  | ||||||
| import "time" | import "time" | ||||||
| 
 | 
 | ||||||
| // JSON unmarshaling types for departure monitor API | // JSON unmarshaling types for departure monitor API | ||||||
| @ -45,110 +43,28 @@ type DMDateTime struct { | |||||||
| 	Minute string `json:"minute"` | 	Minute string `json:"minute"` | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type EFAClient interface { | func FetchDepartures(stopId string) (DMResponse, error) { | ||||||
| 	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 | 	// 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) | ||||||
| 	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 |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| 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 |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| 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 { | 	if err != nil { | ||||||
| 		return DMResponse{}, err | 		return DMResponse{}, 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() | ||||||
|  | 
 | ||||||
| 	// Send the request, wait max 10 seconds | 	// Send the request, wait max 10 seconds | ||||||
| 	client := http.Client{ | 	client := http.Client{ | ||||||
| 		Timeout: 10 * time.Second, | 		Timeout: 10 * time.Second, | ||||||
| @ -235,14 +151,6 @@ func main() { | |||||||
| 		panic("Required environment variable VRNP_PASSWORD is not set") | 		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{ |  | ||||||
| 		BwegtEFAClient{}, |  | ||||||
| 		VRNEFAClient{}, |  | ||||||
| 		KVVEFAClient{}, |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	http.HandleFunc("/departures", func(w http.ResponseWriter, r *http.Request) { | 	http.HandleFunc("/departures", func(w http.ResponseWriter, r *http.Request) { | ||||||
| 		user, pass, ok := r.BasicAuth() | 		user, pass, ok := r.BasicAuth() | ||||||
| 		if !(ok && user == "admin" && pass == password) { | 		if !(ok && user == "admin" && pass == password) { | ||||||
| @ -264,8 +172,7 @@ func main() { | |||||||
| 			platform = &query["platform"][0] | 			platform = &query["platform"][0] | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		c := efaClients[(efaClient.Add(1) - 1) % uint64(len(efaClients))] | 		ds, err := FetchDepartures(stopId) | ||||||
| 		ds, err := FetchDepartures(c, stopId) |  | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			http.Error(w, err.Error(), http.StatusInternalServerError) | 			http.Error(w, err.Error(), http.StatusInternalServerError) | ||||||
| 			return | 			return | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user