1
0
Fork 0
website/src/main.rs

64 lines
1.5 KiB
Rust
Raw Normal View History

2022-08-31 23:20:59 +02:00
use std::{path::PathBuf, sync::Arc};
2022-06-16 23:44:37 +02:00
use axum::{
Router,
2022-08-31 23:20:59 +02:00
routing::get, Extension
2022-06-16 23:44:37 +02:00
};
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
use color_eyre::eyre::Result;
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/**/*")?;
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()));
let mut app = Router::new()
.route("/", get(handlers::index))
.route("/posts", get(handlers::post_index))
.route("/posts/:name", get(handlers::post_view));
app = app.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(())
}