diff options
author | Joris | 2025-02-13 22:38:47 +0100 |
---|---|---|
committer | Joris | 2025-02-13 22:42:05 +0100 |
commit | ede6d7a5d89f3a6c33dbf17e0ba86dd36372cdf6 (patch) | |
tree | d968dd599f2ebf076ac323683d14ab7b57c082b5 | |
parent | 5e2aee9248a00c8b213a8e07e4796d668bff519c (diff) |
Set text/html content type when returning error pages
-rw-r--r-- | src/routes.rs | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/src/routes.rs b/src/routes.rs index 15c255e..3b3fc9a 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -27,10 +27,7 @@ pub async fn routes( let files_dir = Path::new(&files_dir); match (request.method(), path) { - (&Method::GET, [""]) => Ok(with_headers( - response(StatusCode::OK, templates::INDEX), - vec![(CONTENT_TYPE, "text/html")], - )), + (&Method::GET, [""]) => Ok(html(templates::INDEX)), (&Method::GET, ["static", "main.js"]) => Ok(static_file( include_str!("static/main.js"), "application/javascript", @@ -128,10 +125,9 @@ async fn get( ) -> Result<Response<BoxBody<Bytes, std::io::Error>>> { let file = db::files::get(&db_conn, file_id).await; match (get_file, file) { - (GetFile::ShowPage, Ok(Some(file))) => Ok(with_headers( - response(StatusCode::OK, templates::file_page(file)), - vec![(CONTENT_TYPE, "text/html")], - )), + (GetFile::ShowPage, Ok(Some(file))) => { + Ok(html(templates::file_page(file))) + }, (GetFile::Download, Ok(Some(file))) => { let path = files_dir.join(file_id); Ok(stream_file(path, file).await) @@ -199,19 +195,27 @@ async fn stream_file(path: PathBuf, file: model::File) -> Response<BoxBody<Bytes } } +fn html(text: impl Into<String>) -> Response<BoxBody<Bytes, std::io::Error>> { + html_response(response(StatusCode::OK, text)) +} + +fn html_response(response: Response<BoxBody<Bytes, std::io::Error>>) -> Response<BoxBody<Bytes, std::io::Error>> { + with_headers(response, vec![(CONTENT_TYPE, "text/html")]) +} + fn not_found() -> Response<BoxBody<Bytes, std::io::Error>> { - response(StatusCode::NOT_FOUND, templates::NOT_FOUND) + html_response(response(StatusCode::NOT_FOUND, templates::NOT_FOUND)) } fn bad_request() -> Response<BoxBody<Bytes, std::io::Error>> { - response(StatusCode::BAD_REQUEST, templates::BAD_REQUEST) + html_response(response(StatusCode::BAD_REQUEST, templates::BAD_REQUEST)) } fn internal_server_error() -> Response<BoxBody<Bytes, std::io::Error>> { - response( + html_response(response( StatusCode::INTERNAL_SERVER_ERROR, templates::INTERNAL_SERVER_ERROR, - ) + )) } pub fn with_headers( |