From f2bd378e1a8cdfa7d1520b3734a748dd1cd9de25 Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Sat, 7 Sep 2024 22:39:41 -0400 Subject: feat: serve static and assets add policies and index from base --- src/main.rs | 82 +++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 43b6fa6..ca0a3d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use tower_http::services::ServeDir; use axum::response::Html; use axum::extract::State; use std::sync::Arc; @@ -11,29 +12,12 @@ use std::io::*; use std::path::Path; use std::path::PathBuf; -trait Post<'a>: fmt::Debug { +trait Post: fmt::Debug { fn dump(&self); - fn display_name(&'a self) -> &'a str; + fn display_name(&self) -> Cow; fn get_content(&self) -> Cow; } -#[derive(Debug)] -struct MockPost; - -impl<'a> Post<'a> for MockPost { - fn dump(&self) { - println!("Post content goes here."); - } - - fn display_name(&'a self) -> &'a str { - "" - } - - fn get_content(&self) -> Cow { - Cow::Borrowed("") - } -} - struct MdFilePost { path: PathBuf, name: OsString, @@ -48,13 +32,13 @@ impl MdFilePost { } } -impl<'a> Post<'a> for MdFilePost { +impl Post for MdFilePost { fn dump(&self) { println!("{}: {}", &self.display_name(), &self.get_content()); } - fn display_name(&'a self) -> &'a str { - self.name.to_str().unwrap() + fn display_name(&self) -> Cow { + self.name.to_string_lossy() } fn get_content(&self) -> Cow { @@ -94,8 +78,8 @@ impl Posts for FsDirPosts { } } -trait Page<'a>: Template { - fn from_post(post: &impl Post<'a>) -> Self; +trait Page: Template { + fn from_post(post: &impl Post) -> Self; } #[derive(Template)] @@ -104,32 +88,60 @@ struct MdPage { article: String, } -impl<'a> Page<'a> for MdPage { - fn from_post(post: &impl Post<'a>) -> Self { +impl Page for MdPage { + fn from_post(post: &impl Post) -> Self { Self { article: post.get_content().into_owned(), } } } -fn test_get_posts(posts: &impl Posts) { - for post in posts.get_posts() { - let page = MdPage::from_post(&post); - println!("{}", page.render().unwrap()); +#[derive(Template)] +#[template(path = "posts.html")] +struct PostsView { + post_titles: Vec +} + +impl PostsView { + fn with_posts(posts: &impl Posts) -> Self { + Self { + post_titles: posts.get_posts().map(|p| p.display_name().into_owned()).map(String::from).collect() + } } } -async fn handler(State(posts): State>) -> Html { - let post = posts.get_posts().next().unwrap(); - let page = MdPage::from_post(&post); - Html(page.render().unwrap()) +async fn posts_handler(State(posts): State>) -> Html { + let view = PostsView::with_posts(&*posts); + Html(view.render().unwrap()) +} + +#[derive(Template)] +#[template(path = "index.html")] +struct IndexTemplate; + +async fn index_handler() -> Html { + Html(IndexTemplate {}.render().unwrap()) +} + +#[derive(Template)] +#[template(path = "policies.html")] +struct PoliciesTemplate; + +async fn policies_handler() -> Html { + Html(PoliciesTemplate{}.render().unwrap()) } #[tokio::main] async fn main() { let repo = Arc::new(FsDirPosts::new(&format!("/data/ct/{}", "blog"))); - let app = Router::new().route("/posts", get(handler)).with_state(repo); + let app = Router::new() + .nest_service("/static", ServeDir::new("static")) + .nest_service("/assets", ServeDir::new("/data/ct/assets")) + .route("/posts", get(posts_handler)) + .route("/policies", get(policies_handler)) + .route("/", get(index_handler)) + .with_state(repo); let listener = tokio::net::TcpListener::bind("0.0.0.0:8000").await.unwrap(); axum::serve(listener, app).await.unwrap(); -- cgit v1.2.3