mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 10:26:21 +00:00
ref(backend) major refactor
- remove struct Post, use database schema and Map instead - add sub_title - replace NaiveDateTime with DateTime<Utc>
This commit is contained in:
9 files changed
+170
-159
No files matched your search
+1
-1
@@ -13,5 +13,5 @@ serde_json = "1.0.116"
|
|||||||
tokio = { version = "1.37.0", features = ["full"] }
|
tokio = { version = "1.37.0", features = ["full"] }
|
||||||
uuid = { version = "1.8.0", features = ["v4", "fast-rng", "serde"] }
|
uuid = { version = "1.8.0", features = ["v4", "fast-rng", "serde"] }
|
||||||
sqlx = { version = "0.7.4", features = ["runtime-tokio", "tls-native-tls", "postgres", "chrono", "uuid"] }
|
sqlx = { version = "0.7.4", features = ["runtime-tokio", "tls-native-tls", "postgres", "chrono", "uuid"] }
|
||||||
chrono = { version = "0.4.38", features = ["serde"] }
|
chrono = { version = "0.4.38", features = ["serde", "alloc"] }
|
||||||
regex = "1.10.4"
|
regex = "1.10.4"
|
||||||
@@ -3,7 +3,7 @@ use std::str::FromStr;
|
|||||||
use axum::extract::{Path, State};
|
use axum::extract::{Path, State};
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use axum::Json;
|
use axum::Json;
|
||||||
use chrono::NaiveDateTime;
|
use chrono::{DateTime, Utc};
|
||||||
use serde_json::{json, Map, Value};
|
use serde_json::{json, Map, Value};
|
||||||
use sqlx::{Error, PgPool, query, Row};
|
use sqlx::{Error, PgPool, query, Row};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
@@ -19,42 +19,34 @@ FROM Post
|
|||||||
WHERE post_id = $1;
|
WHERE post_id = $1;
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(Uuid::from_str(post_id.as_str()).expect("Failed to parse UUID"))
|
.bind(Uuid::from_str(post_id.as_str()).expect("Failed to parse UUID"))
|
||||||
.fetch_one(&db_connection)
|
.fetch_one(&db_connection)
|
||||||
.await {
|
.await
|
||||||
Ok(value) => { value }
|
{
|
||||||
|
Ok(value) => value,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return match e {
|
return match e {
|
||||||
Error::RowNotFound => {
|
Error::RowNotFound => Err((
|
||||||
Err((
|
StatusCode::BAD_REQUEST,
|
||||||
StatusCode::BAD_REQUEST,
|
Json::from(json!(format!("{{error: {}}}", e.to_string()))),
|
||||||
Json::from(json!(format!(
|
)),
|
||||||
"{{error: {}}}",
|
_ => Err((
|
||||||
e.to_string()
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
)))))
|
Json::from(json!(format!("{{error: {}}}", e.to_string()))),
|
||||||
}
|
)),
|
||||||
_ => {
|
|
||||||
Err((
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
Json::from(json!(format!(
|
|
||||||
"{{error: {}}}",
|
|
||||||
e.to_string()
|
|
||||||
)))))
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
let tags = query(
|
let tags = query(
|
||||||
r#"
|
r#"
|
||||||
SELECT tag FROM Tag WHERE post_id = $1;
|
SELECT tag FROM Tag WHERE post_id = $1;
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(Uuid::from_str(post_id.as_str()).expect("Failed to parse UUID"))
|
.bind(Uuid::from_str(post_id.as_str()).expect("Failed to parse UUID"))
|
||||||
.fetch_all(&db_connection)
|
.fetch_all(&db_connection)
|
||||||
.await
|
.await
|
||||||
.expect("Failed to execute db query");
|
.expect("Failed to execute db query");
|
||||||
|
|
||||||
let mut res = Map::new();
|
let mut res = Map::new();
|
||||||
|
|
||||||
@@ -72,11 +64,11 @@ SELECT tag FROM Tag WHERE post_id = $1;
|
|||||||
);
|
);
|
||||||
res.insert(
|
res.insert(
|
||||||
"last_update".to_string(),
|
"last_update".to_string(),
|
||||||
json!(meta.get::<NaiveDateTime, &str>("last_update")),
|
json!(meta.get::<DateTime<Utc>, &str>("last_update")),
|
||||||
);
|
);
|
||||||
res.insert(
|
res.insert(
|
||||||
"first_update".to_string(),
|
"first_update".to_string(),
|
||||||
json!(meta.get::<NaiveDateTime, &str>("first_update")),
|
json!(meta.get::<DateTime<Utc>, &str>("first_update")),
|
||||||
);
|
);
|
||||||
|
|
||||||
let tags: Vec<String> = tags
|
let tags: Vec<String> = tags
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use axum::extract::State;
|
use axum::extract::State;
|
||||||
use axum::Json;
|
use axum::Json;
|
||||||
use chrono::NaiveDateTime;
|
use chrono::{DateTime, Utc};
|
||||||
use serde_json::{json, Map, Value};
|
use serde_json::{json, Map, Value};
|
||||||
use sqlx::{PgPool, Row};
|
use sqlx::{PgPool, Row};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
@@ -22,8 +22,8 @@ pub async fn get_post_list(
|
|||||||
let post_id: Uuid = post.get("post_id");
|
let post_id: Uuid = post.get("post_id");
|
||||||
let title: String = post.get("title");
|
let title: String = post.get("title");
|
||||||
let summary: String = post.get("summary");
|
let summary: String = post.get("summary");
|
||||||
let last_update: NaiveDateTime = post.get("last_update");
|
let last_update: DateTime<Utc> = post.get("last_update");
|
||||||
let first_update: NaiveDateTime = post.get("first_update");
|
let first_update: DateTime<Utc> = post.get("first_update");
|
||||||
|
|
||||||
tmp.insert("post_id".to_string(), json!(post_id.to_string()));
|
tmp.insert("post_id".to_string(), json!(post_id.to_string()));
|
||||||
tmp.insert("title".to_string(), json!(title));
|
tmp.insert("title".to_string(), json!(title));
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use axum::extract::State;
|
use axum::extract::State;
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use axum::Json;
|
use axum::Json;
|
||||||
use serde_json::{json, Value};
|
use chrono::DateTime;
|
||||||
|
use serde_json::{json, Map, Value};
|
||||||
use sqlx::{PgPool, query};
|
use sqlx::{PgPool, query};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::markdown::meta::parse_meta;
|
use crate::constants::GLOBAL_TIME_FORMAT;
|
||||||
use crate::model::Post;
|
use crate::model::{post, POST_DB_MODEL, TAG_DB_MODEL};
|
||||||
|
|
||||||
pub async fn refresh(
|
pub async fn refresh(
|
||||||
State(db_connection): State<PgPool>,
|
State(db_connection): State<PgPool>,
|
||||||
@@ -40,67 +43,59 @@ pub async fn refresh(
|
|||||||
|
|
||||||
// generate new idx
|
// generate new idx
|
||||||
|
|
||||||
let post_list: Vec<Post> = md_list
|
let post_list: Vec<Map<_, _>> = md_list
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|post| Post::from_path(post).unwrap())
|
.map(|post| post::from_path(post).unwrap())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
query(
|
query(POST_DB_MODEL).execute(&db_connection).await.unwrap();
|
||||||
r##"
|
|
||||||
CREATE TABLE IF NOT EXISTS Post
|
|
||||||
(
|
|
||||||
post_id UUID PRIMARY KEY,
|
|
||||||
title VARCHAR(255) NOT NULL,
|
|
||||||
summary TEXT NOT NULL,
|
|
||||||
content TEXT NOT NULL,
|
|
||||||
last_update TIMESTAMP,
|
|
||||||
first_update TIMESTAMP NOT NULL
|
|
||||||
);
|
|
||||||
"##,
|
|
||||||
)
|
|
||||||
.execute(&db_connection)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
query(
|
query(TAG_DB_MODEL).execute(&db_connection).await.unwrap();
|
||||||
r##"
|
|
||||||
CREATE TABLE IF NOT EXISTS Tag
|
|
||||||
(
|
|
||||||
post_id UUID NOT NULL,
|
|
||||||
tag VARCHAR(255) NOT NULL
|
|
||||||
);
|
|
||||||
"##,
|
|
||||||
)
|
|
||||||
.execute(&db_connection)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// todo: concurrency
|
// todo: concurrency
|
||||||
for post in post_list {
|
for post in post_list {
|
||||||
let meta = parse_meta(&post.content);
|
let meta = &post["meta"];
|
||||||
|
|
||||||
// table Post
|
// table Post
|
||||||
match query(
|
match query(
|
||||||
r##"
|
r##"
|
||||||
INSERT INTO Post (post_id, title, summary, content, last_update, first_update)
|
INSERT INTO Post (post_id, title, summary, content, last_update, first_update, sub_title)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6)
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||||
ON CONFLICT (post_id)
|
|
||||||
DO UPDATE SET title = excluded.title,
|
|
||||||
summary = excluded.summary,
|
|
||||||
content = excluded.content,
|
|
||||||
last_update = excluded.last_update,
|
|
||||||
first_update = excluded.first_update
|
|
||||||
|
|
||||||
"##,
|
"##,
|
||||||
)
|
)
|
||||||
.bind(post.post_id)
|
.bind(Uuid::from_str(post["post_id"].as_str().unwrap()).unwrap())
|
||||||
.bind(post.title)
|
.bind(post["title"].as_str())
|
||||||
.bind(post.summary)
|
.bind(
|
||||||
.bind(&post.content)
|
post.get("summary")
|
||||||
.bind(post.last_update)
|
.and_then(|summary| summary.as_str())
|
||||||
.bind(post.first_update)
|
.unwrap_or(""),
|
||||||
.execute(&db_connection)
|
)
|
||||||
.await
|
.bind(post["content"].as_str())
|
||||||
|
.bind(
|
||||||
|
DateTime::parse_from_str(
|
||||||
|
post["last_update"]
|
||||||
|
.as_str()
|
||||||
|
.expect("Failed to parse `last_update`"),
|
||||||
|
GLOBAL_TIME_FORMAT,
|
||||||
|
)
|
||||||
|
.expect("Failed to convert `last_update` to NaiveDateTime"),
|
||||||
|
)
|
||||||
|
.bind(
|
||||||
|
DateTime::parse_from_str(
|
||||||
|
post["first_update"]
|
||||||
|
.as_str()
|
||||||
|
.expect("Failed to parse `first_update`"),
|
||||||
|
GLOBAL_TIME_FORMAT,
|
||||||
|
)
|
||||||
|
.expect("Failed to convert `first_update` to NaiveDateTime"),
|
||||||
|
)
|
||||||
|
.bind(
|
||||||
|
meta.get("sub_title")
|
||||||
|
.and_then(|sub_title| sub_title.as_str())
|
||||||
|
.unwrap_or(""),
|
||||||
|
)
|
||||||
|
.execute(&db_connection)
|
||||||
|
.await
|
||||||
{
|
{
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
@@ -114,7 +109,7 @@ ON CONFLICT (post_id)
|
|||||||
// table Tag
|
// table Tag
|
||||||
if let Some(tags_string) = meta.get("tags").to_owned() {
|
if let Some(tags_string) = meta.get("tags").to_owned() {
|
||||||
// parse tags string
|
// parse tags string
|
||||||
let parsed: Value = serde_json::from_str(tags_string).unwrap();
|
let parsed: Value = serde_json::from_str(tags_string.as_str().unwrap()).unwrap();
|
||||||
let string_array = parsed.as_array().unwrap();
|
let string_array = parsed.as_array().unwrap();
|
||||||
let tags: Vec<String> = string_array
|
let tags: Vec<String> = string_array
|
||||||
.iter()
|
.iter()
|
||||||
@@ -128,10 +123,10 @@ INSERT INTO Tag
|
|||||||
VALUES ($1, $2);
|
VALUES ($1, $2);
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(post.post_id)
|
.bind(Uuid::from_str(post["post_id"].as_str().unwrap()).unwrap())
|
||||||
.bind(tag)
|
.bind(tag)
|
||||||
.execute(&db_connection)
|
.execute(&db_connection)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
pub const GLOBAL_TIME_FORMAT: &str = "%Y-%m-%dT%H:%M:%S%z";
|
||||||
+2
-1
@@ -11,8 +11,9 @@ use crate::api::get::get_post_list::get_post_list;
|
|||||||
use crate::api::post::refresh::refresh;
|
use crate::api::post::refresh::refresh;
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
mod model;
|
mod constants;
|
||||||
mod markdown;
|
mod markdown;
|
||||||
|
mod model;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
|
/// return the meta info contained in the .md file front matter
|
||||||
pub fn parse_meta(md_content: &str) -> HashMap<String, String> {
|
pub fn parse_meta(md_content: &str) -> HashMap<String, String> {
|
||||||
let mut fields: HashMap<String, String> = HashMap::new();
|
let mut fields: HashMap<String, String> = HashMap::new();
|
||||||
let match_regex = Regex::new(r"(?s)---\n(.*\n)---\n").unwrap();
|
let match_regex = Regex::new(r"(?s)---\n(.*\n)---\n").unwrap();
|
||||||
@@ -41,11 +42,14 @@ mod meta_test {
|
|||||||
title: Hello World!
|
title: Hello World!
|
||||||
tags: [ "test", "hello_world" ]
|
tags: [ "test", "hello_world" ]
|
||||||
---
|
---
|
||||||
"##
|
"##,
|
||||||
);
|
);
|
||||||
dbg!(&parsed_meta);
|
dbg!(&parsed_meta);
|
||||||
assert_eq!(parsed_meta.get("title"), Some(&"Hello World!".to_string()));
|
assert_eq!(parsed_meta.get("title"), Some(&"Hello World!".to_string()));
|
||||||
assert_eq!(parsed_meta.get("tags"), Some(&r#"[ "test", "hello_world" ]"#.to_string()));
|
assert_eq!(
|
||||||
|
parsed_meta.get("tags"),
|
||||||
|
Some(&r#"[ "test", "hello_world" ]"#.to_string())
|
||||||
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+19
-15
@@ -1,18 +1,22 @@
|
|||||||
use chrono::NaiveDateTime;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use sqlx::FromRow;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub mod post;
|
pub mod post;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, FromRow)]
|
pub const POST_DB_MODEL: &str = r##"
|
||||||
pub struct Post {
|
CREATE TABLE IF NOT EXISTS Post
|
||||||
pub post_id: Uuid,
|
(
|
||||||
pub title: String,
|
post_id UUID PRIMARY KEY,
|
||||||
pub summary: String,
|
title VARCHAR(255) NOT NULL,
|
||||||
pub content: String,
|
summary TEXT,
|
||||||
pub last_update: NaiveDateTime,
|
sub_title TEXT,
|
||||||
pub first_update: NaiveDateTime,
|
content TEXT NOT NULL,
|
||||||
}
|
last_update TIMESTAMPTZ NOT NULL,
|
||||||
|
first_update TIMESTAMPTZ NOT NULL
|
||||||
|
);
|
||||||
|
"##;
|
||||||
|
|
||||||
|
pub const TAG_DB_MODEL: &str = r##"
|
||||||
|
CREATE TABLE IF NOT EXISTS Tag
|
||||||
|
(
|
||||||
|
post_id UUID NOT NULL,
|
||||||
|
tag VARCHAR(255) NOT NULL
|
||||||
|
);
|
||||||
|
"##;
|
||||||
+67
-53
@@ -4,67 +4,81 @@ use std::process::Command;
|
|||||||
use std::str;
|
use std::str;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use chrono::{NaiveDate, NaiveDateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
|
use serde_json::{Map, Value};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::constants::GLOBAL_TIME_FORMAT;
|
||||||
use crate::markdown::meta::parse_meta;
|
use crate::markdown::meta::parse_meta;
|
||||||
use crate::model::Post;
|
|
||||||
|
|
||||||
impl Post {
|
pub fn from_path(path: String) -> Result<Map<String, Value>, Box<dyn Error>> {
|
||||||
pub fn from_path(path: String) -> Result<Self, Box<dyn Error>> {
|
let mut res = Map::new();
|
||||||
let md_content = fs::read_to_string(&path)?;
|
|
||||||
let meta = parse_meta(&md_content);
|
|
||||||
|
|
||||||
let post_id = match meta.get("id") {
|
let md_content = fs::read_to_string(&path)?;
|
||||||
None => { Uuid::new_v4() }
|
res.insert("content".to_string(), Value::from(md_content.to_owned()));
|
||||||
Some(id) => { Uuid::from_str(id.as_str()).expect("Invalid UUID String") }
|
|
||||||
};
|
|
||||||
|
|
||||||
let title = match meta.get("title") {
|
let meta = parse_meta(&md_content);
|
||||||
None => "Title Not Found".to_string(),
|
let meta_map: Map<String, Value> = meta
|
||||||
Some(title) => title.to_owned(),
|
.clone()
|
||||||
};
|
.into_iter()
|
||||||
let summary = match meta.get("summary") {
|
.map(|(k, v)| (k, Value::from(v)))
|
||||||
None => "".to_string(),
|
.collect();
|
||||||
Some(summary) => summary.to_owned(),
|
res.insert("meta".to_string(), Value::from(meta_map));
|
||||||
};
|
|
||||||
|
|
||||||
let git_output = Command::new("git")
|
let post_id = match meta.get("id") {
|
||||||
.arg("--no-pager")
|
None => Uuid::new_v4(),
|
||||||
.arg("log")
|
Some(id) => Uuid::from_str(id.as_str()).expect("Invalid UUID String"),
|
||||||
.arg("-1")
|
};
|
||||||
.arg("--format=%cI")
|
res.insert("post_id".to_string(), Value::from(post_id.to_string()));
|
||||||
.arg("--")
|
|
||||||
.current_dir(env::var("GIT_WORK_DIR")?)
|
|
||||||
.arg(&path)
|
|
||||||
.output()
|
|
||||||
.expect("Failed to execute git command");
|
|
||||||
|
|
||||||
let last_update = if git_output.status.success() {
|
let title = match meta.get("title") {
|
||||||
NaiveDateTime::parse_from_str(
|
None => "Title Not Found".to_string(),
|
||||||
str::from_utf8(&git_output.stdout).unwrap().trim_end(),
|
Some(title) => title.to_owned(),
|
||||||
"%Y-%m-%dT%H:%M:%S%z",
|
};
|
||||||
)
|
res.insert("title".to_string(), Value::from(title));
|
||||||
.unwrap_or_else(|_| Utc::now().naive_utc())
|
|
||||||
} else {
|
|
||||||
Utc::now().naive_utc()
|
|
||||||
};
|
|
||||||
|
|
||||||
let first_update: NaiveDateTime = match meta.get("date") {
|
let summary = match meta.get("summary") {
|
||||||
None => Utc::now().naive_utc(),
|
None => "".to_string(),
|
||||||
Some(date) => NaiveDateTime::from(
|
Some(summary) => summary.to_owned(),
|
||||||
NaiveDate::parse_from_str(date, "%Y-%m-%d%z")
|
};
|
||||||
.expect("Failed to parse first_update time"),
|
res.insert("summary".to_string(), Value::from(summary));
|
||||||
),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Post {
|
let git_output = Command::new("git")
|
||||||
post_id,
|
.arg("--no-pager")
|
||||||
title,
|
.arg("log")
|
||||||
summary,
|
.arg("-1")
|
||||||
content: md_content,
|
.arg("--format=%cI")
|
||||||
last_update,
|
.arg("--")
|
||||||
first_update,
|
.current_dir(env::var("GIT_WORK_DIR")?)
|
||||||
})
|
.arg(&path)
|
||||||
}
|
.output()
|
||||||
|
.expect("Failed to execute git command");
|
||||||
|
let last_update: DateTime<Utc> = if git_output.status.success() {
|
||||||
|
chrono::DateTime::parse_from_str(
|
||||||
|
str::from_utf8(&git_output.stdout).unwrap().trim_end(),
|
||||||
|
GLOBAL_TIME_FORMAT,
|
||||||
|
)
|
||||||
|
.unwrap_or_else(|_| DateTime::from(Utc::now()))
|
||||||
|
.to_utc()
|
||||||
|
} else {
|
||||||
|
Utc::now().to_utc()
|
||||||
|
};
|
||||||
|
res.insert(
|
||||||
|
"last_update".to_string(),
|
||||||
|
Value::from(last_update.format(GLOBAL_TIME_FORMAT).to_string()),
|
||||||
|
);
|
||||||
|
|
||||||
|
let first_update: DateTime<Utc> = match meta.get("date") {
|
||||||
|
None => 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"),
|
||||||
|
),
|
||||||
|
};
|
||||||
|
res.insert(
|
||||||
|
"first_update".to_string(),
|
||||||
|
Value::from(first_update.format(GLOBAL_TIME_FORMAT).to_string()),
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(res)
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user