1
0
Fork 0
website/src/main.rs

90 lines
2.1 KiB
Rust
Raw Normal View History

2023-03-25 16:14:53 +01:00
use std::{collections::HashMap, sync::Arc};
2022-09-05 00:05:24 +02:00
2023-03-26 12:01:59 +02:00
use axum::{body, response::Response, routing::get, Extension, Router};
use color_eyre::eyre::{Error, Result};
2023-03-26 00:30:21 +01:00
use hyper::header::CONTENT_TYPE;
2023-03-25 12:23:11 +01:00
use post::Post;
2023-03-26 12:01:59 +02:00
use prometheus::{Encoder, TextEncoder};
2022-08-31 23:20:59 +02:00
use tera::Tera;
2023-03-25 16:14:53 +01:00
use tower_http::{compression::CompressionLayer, trace::TraceLayer};
use tracing::{instrument, log::*};
2022-06-16 23:44:37 +02:00
2022-08-31 23:20:59 +02:00
mod handlers;
2023-03-25 12:23:11 +01:00
mod post;
2022-08-31 23:20:59 +02:00
pub struct State {
2023-03-25 16:14:53 +01:00
posts: HashMap<String, Post>,
2022-08-31 23:20:59 +02:00
tera: Tera,
}
2022-06-16 23:44:37 +02:00
#[tokio::main]
async fn main() -> Result<()> {
color_eyre::install()?;
tracing_subscriber::fmt::init();
info!("Starting server...");
2023-03-25 16:14:53 +01:00
let app = init_app().await?;
2022-09-05 00:02:58 +02:00
2023-03-25 16:14:53 +01:00
info!("Now listening at http://localhost:8180");
axum::Server::bind(&"0.0.0.0:8180".parse().unwrap())
.serve(app.into_make_service())
.await?;
Ok(())
}
#[instrument]
pub async fn init_app() -> Result<Router> {
2023-03-25 12:23:11 +01:00
let tera = Tera::new("templates/**/*")?;
2023-03-25 16:14:53 +01:00
let posts = post::load_all().await?;
2022-08-31 23:20:59 +02:00
let state = Arc::new(State { tera, posts });
2022-06-16 23:44:37 +02:00
2022-08-31 23:20:59 +02:00
let middleware = tower::ServiceBuilder::new()
.layer(TraceLayer::new_for_http())
2023-03-25 16:14:53 +01:00
.layer(Extension(state))
.layer(CompressionLayer::new());
2022-08-31 23:20:59 +02:00
2022-08-31 23:25:17 +02:00
let app = Router::new()
2022-08-31 23:20:59 +02:00
.route("/", get(handlers::index))
2023-03-25 22:12:49 +01:00
.nest("/posts", handlers::posts::router())
2023-03-22 22:39:18 +01:00
.nest_service("/static", tower_http::services::ServeDir::new("./static"))
2023-03-26 12:40:25 +02:00
.route("/healthcheck", get(healthcheck))
2023-03-26 00:30:21 +01:00
.route("/metrics", get(metrics))
2022-08-31 23:25:17 +02:00
.layer(middleware);
2022-06-16 23:44:37 +02:00
2023-03-25 16:14:53 +01:00
Ok(app)
2022-06-16 23:44:37 +02:00
}
2023-03-25 21:38:16 +01:00
async fn healthcheck() -> &'static str {
"OK"
}
2023-03-26 00:30:21 +01:00
async fn metrics() -> Response {
let encoder = TextEncoder::new();
let metric_families = prometheus::gather();
let mut buffer = vec![];
encoder.encode(&metric_families, &mut buffer).unwrap();
Response::builder()
.status(200)
.header(CONTENT_TYPE, encoder.format_type())
.body(body::boxed(body::Full::from(buffer)))
.unwrap()
}
2023-03-25 21:38:16 +01:00
#[derive(Debug)]
pub enum WebsiteError {
NotFound,
2023-03-26 12:40:25 +02:00
InternalError(Error),
2023-03-25 21:38:16 +01:00
}
impl<E> From<E> for WebsiteError
where
2023-03-26 12:40:25 +02:00
E: Into<Error>,
2023-03-25 21:38:16 +01:00
{
fn from(value: E) -> Self {
WebsiteError::InternalError(value.into())
}
}