78 lines
2.0 KiB
Go
78 lines
2.0 KiB
Go
package main
|
|
|
|
import "encoding/json"
|
|
import "fmt"
|
|
import "log"
|
|
import "net/http"
|
|
import "os"
|
|
import "slices"
|
|
import "strings"
|
|
import "sync/atomic"
|
|
|
|
func main() {
|
|
password := os.Getenv("VRNP_PASSWORD")
|
|
if len(password) == 0 {
|
|
log.Fatal("Required environment variable VRNP_PASSWORD is not set")
|
|
}
|
|
|
|
clientAllowlist := strings.Split(os.Getenv("VRNP_CLIENT_ALLOWLIST"), ",")
|
|
|
|
// Use round-robin to send incoming requests to different servers
|
|
var efaClient atomic.Uint64
|
|
var efaClients []EFAClient
|
|
for _, efaClient := range allEfaClients {
|
|
if len(clientAllowlist) != 0 && slices.Contains(clientAllowlist, efaClient.GetName()) {
|
|
efaClients = append(efaClients, efaClient)
|
|
}
|
|
}
|
|
|
|
http.HandleFunc("/departures", func(w http.ResponseWriter, r *http.Request) {
|
|
user, pass, ok := r.BasicAuth()
|
|
if !(ok && user == "admin" && pass == password) {
|
|
w.Header().Set("WWW-Authenticate", "Basic realm=\"Access to departure API\"")
|
|
http.Error(w, "You shall not pass", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
query := r.URL.Query()
|
|
|
|
if query["stop_id"] == nil || len(query["stop_id"]) < 1 {
|
|
http.Error(w, "Missing query parameter: stop_id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
stopId := query["stop_id"][0]
|
|
|
|
/*
|
|
var platform *string = nil
|
|
if query["platform"] != nil && len(query["platform"]) != 0 {
|
|
platform = &query["platform"][0]
|
|
}
|
|
*/
|
|
|
|
c := efaClients[(efaClient.Add(1)-1)%uint64(len(efaClients))]
|
|
departures, err := FetchDepartures(c, stopId)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
// TODO: Filter for platform here
|
|
|
|
// Does not handle multiple media types
|
|
if r.Header.Get("Accept") == "application/json" {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(departures)
|
|
} else {
|
|
// plain text
|
|
for _, d := range departures.Departures {
|
|
fmt.Fprintf(w, "%2s %-11s %6s\n",
|
|
d.Symbol[:min(2, len(d.Symbol))],
|
|
d.Direction[:min(11, len(d.Direction))],
|
|
d.Leaving,
|
|
)
|
|
}
|
|
}
|
|
return
|
|
})
|
|
log.Fatal(http.ListenAndServe(":8000", nil))
|
|
}
|