mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 18:36:21 +00:00
feat(global) better logging, add several fields for get_post api
- add sub_title, category, header_img to `get_post`
This commit is contained in:
7 files changed
+194
-75
No files matched your search
@@ -6,17 +6,20 @@ use std::str::FromStr;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde_json::{Map, Value};
|
||||
use tracing::{error, warn};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::constants::GLOBAL_TIME_FORMAT;
|
||||
use crate::markdown::meta::parse_meta;
|
||||
|
||||
pub fn from_path(path: String) -> Result<Map<String, Value>, Box<dyn Error>> {
|
||||
pub fn from_path(path: &str) -> Result<Map<String, Value>, Box<dyn Error>> {
|
||||
let mut res = Map::new();
|
||||
|
||||
let md_content = fs::read_to_string(&path)?;
|
||||
// field `content`
|
||||
let md_content = fs::read_to_string(path)?;
|
||||
res.insert("content".to_string(), Value::from(md_content.to_owned()));
|
||||
|
||||
// field `meta`
|
||||
let meta = parse_meta(&md_content);
|
||||
let meta_map: Map<String, Value> = meta
|
||||
.clone()
|
||||
@@ -25,25 +28,35 @@ pub fn from_path(path: String) -> Result<Map<String, Value>, Box<dyn Error>> {
|
||||
.collect();
|
||||
res.insert("meta".to_string(), Value::from(meta_map));
|
||||
|
||||
// field `post_id`
|
||||
let post_id = match meta.get("id") {
|
||||
None => Uuid::new_v4(),
|
||||
Some(id) => Uuid::from_str(id.as_str()).expect("Invalid UUID String"),
|
||||
Some(id) => match Uuid::from_str(id.as_str()) {
|
||||
Ok(id) => id,
|
||||
Err(e) => {
|
||||
error!("Failed to parse UUID <{}>: {}", id.as_str(), e.to_string());
|
||||
return Err(Box::new(e));
|
||||
}
|
||||
},
|
||||
};
|
||||
res.insert("post_id".to_string(), Value::from(post_id.to_string()));
|
||||
|
||||
// field `title`
|
||||
let title = match meta.get("title") {
|
||||
None => "Title Not Found".to_string(),
|
||||
Some(title) => title.to_owned(),
|
||||
};
|
||||
res.insert("title".to_string(), Value::from(title));
|
||||
|
||||
// field `summary`
|
||||
let summary = match meta.get("summary") {
|
||||
None => "".to_string(),
|
||||
Some(summary) => summary.to_owned(),
|
||||
};
|
||||
res.insert("summary".to_string(), Value::from(summary));
|
||||
|
||||
let git_output = Command::new("git")
|
||||
// field `last_update`
|
||||
let git_output = match Command::new("git")
|
||||
.arg("--no-pager")
|
||||
.arg("log")
|
||||
.arg("-1")
|
||||
@@ -52,7 +65,13 @@ pub fn from_path(path: String) -> Result<Map<String, Value>, Box<dyn Error>> {
|
||||
.current_dir(env::var("GIT_WORK_DIR")?)
|
||||
.arg(&path)
|
||||
.output()
|
||||
.expect("Failed to execute git command");
|
||||
{
|
||||
Ok(res) => res,
|
||||
Err(e) => {
|
||||
error!("Failed to execute `git` command: {}", e.to_string());
|
||||
return Err(Box::new(e));
|
||||
}
|
||||
};
|
||||
let last_update: DateTime<Utc> = if git_output.status.success() {
|
||||
chrono::DateTime::parse_from_str(
|
||||
str::from_utf8(&git_output.stdout).unwrap().trim_end(),
|
||||
@@ -68,11 +87,28 @@ pub fn from_path(path: String) -> Result<Map<String, Value>, Box<dyn Error>> {
|
||||
Value::from(last_update.format(GLOBAL_TIME_FORMAT).to_string()),
|
||||
);
|
||||
|
||||
// field `first_update`
|
||||
let first_update: DateTime<Utc> = match meta.get("date") {
|
||||
None => Utc::now().to_utc(),
|
||||
None => {
|
||||
warn!(
|
||||
"Failed to parse first update time for post {}, using current time instead",
|
||||
post_id.to_string()
|
||||
);
|
||||
Utc::now().to_utc()
|
||||
}
|
||||
Some(date) => DateTime::from(
|
||||
DateTime::parse_from_str(format!("{}T000000", date).as_str(), "%Y-%m-%d%zT%H%M%S")
|
||||
.expect("Failed to parse first_update time"),
|
||||
match DateTime::parse_from_str(format!("{}T000000", date).as_str(), "%Y-%m-%d%zT%H%M%S")
|
||||
{
|
||||
Ok(time) => time,
|
||||
Err(e) => {
|
||||
warn!(
|
||||
"Failed to parse first update time for post {}, using current time instead: {}",
|
||||
post_id.to_string(),
|
||||
e.to_string()
|
||||
);
|
||||
DateTime::from(Utc::now())
|
||||
}
|
||||
},
|
||||
),
|
||||
};
|
||||
res.insert(
|
||||
|
||||
Reference in new issue
Block a user