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

36 lines
999 B
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
2023-03-25 21:38:16 +01:00
use crate::{State, WebsiteError};
2022-08-31 23:20:59 +02:00
2023-03-25 16:14:53 +01:00
#[instrument(skip(state))]
2023-03-25 21:38:16 +01:00
pub async fn index(Extension(state): Extension<Arc<State>>) -> std::result::Result<Html<Vec<u8>>, WebsiteError> {
2022-08-31 23:20:59 +02:00
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);
2023-03-25 21:38:16 +01:00
WebsiteError::NotFound
2022-08-31 23:25:17 +02:00
})?;
2022-08-31 23:20:59 +02:00
Ok(Html(res.into()))
}
2023-03-25 21:38:16 +01:00
impl IntoResponse for WebsiteError {
2022-08-31 23:20:59 +02:00
fn into_response(self) -> Response {
match self {
2023-03-25 21:38:16 +01:00
WebsiteError::NotFound => {
2023-03-25 16:14:53 +01:00
info!("not found");
(StatusCode::NOT_FOUND, ()).into_response()
}
2023-03-25 21:38:16 +01:00
WebsiteError::InternalError(e) => {
2023-03-25 16:14:53 +01:00
error!("internal error: {e}");
(StatusCode::INTERNAL_SERVER_ERROR, ()).into_response()
}
2022-08-31 23:20:59 +02:00
}
}
}