1
0
Fork 0
website/src/main.rs

68 lines
1.6 KiB
Rust
Raw Normal View History

2022-08-31 23:20:59 +02:00
use std::{path::PathBuf, sync::Arc};
2022-08-31 23:25:17 +02:00
use axum::{routing::get, Extension, Router};
use color_eyre::eyre::Result;
2022-08-31 23:20:59 +02:00
use glob::glob;
use serde_derive::Serialize;
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;
pub struct State {
posts: Vec<Post>,
tera: Tera,
}
#[derive(Serialize)]
pub struct Post {
pub name: String,
pub content: String,
}
2022-06-16 23:44:37 +02:00
#[tokio::main]
async fn main() -> Result<()> {
color_eyre::install()?;
tracing_subscriber::fmt::init();
info!("Starting server...");
2022-08-31 23:20:59 +02:00
let tera = Tera::new("templates/**/*")?;
2022-08-31 23:25:17 +02:00
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();
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))
.route("/posts", get(handlers::post_index))
2022-08-31 23:25:17 +02:00
.route("/posts/:name", get(handlers::post_view))
.layer(middleware);
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(())
}