diff --git a/posts/foldertest/FbHSmoeUUAA2x-m.png b/posts/foldertest/FbHSmoeUUAA2x-m.png new file mode 100644 index 0000000..ca47d91 Binary files /dev/null and b/posts/foldertest/FbHSmoeUUAA2x-m.png differ diff --git a/posts/foldertest/index.md b/posts/foldertest/index.md new file mode 100644 index 0000000..752c225 --- /dev/null +++ b/posts/foldertest/index.md @@ -0,0 +1,7 @@ +# Testing post as index within folder + +hope it works yay + +here have a squid miku to test relative paths: + +![Squid kid miku](FbHSmoeUUAA2x-m.png) diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 19eb128..bb89428 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -5,12 +5,15 @@ use tracing::log::*; use axum::{ extract::Path, response::{Html, IntoResponse, Response}, + http::{StatusCode, Uri}, Extension, }; use pulldown_cmark::{html, Options, Parser}; use crate::State; +pub mod posts; + pub enum Error { NotFound, } @@ -31,45 +34,6 @@ pub async fn index(Extension(state): Extension>) -> Result { Ok(Html(res.into())) } -pub async fn post_view( - Path(name): Path, - Extension(state): Extension>, -) -> Result { - info!("Requested post: {}", name); - let state = state.clone(); - 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); - - 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 res = state.tera.render("post.html", &ctx).map_err(|e| { - error!("Failed rendering post: {}", e); - Error::NotFound - })?; - - Ok(Html(res.into())) -} - -pub async fn post_index(Extension(state): Extension>) -> 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 - })?; - Ok(Html(res.into())) -} - impl IntoResponse for Error { fn into_response(self) -> Response { let result: Vec = "not found".into(); diff --git a/src/handlers/posts.rs b/src/handlers/posts.rs new file mode 100644 index 0000000..8b14880 --- /dev/null +++ b/src/handlers/posts.rs @@ -0,0 +1,47 @@ +use std::sync::Arc; + +use axum::{extract::Path, Extension, response::Html}; +use pulldown_cmark::{Options, Parser, html}; +use tracing::log::*; + +use crate::{State, handlers::PageContext}; +use super::{Result, Error}; + +pub async fn view( + Path(slug): Path, + Extension(state): Extension>, +) -> Result { + let post = state + .posts + .iter() + .find(|p| p.slug == slug) + .ok_or(Error::NotFound)?; + + info!("Requested post: {}", slug); + + let options = Options::all(); + let parser = Parser::new_ext(&post.content, options); + + 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 res = state.tera.render("post.html", &ctx).map_err(|e| { + error!("Failed rendering post: {}", e); + Error::NotFound + })?; + + Ok(Html(res.into())) +} + +pub async fn index(Extension(state): Extension>) -> 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 + })?; + Ok(Html(res.into())) +} diff --git a/src/main.rs b/src/main.rs index 53ea8ee..5a1c3a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::{path::PathBuf, sync::Arc}; -use axum::{routing::get, Extension, Router}; +use axum::{routing::{get, get_service}, Extension, Router, handler::Handler, http::StatusCode}; use color_eyre::eyre::Result; use glob::glob; use serde_derive::Serialize; @@ -17,7 +17,7 @@ pub struct State { #[derive(Serialize)] pub struct Post { - pub name: String, + pub slug: String, pub content: String, } @@ -31,15 +31,24 @@ async fn main() -> Result<()> { let posts = glob("posts/**/*.md")? .map(|p| { let path = p.unwrap(); - let name = path + + let filename = path .file_name() .unwrap() - .to_string_lossy() - .strip_suffix(".md") - .unwrap() - .to_owned(); + .to_string_lossy(); + + let (filename, _) = filename.rsplit_once('.') + .unwrap(); + + let slug = if filename.eq_ignore_ascii_case("index") { + path.parent().unwrap().file_name().unwrap().to_string_lossy().into_owned() + } + else { + filename.to_owned() + }; + Post { - name, + slug, content: std::fs::read_to_string(&path).unwrap(), } }) @@ -53,8 +62,12 @@ async fn main() -> Result<()> { let app = Router::new() .route("/", get(handlers::index)) - .route("/posts", get(handlers::post_index)) - .route("/posts/:name", get(handlers::post_view)) + .route("/posts/", get(handlers::posts::index)) + .route("/posts/:slug/", get(handlers::posts::view)) + .nest("/static", get_service(tower_http::services::ServeDir::new("./static")).handle_error(|e| async { + (StatusCode::NOT_FOUND, "not found") + })) + .fallback((|| async { (StatusCode::NOT_FOUND, "not found") }).into_service()) .layer(middleware); info!("Now listening at http://localhost:8180"); @@ -65,3 +78,7 @@ async fn main() -> Result<()> { Ok(()) } + +async fn healthcheck() -> &'static str { + "OK" +} diff --git a/static/site.css b/static/site.css index 230972c..73aad82 100644 --- a/static/site.css +++ b/static/site.css @@ -1,4 +1,5 @@ body { - width: 800px; + max-width: 800px; margin: auto; + padding: 8px; } \ No newline at end of file diff --git a/templates/partials/head.html b/templates/partials/head.html index 8837f79..3e2c07e 100644 --- a/templates/partials/head.html +++ b/templates/partials/head.html @@ -2,5 +2,6 @@ + Document \ No newline at end of file diff --git a/templates/partials/header.html b/templates/partials/header.html index 312ef42..0062155 100644 --- a/templates/partials/header.html +++ b/templates/partials/header.html @@ -1,3 +1,3 @@ \ No newline at end of file diff --git a/templates/postsindex.html b/templates/postsindex.html index 5ddd5cf..a99f835 100644 --- a/templates/postsindex.html +++ b/templates/postsindex.html @@ -1,6 +1,8 @@ {% extends "base.html" %} {% block main %} +

posts

+

i occasionally write some stuff i guess