From f071a702af73d524efb19931dd4f3025f76a7cd1 Mon Sep 17 00:00:00 2001 From: Michal Vanko Date: Tue, 9 Jan 2024 20:54:36 +0100 Subject: [PATCH] first refactor --- axum_server/src/main.rs | 117 ++------------------------------- axum_server/src/post_parser.rs | 74 +++++++++++++++++++++ axum_server/src/router.rs | 27 ++++++++ 3 files changed, 106 insertions(+), 112 deletions(-) create mode 100644 axum_server/src/post_parser.rs create mode 100644 axum_server/src/router.rs diff --git a/axum_server/src/main.rs b/axum_server/src/main.rs index 47151e1..127747a 100644 --- a/axum_server/src/main.rs +++ b/axum_server/src/main.rs @@ -1,19 +1,9 @@ -use axum::{ - extract::{MatchedPath, Path}, - http::{Request, StatusCode}, - response::Html, - routing::get, - Router, -}; -use chrono::{DateTime, Utc}; -use gray_matter::{engine::YAML, Matter}; -use markdown::{to_html_with_options, CompileOptions, Constructs, Options, ParseOptions}; -use serde::{Deserialize, Deserializer}; -use tokio::fs; -use tower_http::trace::TraceLayer; -use tracing::info_span; +use axum; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; +mod post_parser; +mod router; + #[tokio::main] async fn main() { tracing_subscriber::registry() @@ -28,108 +18,11 @@ async fn main() { .init(); // build our application with a single route - let app = Router::new() - .route("/", get(|| async { "Hello, World!" })) - .route("/blog/:post_id", get(parse_post)) - .layer( - TraceLayer::new_for_http().make_span_with(|request: &Request<_>| { - // Log the matched route's path (with placeholders not filled in). - // Use request.uri() or OriginalUri if you want the real path. - let matched_path = request - .extensions() - .get::() - .map(MatchedPath::as_str); - - info_span!( - "http_request", - method = ?request.method(), - matched_path, - some_other_field = tracing::field::Empty, - ) - }), - ); - + let app = router::get_router(); // run our app with hyper, listening globally on port 3080 let listener = tokio::net::TcpListener::bind("0.0.0.0:3080").await.unwrap(); axum::serve(listener, app).await.unwrap(); } -async fn parse_post(Path(post_id): Path) -> Result, StatusCode> { - let path = format!("../_posts/blog/{}", post_id); - let contents = fs::read_to_string(path).await; - - let raw_content = match contents { - Err(_reason) => { - // TODO find the real reason - return Err(StatusCode::NOT_FOUND); - } - Ok(content) => content, - }; - - let markdown_options = Options { - parse: ParseOptions { - constructs: Constructs { - frontmatter: true, - ..Default::default() - }, - ..Default::default() - }, - compile: CompileOptions { - allow_dangerous_html: true, - ..Default::default() - }, - ..Default::default() - }; - - #[derive(Deserialize, Debug)] - struct Metadata { - layout: String, - title: String, - segments: Vec, - published: bool, - #[serde(deserialize_with = "deserialize_date")] - date: DateTime, - thumbnail: String, - tags: Vec, - } - - let matter = Matter::::new(); - let metadata = matter.parse_with_struct::(&raw_content); - - // Deserialize JSON into MyData struct - - // Print the entire struct using the Debug trait - println!("{:?}", metadata.unwrap().data); - - let parsed = to_html_with_options(&raw_content, &markdown_options); - - let content = match parsed { - Err(reason) => { - tracing::error!(reason); - return Err(StatusCode::INTERNAL_SERVER_ERROR); - } - Ok(content) => content, - }; - - // TODO Parse file - return Ok(Html(content)); -} - -fn deserialize_date<'de, D>(deserializer: D) -> Result, D::Error> -where - D: Deserializer<'de>, -{ - let date_str = String::deserialize(deserializer)?; - match DateTime::parse_from_rfc3339(&date_str) { - Ok(datetime) => Ok(datetime.with_timezone(&Utc)), - Err(err) => Err(serde::de::Error::custom(format!( - "Error parsing date: {}", - err - ))), - } -} - // TODO Port from env variable // TODO templating system -// TODO simple Logging -// TODO parse md files diff --git a/axum_server/src/post_parser.rs b/axum_server/src/post_parser.rs new file mode 100644 index 0000000..3235dfd --- /dev/null +++ b/axum_server/src/post_parser.rs @@ -0,0 +1,74 @@ +use axum::{extract::Path, http::StatusCode, response::Html}; +use chrono::{DateTime, Utc}; +use gray_matter::{engine::YAML, Matter}; +use markdown::{to_html_with_options, CompileOptions, Constructs, Options, ParseOptions}; +use serde::{Deserialize, Deserializer}; +use tokio::fs; + +fn deserialize_date<'de, D>(deserializer: D) -> Result, D::Error> +where + D: Deserializer<'de>, +{ + let date_str = String::deserialize(deserializer)?; + match DateTime::parse_from_rfc3339(&date_str) { + Ok(datetime) => Ok(datetime.with_timezone(&Utc)), + Err(err) => Err(serde::de::Error::custom(format!( + "Error parsing date: {}", + err + ))), + } +} + +#[derive(Deserialize, Debug)] +pub struct Metadata { + pub layout: String, + pub title: String, + pub segments: Vec, + pub published: bool, + #[serde(deserialize_with = "deserialize_date")] + pub date: DateTime, + pub thumbnail: String, + pub tags: Vec, +} + +pub async fn parse_post(Path(post_id): Path) -> Result, StatusCode> { + let path = format!("../_posts/blog/{}.md", post_id); + let contents = fs::read_to_string(path).await; + + let raw_content = match contents { + Err(_reason) => { + // TODO find the real reason + return Err(StatusCode::NOT_FOUND); + } + Ok(content) => content, + }; + + let markdown_options = Options { + parse: ParseOptions { + constructs: Constructs { + frontmatter: true, + ..Default::default() + }, + ..Default::default() + }, + compile: CompileOptions { + allow_dangerous_html: true, + ..Default::default() + }, + ..Default::default() + }; + + let matter = Matter::::new(); + let _metadata = matter.parse_with_struct::(&raw_content); + let parsed = to_html_with_options(&raw_content, &markdown_options); + + let content = match parsed { + Err(reason) => { + tracing::error!(reason); + return Err(StatusCode::INTERNAL_SERVER_ERROR); + } + Ok(content) => content, + }; + + return Ok(Html(content)); +} diff --git a/axum_server/src/router.rs b/axum_server/src/router.rs new file mode 100644 index 0000000..f6b16ee --- /dev/null +++ b/axum_server/src/router.rs @@ -0,0 +1,27 @@ +use crate::post_parser::parse_post; +use axum::{extract::MatchedPath, http::Request, routing::get, Router}; +use tower_http::trace::TraceLayer; +use tracing::info_span; + +pub fn get_router() -> Router { + Router::new() + .route("/", get(|| async { "Hello, World!" })) + .route("/blog/:post_id", get(parse_post)) + .layer( + TraceLayer::new_for_http().make_span_with(|request: &Request<_>| { + // Log the matched route's path (with placeholders not filled in). + // Use request.uri() or OriginalUri if you want the real path. + let matched_path = request + .extensions() + .get::() + .map(MatchedPath::as_str); + + info_span!( + "http_request", + method = ?request.method(), + matched_path, + some_other_field = tracing::field::Empty, + ) + }), + ) +}