1
0
Fork 0

cargo fmt

This commit is contained in:
Adrian Hedqvist 2022-08-31 23:25:17 +02:00
parent 33298d757d
commit b98031de94
2 changed files with 53 additions and 37 deletions

View file

@ -1,13 +1,16 @@
use std::sync::Arc;
use serde_derive::Serialize;
use std::sync::Arc;
use tracing::log::*;
use axum::{response::{Html, IntoResponse, Response}, extract::Path, Extension};
use pulldown_cmark::{Options, Parser, html};
use axum::{
extract::Path,
response::{Html, IntoResponse, Response},
Extension,
};
use pulldown_cmark::{html, Options, Parser};
use crate::State;
pub enum Error {
NotFound,
}
@ -16,19 +19,29 @@ pub type Result<T = Html<Vec<u8>>> = std::result::Result<T, Error>;
#[derive(Serialize)]
struct PageContext {
content: String
content: String,
}
pub async fn index(Extension(state): Extension<Arc<State>>) -> Result {
let ctx = tera::Context::new();
let res = state.tera.render("index.html", &ctx).map_err(|e| { error!("Failed rendering index: {}", e); Error::NotFound})?;
let res = state.tera.render("index.html", &ctx).map_err(|e| {
error!("Failed rendering index: {}", e);
Error::NotFound
})?;
Ok(Html(res.into()))
}
pub async fn post_view(Path(name): Path<String>, Extension(state): Extension<Arc<State>>) -> Result {
pub async fn post_view(
Path(name): Path<String>,
Extension(state): Extension<Arc<State>>,
) -> Result {
info!("Requested post: {}", name);
let state = state.clone();
let post = state.posts.iter().find(|p| p.name == name).ok_or(Error::NotFound)?;
let post = state
.posts
.iter()
.find(|p| p.name == name)
.ok_or(Error::NotFound)?;
let options = Options::all();
let parser = Parser::new_ext(&post.content, options);
@ -36,12 +49,13 @@ pub async fn post_view(Path(name): Path<String>, Extension(state): Extension<Arc
let mut out = String::new();
html::push_html(&mut out, parser);
let ctx = tera::Context::from_serialize(PageContext {
content: out
}).map_err(|_| Error::NotFound)?;
let ctx =
tera::Context::from_serialize(PageContext { content: out }).map_err(|_| Error::NotFound)?;
let res = state.tera.render("post.html", &ctx).map_err(|e| { error!("Failed rendering post: {}", e); Error::NotFound})?;
let res = state.tera.render("post.html", &ctx).map_err(|e| {
error!("Failed rendering post: {}", e);
Error::NotFound
})?;
Ok(Html(res.into()))
}
@ -49,7 +63,10 @@ pub async fn post_view(Path(name): Path<String>, Extension(state): Extension<Arc
pub async fn post_index(Extension(state): Extension<Arc<State>>) -> Result {
let mut ctx = tera::Context::new();
ctx.insert("posts", &state.posts);
let res = state.tera.render("postsindex.html", &ctx).map_err(|e| { error!("Failed rendering posts index: {}", e); Error::NotFound})?;
let res = state.tera.render("postsindex.html", &ctx).map_err(|e| {
error!("Failed rendering posts index: {}", e);
Error::NotFound
})?;
Ok(Html(res.into()))
}
@ -58,12 +75,7 @@ impl IntoResponse for Error {
let result: Vec<u8> = "not found".into();
let body = axum::body::boxed(axum::body::Full::from(result));
match self {
Error::NotFound => {
Response::builder()
.status(404)
.body(body)
.unwrap()
},
Error::NotFound => Response::builder().status(404).body(body).unwrap(),
}
}
}

View file

@ -1,15 +1,12 @@
use std::{path::PathBuf, sync::Arc};
use axum::{
Router,
routing::get, Extension
};
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::*;
use color_eyre::eyre::Result;
mod handlers;
@ -31,14 +28,22 @@ async fn main() -> Result<()> {
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 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 });
@ -46,12 +51,11 @@ async fn main() -> Result<()> {
.layer(TraceLayer::new_for_http())
.layer(Extension(state.clone()));
let mut app = Router::new()
let app = Router::new()
.route("/", get(handlers::index))
.route("/posts", get(handlers::post_index))
.route("/posts/:name", get(handlers::post_view));
app = app.layer(middleware);
.route("/posts/:name", get(handlers::post_view))
.layer(middleware);
info!("Now listening at http://localhost:8180");