2023-03-25 12:23:11 +01:00
|
|
|
use std::{sync::Arc, time};
|
2022-09-05 00:05:24 +02:00
|
|
|
|
2023-03-25 12:23:11 +01:00
|
|
|
use axum::{routing::get, Extension, Router};
|
2022-08-31 23:25:17 +02:00
|
|
|
use color_eyre::eyre::Result;
|
2023-03-25 12:23:11 +01:00
|
|
|
use post::Post;
|
2022-08-31 23:20:59 +02:00
|
|
|
use tera::Tera;
|
2022-06-16 23:44:37 +02:00
|
|
|
use tower_http::trace::TraceLayer;
|
2022-08-31 23:20:59 +02:00
|
|
|
use tracing::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 {
|
|
|
|
posts: Vec<Post>,
|
|
|
|
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 12:23:11 +01:00
|
|
|
let ts = time::Instant::now();
|
2022-09-05 00:02:58 +02:00
|
|
|
|
2023-03-25 12:23:11 +01:00
|
|
|
let tera = Tera::new("templates/**/*")?;
|
|
|
|
let posts = post::load_all()?;
|
2022-06-16 23:44:37 +02:00
|
|
|
|
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())
|
|
|
|
.layer(Extension(state.clone()));
|
|
|
|
|
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 12:23:11 +01:00
|
|
|
.nest(
|
|
|
|
"/posts",
|
|
|
|
Router::new()
|
|
|
|
.route("/", get(handlers::posts::index))
|
|
|
|
.route("/:route/", get(handlers::posts::view))
|
|
|
|
.fallback_service(tower_http::services::ServeDir::new("./posts")),
|
|
|
|
)
|
2023-03-22 22:39:18 +01:00
|
|
|
.nest_service("/static", tower_http::services::ServeDir::new("./static"))
|
2022-08-31 23:25:17 +02:00
|
|
|
.layer(middleware);
|
2022-06-16 23:44:37 +02:00
|
|
|
|
2023-03-25 12:23:11 +01:00
|
|
|
let duration = time::Instant::now() - ts;
|
|
|
|
|
|
|
|
info!("loaded server in {}ms", duration.as_secs_f64() * 1000.0);
|
2022-06-16 23:44:37 +02: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(())
|
|
|
|
}
|