use std::{sync::Arc, time}; use axum::{routing::get, Extension, Router}; use color_eyre::eyre::Result; use post::Post; use tera::Tera; use tower_http::trace::TraceLayer; use tracing::log::*; mod handlers; mod post; pub struct State { posts: Vec, tera: Tera, } #[tokio::main] async fn main() -> Result<()> { color_eyre::install()?; tracing_subscriber::fmt::init(); info!("Starting server..."); let ts = time::Instant::now(); let tera = Tera::new("templates/**/*")?; let posts = post::load_all()?; 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)) .nest( "/posts", Router::new() .route("/", get(handlers::posts::index)) .route("/:route/", get(handlers::posts::view)) .fallback_service(tower_http::services::ServeDir::new("./posts")), ) .nest_service("/static", tower_http::services::ServeDir::new("./static")) .layer(middleware); let duration = time::Instant::now() - ts; info!("loaded server in {}ms", duration.as_secs_f64() * 1000.0); info!("Now listening at http://localhost:8180"); axum::Server::bind(&"0.0.0.0:8180".parse().unwrap()) .serve(app.into_make_service()) .await?; Ok(()) }