forked from matteo/serves3
Correct logic to detect 404 errors from S3
This commit is contained in:
parent
74a7ab55f5
commit
221eb9dbc6
29
src/main.rs
29
src/main.rs
|
@ -59,6 +59,8 @@ lazy_static! {
|
||||||
s3::bucket::Bucket::new(&SETTINGS.bucket_name, region, credentials)
|
s3::bucket::Bucket::new(&SETTINGS.bucket_name, region, credentials)
|
||||||
.expect("Cannot find or authenticate to S3 bucket")
|
.expect("Cannot find or authenticate to S3 bucket")
|
||||||
};
|
};
|
||||||
|
static ref FILEVIEW_TEMPLATE: &'static str =
|
||||||
|
{ std::include_str!("../templates/index.html.tera") };
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Responder)]
|
#[derive(Responder)]
|
||||||
|
@ -74,6 +76,9 @@ enum FileView {
|
||||||
enum Error {
|
enum Error {
|
||||||
#[response(status = 404)]
|
#[response(status = 404)]
|
||||||
NotFound(String),
|
NotFound(String),
|
||||||
|
|
||||||
|
#[response(status = 500)]
|
||||||
|
UnknownError(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::get("/<path..>")]
|
#[rocket::get("/<path..>")]
|
||||||
|
@ -113,14 +118,19 @@ async fn s3_serve_file(path: &PathBuf) -> Result<FileView, Error> {
|
||||||
|
|
||||||
// FIXME: this can be big, we should use streaming,
|
// FIXME: this can be big, we should use streaming,
|
||||||
// not loading in memory!
|
// not loading in memory!
|
||||||
BUCKET
|
let response = BUCKET
|
||||||
.get_object(format!("{}", path.display()))
|
.get_object(format!("{}", path.display()))
|
||||||
.await
|
.await
|
||||||
.map(|contents| {
|
.map_err(|_| Error::UnknownError("Unable to connect to S3 bucket".into()))?;
|
||||||
let bytes = contents.bytes().to_vec();
|
|
||||||
FileView::File(bytes)
|
match response.status_code() {
|
||||||
})
|
200 | 204 => {
|
||||||
.map_err(|_| Error::NotFound("Object not found".into()))
|
let bytes = response.bytes().to_vec();
|
||||||
|
Ok(FileView::File(bytes))
|
||||||
|
}
|
||||||
|
404 => Err(Error::NotFound("Object not found".into())),
|
||||||
|
_ => Err(Error::UnknownError("Unknown S3 error".into())),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn s3_fileview(path: &PathBuf) -> Result<Vec<String>, Error> {
|
async fn s3_fileview(path: &PathBuf) -> Result<Vec<String>, Error> {
|
||||||
|
@ -175,5 +185,10 @@ fn rocket() -> _ {
|
||||||
eprintln!("Proxying to {} for {}", BUCKET.host(), BUCKET.name());
|
eprintln!("Proxying to {} for {}", BUCKET.host(), BUCKET.name());
|
||||||
rocket::build()
|
rocket::build()
|
||||||
.mount("/", rocket::routes![index])
|
.mount("/", rocket::routes![index])
|
||||||
.attach(Template::fairing())
|
.attach(Template::custom(|engines| {
|
||||||
|
engines
|
||||||
|
.tera
|
||||||
|
.add_raw_template("index", *FILEVIEW_TEMPLATE)
|
||||||
|
.unwrap()
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue