Compare commits

..

2 Commits

Author SHA1 Message Date
f29752db77 Proper http error response codes 2024-07-27 21:42:40 +02:00
bf7eb1a411 Fix list syntax 2024-07-27 21:26:24 +02:00

View File

@ -3,6 +3,7 @@ use axum::{
State, State,
Path, Path,
}, },
http::StatusCode,
response::Html, response::Html,
routing::get, routing::get,
Router, Router,
@ -131,7 +132,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");
@ -142,9 +143,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>, String> { ) -> Result<Html<String>, (StatusCode, String)> {
if !state.stations.contains_key(&station) { if !state.stations.contains_key(&station) {
return Err(String::from("")); return Err((StatusCode::NOT_FOUND, String::from("Unknown departing station")));
} }
let mut out = String::new(); let mut out = String::new();
@ -152,7 +153,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");
@ -163,40 +164,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, String> { ) -> Result<String, (StatusCode, String)> {
if !state.stations.contains_key(&station_id) { if !state.stations.contains_key(&station_id) {
return Err(String::from("")); return Err((StatusCode::NOT_FOUND, String::from("Unknown departing station")));
} }
if !state.stations.contains_key(&dest_station_id) { if !state.stations.contains_key(&dest_station_id) {
return Err(String::from("")); return Err((StatusCode::NOT_FOUND, String::from("Unknown destination station")));
} }
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(String::from("")), None => return Err((StatusCode::INTERNAL_SERVER_ERROR, String::from("Station properties for departing station missing"))),
}; };
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(String::from("")), None => return Err((StatusCode::INTERNAL_SERVER_ERROR, String::from("Station properties for destination station missing"))),
}; };
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| String::from("Station Data cannot be fetched"))?; .map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, String::from("Station Data cannot be fetched")))?;
if stationdata_req.status() != reqwest::StatusCode::OK { if stationdata_req.status() != reqwest::StatusCode::OK {
return Err(String::from("Station Data returned an unexpected response")); return Err((StatusCode::INTERNAL_SERVER_ERROR, 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| "Station Data cannot be read")?; .map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, String::from("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| format!("{}", err))?; .map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", err)))?;
let mut out = String::new(); let mut out = String::new();