🐛 Add 404 page for missing file instead of gateway timeout

main
_ 2020-10-29 12:22:42 +00:00
parent ec5b770ae3
commit 9dd2155d7a
1 changed files with 37 additions and 9 deletions

View File

@ -149,6 +149,30 @@ async fn serve_file (
}
}
async fn serve_error (
client: Arc <Client>,
req_id: String,
status_code: ptth::http_serde::StatusCode
) {
let headers: HashMap <String, Vec <u8>> = Default::default ();
let resp_parts = http_serde::ResponseParts {
status_code,
headers,
};
let resp_req = client
.post (&format! ("http://127.0.0.1:4000/http_response/{}", req_id))
.header ("X-PTTH-2LJYXWC4", base64::encode (rmp_serde::to_vec (&resp_parts).unwrap ()))
.body ("404 Not Found")
;
//println! ("Step 6");
if let Err (e) = resp_req.send ().await {
println! ("Err: {:?}", e);
}
}
#[tokio::main]
async fn main () -> Result <(), Box <dyn Error>> {
let client = Arc::new (Client::new ());
@ -221,16 +245,20 @@ async fn main () -> Result <(), Box <dyn Error>> {
tokio::spawn (async move {
let mut path = PathBuf::from ("/home/user");
path.push (&uri [1..]);
let f = File::open (path).await.unwrap ();
serve_file (
client,
f,
req_id,
should_send_body,
range_start,
range_end
).await;
match File::open (path).await {
Ok (f) => serve_file (
client,
f,
req_id,
should_send_body,
range_start,
range_end
).await,
Err (_) => {
serve_error (client, req_id, ptth::http_serde::StatusCode::NotFound).await;
},
}
});
}
}