From 0cb8e84666616956e9b7fac47fbd260facd7bb62 Mon Sep 17 00:00:00 2001 From: Michal Vanko Date: Thu, 22 Feb 2024 20:13:23 +0100 Subject: [PATCH] site_header start --- axum_server/Cargo.toml | 2 +- axum_server/justfile | 9 +- axum_server/src/components/mod.rs | 1 + axum_server/src/components/site_header.rs | 29 ++ axum_server/src/main.rs | 3 +- axum_server/src/pages/index.rs | 11 +- axum_server/src/pages/post.rs | 7 +- axum_server/src/pages/post_list.rs | 7 +- axum_server/styles/input.css | 3 + axum_server/styles/output.css | 562 ++++++++++++++++++++++ axum_server/tailwind.config.js | 8 + axum_server/templates/base.html | 10 +- axum_server/templates/site_header.html | 31 ++ 13 files changed, 673 insertions(+), 10 deletions(-) create mode 100644 axum_server/src/components/site_header.rs create mode 100644 axum_server/styles/input.css create mode 100644 axum_server/styles/output.css create mode 100644 axum_server/tailwind.config.js create mode 100644 axum_server/templates/site_header.html diff --git a/axum_server/Cargo.toml b/axum_server/Cargo.toml index 8e85b38..1541f9a 100644 --- a/axum_server/Cargo.toml +++ b/axum_server/Cargo.toml @@ -16,6 +16,6 @@ rss = "2.0.7" serde = "1.0.195" serde_json = "1.0.111" tokio = { version = "1.35.1", features = ["full"] } -tower-http = { version = "0.5.0", features = ["trace"] } +tower-http = { version = "0.5.0", features = ["trace", "fs"] } tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } diff --git a/axum_server/justfile b/axum_server/justfile index fc067d4..1b9d18c 100644 --- a/axum_server/justfile +++ b/axum_server/justfile @@ -1,8 +1,15 @@ port := env_var_or_default('PORT', '3080') +# Tailwind in watch mode +tailwind: + npx tailwindcss -i ./styles/input.css -o ./styles/output.css --watch + +server_dev: + cargo watch -x run + # Run dev server in watch mode dev: - cargo watch -x run + (just server_dev; just tailwind) | parallel # Run server in production mode prod: diff --git a/axum_server/src/components/mod.rs b/axum_server/src/components/mod.rs index 20389a4..3b29951 100644 --- a/axum_server/src/components/mod.rs +++ b/axum_server/src/components/mod.rs @@ -1 +1,2 @@ pub mod site_footer; +pub mod site_header; diff --git a/axum_server/src/components/site_header.rs b/axum_server/src/components/site_header.rs new file mode 100644 index 0000000..62f36d8 --- /dev/null +++ b/axum_server/src/components/site_header.rs @@ -0,0 +1,29 @@ +pub struct Link { + pub href: String, + pub label: String, +} + +pub struct HeaderProps { + pub links: Vec, +} + +impl Default for HeaderProps { + fn default() -> Self { + Self { + links: vec![ + Link { + href: "/".to_string(), + label: "Introduction".to_string(), + }, + Link { + href: "/blog".to_string(), + label: "Blog".to_string(), + }, + Link { + href: "/portfolio".to_string(), + label: "Portfolio".to_string(), + }, + ], + } + } +} diff --git a/axum_server/src/main.rs b/axum_server/src/main.rs index b30e175..d868f4e 100644 --- a/axum_server/src/main.rs +++ b/axum_server/src/main.rs @@ -1,4 +1,5 @@ use axum; +use tower_http::services::ServeDir; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; mod components; @@ -23,7 +24,7 @@ async fn main() { .init(); // build our application with a single route - let app = router::get_router(); + let app = router::get_router().nest_service("/styles", ServeDir::new("styles")); // run our app with hyper, listening globally on port 3080 let port = std::option_env!("PORT").unwrap_or("3080"); let addr = format!("0.0.0.0:{}", port); diff --git a/axum_server/src/pages/index.rs b/axum_server/src/pages/index.rs index 297730f..dec6797 100644 --- a/axum_server/src/pages/index.rs +++ b/axum_server/src/pages/index.rs @@ -1,14 +1,21 @@ use askama::Template; -use crate::components::site_footer::{render_site_footer, SiteFooter}; +use crate::components::{ + site_footer::{render_site_footer, SiteFooter}, + site_header::HeaderProps, +}; #[derive(Template)] #[template(path = "index.html")] pub struct IndexTemplate { site_footer: SiteFooter, + header_props: HeaderProps, } pub async fn render_index() -> IndexTemplate { let site_footer = render_site_footer().await; - IndexTemplate { site_footer } + IndexTemplate { + site_footer, + header_props: HeaderProps::default(), + } } diff --git a/axum_server/src/pages/post.rs b/axum_server/src/pages/post.rs index 58e4306..6b1e07b 100644 --- a/axum_server/src/pages/post.rs +++ b/axum_server/src/pages/post.rs @@ -4,7 +4,10 @@ use chrono::{DateTime, Utc}; use serde::Deserialize; use crate::{ - components::site_footer::{render_site_footer, SiteFooter}, + components::{ + site_footer::{render_site_footer, SiteFooter}, + site_header::HeaderProps, + }, post_parser::{deserialize_date, parse_post}, }; @@ -26,6 +29,7 @@ pub struct PostTemplate { pub title: String, pub body: String, pub site_footer: SiteFooter, + pub header_props: HeaderProps, } pub async fn render_post(Path(post_id): Path) -> Result { @@ -40,5 +44,6 @@ pub async fn render_post(Path(post_id): Path) -> Result>, pub tag: Option, pub site_footer: SiteFooter, + pub header_props: HeaderProps, } pub async fn render_post_list(tag: Option>) -> Result { @@ -51,6 +55,7 @@ pub async fn render_post_list(tag: Option>) -> Result