Add templates for 404 pages
This commit is contained in:
parent
7bf18486bc
commit
e6616c75af
4 changed files with 364 additions and 7 deletions
34
src/main.rs
34
src/main.rs
|
|
@ -7,7 +7,7 @@ use std::{
|
|||
use axum::{
|
||||
extract::{Path, Query, State},
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Redirect},
|
||||
response::{Html, IntoResponse, Redirect},
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
|
|
@ -20,6 +20,7 @@ use openidconnect::{
|
|||
};
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use sqlx::{postgres::PgPoolOptions, PgPool};
|
||||
use tera::Tera;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use uuid::Uuid;
|
||||
|
||||
|
|
@ -262,12 +263,15 @@ impl OpenidConnector {
|
|||
struct AppState {
|
||||
db: PgPool,
|
||||
oidc: OpenidConnector,
|
||||
tera: Tera,
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
enum Error {
|
||||
#[error("An error occured in the database")]
|
||||
DbError(#[from] sqlx::Error),
|
||||
Db(#[from] sqlx::Error),
|
||||
#[error("An error occured when rendering a template")]
|
||||
Tera(#[from] tera::Error),
|
||||
#[error("An internal error occured")]
|
||||
InternalError,
|
||||
}
|
||||
|
|
@ -284,10 +288,14 @@ impl IntoResponse for Error {
|
|||
fn into_response(self) -> axum::response::Response {
|
||||
match self {
|
||||
Error::InternalError => InternalError.into_response(),
|
||||
Error::DbError(e) => {
|
||||
Error::Db(e) => {
|
||||
tracing::error!("Database error: {e:?}");
|
||||
InternalError.into_response()
|
||||
}
|
||||
Error::Tera(e) => {
|
||||
tracing::error!("Tera error: {e:?}");
|
||||
InternalError.into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -337,6 +345,21 @@ async fn redirected(
|
|||
}
|
||||
}
|
||||
|
||||
fn global_context() -> tera::Context {
|
||||
let mut ctx = tera::Context::new();
|
||||
|
||||
ctx.insert("title", "Mail accounts");
|
||||
|
||||
ctx
|
||||
}
|
||||
|
||||
async fn page_not_found(state: State<Arc<AppState>>) -> Result<(StatusCode, Html<String>), Error> {
|
||||
Ok((
|
||||
StatusCode::NOT_FOUND,
|
||||
Html(state.tera.render("not_found.html", &global_context())?),
|
||||
))
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> color_eyre::Result<()> {
|
||||
color_eyre::install()?;
|
||||
|
|
@ -361,12 +384,15 @@ async fn main() -> color_eyre::Result<()> {
|
|||
|
||||
sqlx::migrate!().run(&db).await?;
|
||||
|
||||
let tera = Tera::new("templates/*.html")?;
|
||||
|
||||
tracing::info!("Listening on {addr}");
|
||||
|
||||
let router = Router::new()
|
||||
.route("/login", get(login))
|
||||
.route("/login/redirect/:id", get(redirected))
|
||||
.with_state(Arc::new(AppState { db, oidc }));
|
||||
.fallback(page_not_found)
|
||||
.with_state(Arc::new(AppState { db, oidc, tera }));
|
||||
|
||||
Ok(axum::Server::bind(&addr)
|
||||
.serve(router.into_make_service())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue