feat(frontend): add 404 page and support for static pages

This commit is contained in:
Mariano Riefolo 2024-08-12 17:27:17 +02:00
parent 44f0b33fe5
commit 6889377f07
6 changed files with 51 additions and 0 deletions

1
Cargo.lock generated
View File

@ -1002,6 +1002,7 @@ dependencies = [
"axum",
"chrono",
"dotenvy",
"http",
"jsonwebtoken",
"rand",
"rusqlite",

View File

@ -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"

5
src/assets/style.css Normal file
View File

@ -0,0 +1,5 @@
@import url('https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css');
.center {
text-align: center;
}

19
src/error_pages/404.html Normal file
View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>404 Not found</title>
<link rel="stylesheet" href="/assets/style.css">
</head>
<body class="center">
<header>
<h1>404</h1>
<h2>Page not found</h2>
</header>
<main>
<p>Sorry, the page you are looking for does not exist.</p>
<p>If you arrived here via a link, please open an issue on the repository.</p>
</main>
</body>
</html>

23
src/frontend/mod.rs Normal file
View File

@ -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()))
}
}

View File

@ -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")