From 6889377f07e840f93ddb75bfc9f0200c4e888040 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Mon, 12 Aug 2024 17:27:17 +0200 Subject: [PATCH] feat(frontend): add 404 page and support for static pages --- Cargo.lock | 1 + Cargo.toml | 1 + src/assets/style.css | 5 +++++ src/error_pages/404.html | 19 +++++++++++++++++++ src/frontend/mod.rs | 23 +++++++++++++++++++++++ src/main.rs | 2 ++ 6 files changed, 51 insertions(+) create mode 100644 src/assets/style.css create mode 100644 src/error_pages/404.html create mode 100644 src/frontend/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 3d06d57..f5a5ff6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1002,6 +1002,7 @@ dependencies = [ "axum", "chrono", "dotenvy", + "http", "jsonwebtoken", "rand", "rusqlite", diff --git a/Cargo.toml b/Cargo.toml index 2dd145a..4c1090b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ argon2 = { version = "0.5.3", features = ["password-hash", "rand"] } axum = "0.7.5" chrono = "0.4.38" dotenvy = "0.15.7" +http = "1.1.0" jsonwebtoken = "9.3.0" rand = "0.8.5" rusqlite = "0.32.1" diff --git a/src/assets/style.css b/src/assets/style.css new file mode 100644 index 0000000..1a34c45 --- /dev/null +++ b/src/assets/style.css @@ -0,0 +1,5 @@ +@import url('https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css'); + +.center { + text-align: center; +} diff --git a/src/error_pages/404.html b/src/error_pages/404.html new file mode 100644 index 0000000..57880ca --- /dev/null +++ b/src/error_pages/404.html @@ -0,0 +1,19 @@ + + + + + + 404 Not found + + + +
+

404

+

Page not found

+
+
+

Sorry, the page you are looking for does not exist.

+

If you arrived here via a link, please open an issue on the repository.

+
+ + diff --git a/src/frontend/mod.rs b/src/frontend/mod.rs new file mode 100644 index 0000000..c1cf6cd --- /dev/null +++ b/src/frontend/mod.rs @@ -0,0 +1,23 @@ +use axum::handler::HandlerWithoutStateExt; +use axum::response::{Html, IntoResponse}; +use axum::Router; +use http::StatusCode; +use tower_http::services::ServeDir; + +pub fn get_routes() -> Router { + Router::new() + .nest_service( + "/", + ServeDir::new("src/static_pages").not_found_service(handler.into_service()), + ) + .nest_service("/assets", ServeDir::new("src/assets")) +} + +pub async fn handler() -> impl IntoResponse { + let path = "src/error_pages/404.html"; + if let Ok(content) = tokio::fs::read_to_string(path).await { + (StatusCode::NOT_FOUND, Html(content)) + } else { + (StatusCode::NOT_FOUND, Html("404 Not Found".to_string())) + } +} diff --git a/src/main.rs b/src/main.rs index 27b2fe8..756f3ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ use tower_http::services::ServeDir; mod api; mod db; +mod frontend; #[tokio::main] async fn main() { @@ -17,6 +18,7 @@ async fn main() { let app = Router::new() .merge(api::get_routes()) + .merge(frontend::get_routes()) .nest_service("/scripts", ServeDir::new("scripts")); let port = std::env::var("PORT")