use axum::{
response::{Html, IntoResponse, Response},
Extension,
};
use hyper::StatusCode;
use lazy_static::lazy_static;
use prometheus::{opts, IntCounterVec};
use std::sync::Arc;
use tracing::{instrument, log::*};
use crate::{State, WebsiteError};
pub mod posts;
lazy_static! {
pub static ref HIT_COUNTER: IntCounterVec =
prometheus::register_int_counter_vec!(opts!("page_hits", "Number of hits to various pages"), &["page"]).unwrap();
}
#[instrument(skip(state))]
pub async fn index(
Extension(state): Extension>,
) -> std::result::Result>, WebsiteError> {
let ctx = tera::Context::new();
let res = state.tera.render("index.html", &ctx).map_err(|e| {
error!("Failed rendering index: {}", e);
WebsiteError::NotFound
})?;
HIT_COUNTER.with_label_values(&["/"]).inc();
Ok(Html(res.into()))
}
impl IntoResponse for WebsiteError {
fn into_response(self) -> Response {
match self {
WebsiteError::NotFound => {
info!("not found");
(StatusCode::NOT_FOUND, ()).into_response()
}
WebsiteError::InternalError(e) => {
error!("internal error: {e}");
(StatusCode::INTERNAL_SERVER_ERROR, ()).into_response()
}
}
}
}