Compare commits

..

No commits in common. "f29752db77178a32bc61e32ac607c5ebb11c61bc" and "22c963b0374bf756a664b9a54bc1e12c989af4f2" have entirely different histories.

View File

@ -3,7 +3,6 @@ use axum::{
State, State,
Path, Path,
}, },
http::StatusCode,
response::Html, response::Html,
routing::get, routing::get,
Router, Router,
@ -132,7 +131,7 @@ async fn route_index(
out.push_str("<html><body><ul>\n"); out.push_str("<html><body><ul>\n");
for (id, station) in state.stations.into_iter() { for (id, station) in state.stations.into_iter() {
out.push_str(&format!("<li><a href=\"/station/{}\">{}</a></li>\n", id, station.name)); out.push_str(&format!("<li><a href=\"/station/{}\">{}</a><li>\n", id, station.name));
} }
out.push_str("</ul></body></html>\n"); out.push_str("</ul></body></html>\n");
@ -143,9 +142,9 @@ async fn route_index(
async fn route_station_overview( async fn route_station_overview(
State(state): State<AppState>, State(state): State<AppState>,
Path(station): Path<String> Path(station): Path<String>
) -> Result<Html<String>, (StatusCode, String)> { ) -> Result<Html<String>, String> {
if !state.stations.contains_key(&station) { if !state.stations.contains_key(&station) {
return Err((StatusCode::NOT_FOUND, String::from("Unknown departing station"))); return Err(String::from(""));
} }
let mut out = String::new(); let mut out = String::new();
@ -153,7 +152,7 @@ async fn route_station_overview(
out.push_str("<html><body><ul>\n"); out.push_str("<html><body><ul>\n");
for (dest_station_id, dest_station) in state.stations.into_iter() { for (dest_station_id, dest_station) in state.stations.into_iter() {
out.push_str(&format!("<li><a href=\"/station/{}/to/{}\">{}</a></li>\n", station, dest_station_id, dest_station.name)); out.push_str(&format!("<li><a href=\"/station/{}/to/{}\">{}</a><li>\n", station, dest_station_id, dest_station.name));
} }
out.push_str("<html><body><ul>\n"); out.push_str("<html><body><ul>\n");
@ -164,40 +163,40 @@ 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)>
) -> Result<String, (StatusCode, String)> { ) -> Result<String, 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(String::from(""));
} }
if !state.stations.contains_key(&dest_station_id) { if !state.stations.contains_key(&dest_station_id) {
return Err((StatusCode::NOT_FOUND, String::from("Unknown destination station"))); return Err(String::from(""));
} }
let station_properties = match state.stations.get(&station_id) { let station_properties = match state.stations.get(&station_id) {
Some(v) => v, Some(v) => v,
None => return Err((StatusCode::INTERNAL_SERVER_ERROR, String::from("Station properties for departing station missing"))), None => return Err(String::from("")),
}; };
let dest_station_properties = match state.stations.get(&dest_station_id) { let dest_station_properties = match state.stations.get(&dest_station_id) {
Some(v) => v, Some(v) => v,
None => return Err((StatusCode::INTERNAL_SERVER_ERROR, String::from("Station properties for destination station missing"))), None => return Err(String::from("")),
}; };
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))
.send().await .send().await
.map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, String::from("Station Data cannot be fetched")))?; .map_err(|_err| String::from("Station Data cannot be fetched"))?;
if stationdata_req.status() != reqwest::StatusCode::OK { if stationdata_req.status() != reqwest::StatusCode::OK {
return Err((StatusCode::INTERNAL_SERVER_ERROR, String::from("Station Data returned an unexpected response"))); return Err(String::from("Station Data returned an unexpected response"));
} }
let stationdata_body = stationdata_req.text().await let stationdata_body = stationdata_req.text().await
.map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, String::from("Station Data cannot be read")))?; .map_err(|_err| "Station Data cannot be read")?;
let stationdata: Departures = serde_json::from_str(stationdata_body.as_str()) let stationdata: Departures = serde_json::from_str(stationdata_body.as_str())
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", err)))?; .map_err(|err| format!("{}", err))?;
let mut out = String::new(); let mut out = String::new();