use std::{path::PathBuf, sync::Arc}; use axum::{routing::get, Extension, Router}; use color_eyre::eyre::Result; use glob::glob; use serde_derive::Serialize; use tera::Tera; use tower_http::trace::TraceLayer; use tracing::log::*; mod handlers; pub struct State { posts: Vec, tera: Tera, } #[derive(Serialize)] pub struct Post { pub name: String, pub content: String, } #[tokio::main] async fn main() -> Result<()> { color_eyre::install()?; tracing_subscriber::fmt::init(); info!("Starting server..."); let tera = Tera::new("templates/**/*")?; let posts = glob("posts/**/*.md")? .map(|p| { let path = p.unwrap(); let name = path .file_name() .unwrap() .to_string_lossy() .strip_suffix(".md") .unwrap() .to_owned(); Post { name, content: std::fs::read_to_string(&path).unwrap(), } }) .collect(); let state = Arc::new(State { tera, posts }); let middleware = tower::ServiceBuilder::new() .layer(TraceLayer::new_for_http()) .layer(Extension(state.clone())); let app = Router::new() .route("/", get(handlers::index)) .route("/posts", get(handlers::post_index)) .route("/posts/:name", get(handlers::post_view)) .layer(middleware); info!("Now listening at http://localhost:8180"); axum::Server::bind(&"0.0.0.0:8180".parse().unwrap()) .serve(app.into_make_service()) .await?; Ok(()) }