Compare commits
2 Commits
26ea045216
...
b2c24df08a
Author | SHA1 | Date | |
---|---|---|---|
b2c24df08a | |||
e0a350725e |
26
src/main.rs
26
src/main.rs
@ -2,6 +2,7 @@ use axum::{
|
|||||||
extract::{
|
extract::{
|
||||||
State,
|
State,
|
||||||
Path,
|
Path,
|
||||||
|
Query,
|
||||||
},
|
},
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
response::Html,
|
response::Html,
|
||||||
@ -59,6 +60,10 @@ struct AppState {
|
|||||||
stations: HashMap<String, Station>,
|
stations: HashMap<String, Station>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct RoutingQuery {
|
||||||
|
all: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
@ -175,7 +180,8 @@ async fn route_station_overview(
|
|||||||
|
|
||||||
async fn route_station_to_dest_station(
|
async fn route_station_to_dest_station(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Path((station_id, dest_station_id)): Path<(String, String)>
|
Path((station_id, dest_station_id)): Path<(String, String)>,
|
||||||
|
query: Query<RoutingQuery>,
|
||||||
) -> Result<Html<String>, (StatusCode, String)> {
|
) -> Result<Html<String>, (StatusCode, String)> {
|
||||||
if !state.stations.contains_key(&station_id) {
|
if !state.stations.contains_key(&station_id) {
|
||||||
return Err((StatusCode::NOT_FOUND, String::from("Unknown departing station")));
|
return Err((StatusCode::NOT_FOUND, String::from("Unknown departing station")));
|
||||||
@ -198,6 +204,7 @@ async fn route_station_to_dest_station(
|
|||||||
|
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let stationdata_req = client.get(format!("https://bahn.expert/api/iris/v2/abfahrten/{}?lookahead=150&lookbehind=10", station_properties.code))
|
let stationdata_req = client.get(format!("https://bahn.expert/api/iris/v2/abfahrten/{}?lookahead=150&lookbehind=10", station_properties.code))
|
||||||
|
.header(reqwest::header::USER_AGENT, "https://nurausstieg.clerie.de - abuse: nurausstieg@clerie.de")
|
||||||
.send().await
|
.send().await
|
||||||
.map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, String::from("Station Data cannot be fetched")))?;
|
.map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, String::from("Station Data cannot be fetched")))?;
|
||||||
|
|
||||||
@ -215,9 +222,26 @@ async fn route_station_to_dest_station(
|
|||||||
|
|
||||||
out.push_str("<html><body>\n");
|
out.push_str("<html><body>\n");
|
||||||
out.push_str(&format!("<h1>Abfahren für {} nach {}</h1>\n", station_properties.name, dest_station_properties.name));
|
out.push_str(&format!("<h1>Abfahren für {} nach {}</h1>\n", station_properties.name, dest_station_properties.name));
|
||||||
|
|
||||||
|
let display_all_departures = match query.all {
|
||||||
|
Some(_) => true,
|
||||||
|
None => false,
|
||||||
|
};
|
||||||
|
|
||||||
|
if display_all_departures {
|
||||||
|
out.push_str("<a href=\"?\">Don't show all departures</a>\n");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
out.push_str("<a href=\"?all=1\">Show all departures</a>\n");
|
||||||
|
}
|
||||||
|
|
||||||
out.push_str("<ul>\n");
|
out.push_str("<ul>\n");
|
||||||
|
|
||||||
for departure in stationdata.departures {
|
for departure in stationdata.departures {
|
||||||
|
if !display_all_departures && !vec![String::from("ICE"), String::from("IC"), String::from("EC")].contains(&departure.train.r#type) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
let departure_info = match departure.departure {
|
let departure_info = match departure.departure {
|
||||||
Some(d) => d,
|
Some(d) => d,
|
||||||
None => continue,
|
None => continue,
|
||||||
|
Loading…
Reference in New Issue
Block a user