cargo fmt
This commit is contained in:
parent
33298d757d
commit
b98031de94
2 changed files with 53 additions and 37 deletions
|
@ -1,13 +1,16 @@
|
||||||
use std::sync::Arc;
|
|
||||||
use serde_derive::Serialize;
|
use serde_derive::Serialize;
|
||||||
|
use std::sync::Arc;
|
||||||
use tracing::log::*;
|
use tracing::log::*;
|
||||||
|
|
||||||
use axum::{response::{Html, IntoResponse, Response}, extract::Path, Extension};
|
use axum::{
|
||||||
use pulldown_cmark::{Options, Parser, html};
|
extract::Path,
|
||||||
|
response::{Html, IntoResponse, Response},
|
||||||
|
Extension,
|
||||||
|
};
|
||||||
|
use pulldown_cmark::{html, Options, Parser};
|
||||||
|
|
||||||
use crate::State;
|
use crate::State;
|
||||||
|
|
||||||
|
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
NotFound,
|
NotFound,
|
||||||
}
|
}
|
||||||
|
@ -16,19 +19,29 @@ pub type Result<T = Html<Vec<u8>>> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct PageContext {
|
struct PageContext {
|
||||||
content: String
|
content: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn index(Extension(state): Extension<Arc<State>>) -> Result {
|
pub async fn index(Extension(state): Extension<Arc<State>>) -> Result {
|
||||||
let ctx = tera::Context::new();
|
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()))
|
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);
|
info!("Requested post: {}", name);
|
||||||
let state = state.clone();
|
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 options = Options::all();
|
||||||
let parser = Parser::new_ext(&post.content, options);
|
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();
|
let mut out = String::new();
|
||||||
html::push_html(&mut out, parser);
|
html::push_html(&mut out, parser);
|
||||||
|
|
||||||
let ctx = tera::Context::from_serialize(PageContext {
|
let ctx =
|
||||||
content: out
|
tera::Context::from_serialize(PageContext { content: out }).map_err(|_| Error::NotFound)?;
|
||||||
}).map_err(|_| Error::NotFound)?;
|
|
||||||
|
|
||||||
|
let res = state.tera.render("post.html", &ctx).map_err(|e| {
|
||||||
let res = state.tera.render("post.html", &ctx).map_err(|e| { error!("Failed rendering post: {}", e); Error::NotFound})?;
|
error!("Failed rendering post: {}", e);
|
||||||
|
Error::NotFound
|
||||||
|
})?;
|
||||||
|
|
||||||
Ok(Html(res.into()))
|
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 {
|
pub async fn post_index(Extension(state): Extension<Arc<State>>) -> Result {
|
||||||
let mut ctx = tera::Context::new();
|
let mut ctx = tera::Context::new();
|
||||||
ctx.insert("posts", &state.posts);
|
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()))
|
Ok(Html(res.into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,12 +75,7 @@ impl IntoResponse for Error {
|
||||||
let result: Vec<u8> = "not found".into();
|
let result: Vec<u8> = "not found".into();
|
||||||
let body = axum::body::boxed(axum::body::Full::from(result));
|
let body = axum::body::boxed(axum::body::Full::from(result));
|
||||||
match self {
|
match self {
|
||||||
Error::NotFound => {
|
Error::NotFound => Response::builder().status(404).body(body).unwrap(),
|
||||||
Response::builder()
|
|
||||||
.status(404)
|
|
||||||
.body(body)
|
|
||||||
.unwrap()
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
38
src/main.rs
38
src/main.rs
|
@ -1,15 +1,12 @@
|
||||||
use std::{path::PathBuf, sync::Arc};
|
use std::{path::PathBuf, sync::Arc};
|
||||||
|
|
||||||
use axum::{
|
use axum::{routing::get, Extension, Router};
|
||||||
Router,
|
use color_eyre::eyre::Result;
|
||||||
routing::get, Extension
|
|
||||||
};
|
|
||||||
use glob::glob;
|
use glob::glob;
|
||||||
use serde_derive::Serialize;
|
use serde_derive::Serialize;
|
||||||
use tera::Tera;
|
use tera::Tera;
|
||||||
use tower_http::trace::TraceLayer;
|
use tower_http::trace::TraceLayer;
|
||||||
use tracing::log::*;
|
use tracing::log::*;
|
||||||
use color_eyre::eyre::Result;
|
|
||||||
|
|
||||||
mod handlers;
|
mod handlers;
|
||||||
|
|
||||||
|
@ -31,14 +28,22 @@ async fn main() -> Result<()> {
|
||||||
info!("Starting server...");
|
info!("Starting server...");
|
||||||
|
|
||||||
let tera = Tera::new("templates/**/*")?;
|
let tera = Tera::new("templates/**/*")?;
|
||||||
let posts = glob("posts/**/*.md")?.map(|p| {
|
let posts = glob("posts/**/*.md")?
|
||||||
let path = p.unwrap();
|
.map(|p| {
|
||||||
let name = path.file_name().unwrap().to_string_lossy().strip_suffix(".md").unwrap().to_owned();
|
let path = p.unwrap();
|
||||||
Post {
|
let name = path
|
||||||
name,
|
.file_name()
|
||||||
content: std::fs::read_to_string(&path).unwrap(),
|
.unwrap()
|
||||||
}
|
.to_string_lossy()
|
||||||
}).collect();
|
.strip_suffix(".md")
|
||||||
|
.unwrap()
|
||||||
|
.to_owned();
|
||||||
|
Post {
|
||||||
|
name,
|
||||||
|
content: std::fs::read_to_string(&path).unwrap(),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
let state = Arc::new(State { tera, posts });
|
let state = Arc::new(State { tera, posts });
|
||||||
|
|
||||||
|
@ -46,12 +51,11 @@ async fn main() -> Result<()> {
|
||||||
.layer(TraceLayer::new_for_http())
|
.layer(TraceLayer::new_for_http())
|
||||||
.layer(Extension(state.clone()));
|
.layer(Extension(state.clone()));
|
||||||
|
|
||||||
let mut app = Router::new()
|
let app = Router::new()
|
||||||
.route("/", get(handlers::index))
|
.route("/", get(handlers::index))
|
||||||
.route("/posts", get(handlers::post_index))
|
.route("/posts", get(handlers::post_index))
|
||||||
.route("/posts/:name", get(handlers::post_view));
|
.route("/posts/:name", get(handlers::post_view))
|
||||||
app = app.layer(middleware);
|
.layer(middleware);
|
||||||
|
|
||||||
|
|
||||||
info!("Now listening at http://localhost:8180");
|
info!("Now listening at http://localhost:8180");
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue