first refactor
This commit is contained in:
parent
a474164964
commit
f071a702af
@ -1,19 +1,9 @@
|
|||||||
use axum::{
|
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 tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
|
|
||||||
|
mod post_parser;
|
||||||
|
mod router;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
tracing_subscriber::registry()
|
tracing_subscriber::registry()
|
||||||
@ -28,108 +18,11 @@ async fn main() {
|
|||||||
.init();
|
.init();
|
||||||
|
|
||||||
// build our application with a single route
|
// build our application with a single route
|
||||||
let app = Router::new()
|
let app = router::get_router();
|
||||||
.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::<MatchedPath>()
|
|
||||||
.map(MatchedPath::as_str);
|
|
||||||
|
|
||||||
info_span!(
|
|
||||||
"http_request",
|
|
||||||
method = ?request.method(),
|
|
||||||
matched_path,
|
|
||||||
some_other_field = tracing::field::Empty,
|
|
||||||
)
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
// run our app with hyper, listening globally on port 3080
|
// run our app with hyper, listening globally on port 3080
|
||||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3080").await.unwrap();
|
let listener = tokio::net::TcpListener::bind("0.0.0.0:3080").await.unwrap();
|
||||||
axum::serve(listener, app).await.unwrap();
|
axum::serve(listener, app).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn parse_post(Path(post_id): Path<String>) -> Result<Html<String>, 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<String>,
|
|
||||||
published: bool,
|
|
||||||
#[serde(deserialize_with = "deserialize_date")]
|
|
||||||
date: DateTime<Utc>,
|
|
||||||
thumbnail: String,
|
|
||||||
tags: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
let matter = Matter::<YAML>::new();
|
|
||||||
let metadata = matter.parse_with_struct::<Metadata>(&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<DateTime<Utc>, 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 Port from env variable
|
||||||
// TODO templating system
|
// TODO templating system
|
||||||
// TODO simple Logging
|
|
||||||
// TODO parse md files
|
|
||||||
|
74
axum_server/src/post_parser.rs
Normal file
74
axum_server/src/post_parser.rs
Normal file
@ -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<DateTime<Utc>, 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<String>,
|
||||||
|
pub published: bool,
|
||||||
|
#[serde(deserialize_with = "deserialize_date")]
|
||||||
|
pub date: DateTime<Utc>,
|
||||||
|
pub thumbnail: String,
|
||||||
|
pub tags: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn parse_post(Path(post_id): Path<String>) -> Result<Html<String>, 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::<YAML>::new();
|
||||||
|
let _metadata = matter.parse_with_struct::<Metadata>(&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));
|
||||||
|
}
|
27
axum_server/src/router.rs
Normal file
27
axum_server/src/router.rs
Normal file
@ -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::<MatchedPath>()
|
||||||
|
.map(MatchedPath::as_str);
|
||||||
|
|
||||||
|
info_span!(
|
||||||
|
"http_request",
|
||||||
|
method = ?request.method(),
|
||||||
|
matched_path,
|
||||||
|
some_other_field = tracing::field::Empty,
|
||||||
|
)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user