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