2022-08-31 23:25:17 +02:00
|
|
|
use axum::{
|
|
|
|
response::{Html, IntoResponse, Response},
|
|
|
|
Extension,
|
|
|
|
};
|
2023-03-25 22:12:49 +01:00
|
|
|
use hyper::StatusCode;
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use prometheus::{opts, IntCounterVec};
|
|
|
|
use std::sync::Arc;
|
|
|
|
use tracing::{instrument, log::*};
|
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 22:12:49 +01:00
|
|
|
pub mod posts;
|
|
|
|
|
|
|
|
lazy_static! {
|
2023-03-26 12:01:59 +02:00
|
|
|
pub static ref HIT_COUNTER: IntCounterVec = prometheus::register_int_counter_vec!(
|
|
|
|
opts!("page_hits", "Number of hits to various pages"),
|
|
|
|
&["page"]
|
|
|
|
)
|
|
|
|
.unwrap();
|
2023-03-25 22:12:49 +01:00
|
|
|
}
|
|
|
|
|
2023-03-25 16:14:53 +01:00
|
|
|
#[instrument(skip(state))]
|
2023-03-25 22:12:49 +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
|
|
|
})?;
|
2023-03-26 00:30:21 +01:00
|
|
|
HIT_COUNTER.with_label_values(&["/"]).inc();
|
2022-08-31 23:20:59 +02:00
|
|
|
Ok(Html(res.into()))
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:40:25 +02:00
|
|
|
pub async fn not_found() -> Response {
|
|
|
|
(StatusCode::NOT_FOUND, ()).into_response()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|