site_header start

This commit is contained in:
2024-02-22 20:13:23 +01:00
parent 8892624e9a
commit 0cb8e84666
13 changed files with 673 additions and 10 deletions

View File

@ -1 +1,2 @@
pub mod site_footer;
pub mod site_header;

View File

@ -0,0 +1,29 @@
pub struct Link {
pub href: String,
pub label: String,
}
pub struct HeaderProps {
pub links: Vec<Link>,
}
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(),
},
],
}
}
}

View File

@ -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);

View File

@ -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(),
}
}

View File

@ -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<String>) -> Result<PostTemplate, StatusCode> {
@ -40,5 +44,6 @@ pub async fn render_post(Path(post_id): Path<String>) -> Result<PostTemplate, St
title: parsed.metadata.title,
body: parsed.body,
site_footer,
header_props: HeaderProps::default(),
})
}

View File

@ -2,7 +2,10 @@ use askama::Template;
use axum::{extract::Path, http::StatusCode};
use crate::{
components::site_footer::{render_site_footer, SiteFooter},
components::{
site_footer::{render_site_footer, SiteFooter},
site_header::HeaderProps,
},
post_list::get_post_list,
post_parser::ParseResult,
};
@ -16,6 +19,7 @@ pub struct PostListTemplate {
pub posts: Vec<ParseResult<PostMetadata>>,
pub tag: Option<String>,
pub site_footer: SiteFooter,
pub header_props: HeaderProps,
}
pub async fn render_post_list(tag: Option<Path<String>>) -> Result<PostListTemplate, StatusCode> {
@ -51,6 +55,7 @@ pub async fn render_post_list(tag: Option<Path<String>>) -> Result<PostListTempl
posts,
tag,
site_footer,
header_props: HeaderProps::default(),
})
}