1
0
Fork 0
website/src/handlers/mod.rs

53 lines
1.2 KiB
Rust
Raw Normal View History

2023-03-25 16:14:53 +01:00
use hyper::StatusCode;
2022-08-31 23:25:17 +02:00
use std::sync::Arc;
2023-03-25 16:14:53 +01:00
use tracing::{instrument, log::*};
2022-08-31 23:20:59 +02:00
2022-08-31 23:25:17 +02:00
use axum::{
response::{Html, IntoResponse, Response},
Extension,
};
2022-08-31 23:20:59 +02:00
use crate::State;
2023-03-25 16:14:53 +01:00
#[derive(Debug)]
2022-08-31 23:20:59 +02:00
pub enum Error {
NotFound,
2023-03-25 16:14:53 +01:00
InternalError(anyhow::Error),
}
impl<E> From<E> for Error
where
E: Into<anyhow::Error>,
{
fn from(value: E) -> Self {
Error::InternalError(value.into())
}
2022-08-31 23:20:59 +02:00
}
pub type Result<T = Html<Vec<u8>>> = std::result::Result<T, Error>;
2023-03-25 16:14:53 +01:00
#[instrument(skip(state))]
2022-08-31 23:20:59 +02:00
pub async fn index(Extension(state): Extension<Arc<State>>) -> Result {
let ctx = tera::Context::new();
2022-08-31 23:25:17 +02:00
let res = state.tera.render("index.html", &ctx).map_err(|e| {
error!("Failed rendering index: {}", e);
Error::NotFound
})?;
2022-08-31 23:20:59 +02:00
Ok(Html(res.into()))
}
impl IntoResponse for Error {
fn into_response(self) -> Response {
match self {
2023-03-25 16:14:53 +01:00
Error::NotFound => {
info!("not found");
(StatusCode::NOT_FOUND, ()).into_response()
}
Error::InternalError(e) => {
error!("internal error: {e}");
(StatusCode::INTERNAL_SERVER_ERROR, ()).into_response()
}
2022-08-31 23:20:59 +02:00
}
}
}