Compare commits
2 Commits
22c963b037
...
f29752db77
Author | SHA1 | Date | |
---|---|---|---|
f29752db77 | |||
bf7eb1a411 |
27
src/main.rs
27
src/main.rs
@ -3,6 +3,7 @@ use axum::{
|
||||
State,
|
||||
Path,
|
||||
},
|
||||
http::StatusCode,
|
||||
response::Html,
|
||||
routing::get,
|
||||
Router,
|
||||
@ -131,7 +132,7 @@ async fn route_index(
|
||||
out.push_str("<html><body><ul>\n");
|
||||
|
||||
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");
|
||||
@ -142,9 +143,9 @@ async fn route_index(
|
||||
async fn route_station_overview(
|
||||
State(state): State<AppState>,
|
||||
Path(station): Path<String>
|
||||
) -> Result<Html<String>, String> {
|
||||
) -> Result<Html<String>, (StatusCode, String)> {
|
||||
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();
|
||||
@ -152,7 +153,7 @@ async fn route_station_overview(
|
||||
out.push_str("<html><body><ul>\n");
|
||||
|
||||
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");
|
||||
@ -163,40 +164,40 @@ async fn route_station_overview(
|
||||
async fn route_station_to_dest_station(
|
||||
State(state): State<AppState>,
|
||||
Path((station_id, dest_station_id)): Path<(String, String)>
|
||||
) -> Result<String, String> {
|
||||
) -> Result<String, (StatusCode, String)> {
|
||||
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) {
|
||||
return Err(String::from(""));
|
||||
return Err((StatusCode::NOT_FOUND, String::from("Unknown destination station")));
|
||||
}
|
||||
|
||||
let station_properties = match state.stations.get(&station_id) {
|
||||
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) {
|
||||
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 stationdata_req = client.get(format!("https://bahn.expert/api/iris/v2/abfahrten/{}?lookahead=150&lookbehind=10", station_properties.code))
|
||||
.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 {
|
||||
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
|
||||
.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())
|
||||
.map_err(|err| format!("{}", err))?;
|
||||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", err)))?;
|
||||
|
||||
let mut out = String::new();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user