aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/routes.rs28
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(