Umgebungsvariable für die Dauer und zusätzliche Variablen für Ferry, Tram und Taxi hinzuzufügt

This commit is contained in:
Simon Rieger 2024-09-28 13:39:46 +02:00
parent 6811f87281
commit 248f280e22
2 changed files with 25 additions and 4 deletions

View file

@ -13,8 +13,12 @@ services:
- DB_NAME=traindb
- DB_DSN=root:password@tcp(mariadb:3306)/traindb
- API_BASE_URL=http://db-rest:3000
- MAX_RESULTS=100
- MAX_RESULTS=200
- DURATION=240
- BUS=false
- FERRY=false
- TRAM=false
- TAXI=false
# Hildesheim HBF, Braunschweig HBF, Hannover HBF
- STATION_IDS=8000169,8000049,8000152
restart: always

23
main.go
View file

@ -43,10 +43,26 @@ func main() {
if err != nil {
log.Fatalf("Ungültiger Wert für MAX_RESULTS: %v", err)
}
duration, err := strconv.Atoi(os.Getenv("DURATION"))
if err != nil {
log.Fatalf("Ungültiger Wert für DURATION: %v", err)
}
includeBus, err := strconv.ParseBool(os.Getenv("BUS"))
if err != nil {
log.Fatalf("Ungültiger Wert für BUS: %v", err)
}
includeFerry, err := strconv.ParseBool(os.Getenv("FERRY"))
if err != nil {
log.Fatalf("Ungültiger Wert für FERRY: %v", err)
}
includeTram, err := strconv.ParseBool(os.Getenv("TRAM"))
if err != nil {
log.Fatalf("Ungültiger Wert für TRAM: %v", err)
}
includeTaxi, err := strconv.ParseBool(os.Getenv("TAXI"))
if err != nil {
log.Fatalf("Ungültiger Wert für TAXI: %v", err)
}
stationIDs := strings.Split(os.Getenv("STATION_IDS"), ",")
db, err := sql.Open("mysql", dbDSN)
@ -57,7 +73,7 @@ func main() {
for {
for _, stationID := range stationIDs {
departures := fetchDepartures(apiBaseURL, stationID, maxResults, includeBus)
departures := fetchDepartures(apiBaseURL, stationID, maxResults, duration, includeBus, includeFerry, includeTram, includeTaxi)
for _, dep := range departures {
savePosition(db, dep)
}
@ -66,8 +82,9 @@ func main() {
}
}
func fetchDepartures(apiBaseURL, stationID string, maxResults int, includeBus bool) []Departure {
url := fmt.Sprintf("%s/stops/%s/departures?results=%d&bus=%t", apiBaseURL, stationID, maxResults, includeBus)
func fetchDepartures(apiBaseURL, stationID string, maxResults, duration int, includeBus, includeFerry, includeTram, includeTaxi bool) []Departure {
url := fmt.Sprintf("%s/stops/%s/departures?results=%d&duration=%d&bus=%t&ferry=%t&tram=%t&taxi=%t",
apiBaseURL, stationID, maxResults, duration, includeBus, includeFerry, includeTram, includeTaxi)
resp, err := http.Get(url)
if err != nil {
log.Printf("Fehler beim Abrufen der Abfahrten für Station %s: %v\n", stationID, err)