mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 18:36:21 +00:00
Compare commits
61
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1f27e0bddc | ||
|
|
45010f030c | ||
|
|
06a4ab8cf7 | ||
|
|
f491bc99a8 | ||
|
|
d15cdf216a | ||
|
|
2f6cafa543 | ||
|
|
dc2c294617 | ||
|
|
80a164625b | ||
|
|
2c11af4a25 | ||
|
|
e83cea1d42 | ||
|
|
71af26a09c | ||
|
|
15a88074e9 | ||
|
|
5bf6d24733 | ||
|
|
6a6af722c3 | ||
|
|
512197d87a | ||
|
|
3c16bf5100 | ||
|
|
ff4245a856 | ||
|
|
9270abc4b7 | ||
|
|
b11b19b0f5 | ||
|
|
7d988d380b | ||
|
|
8ba348a93e | ||
|
|
10f14312bd | ||
|
|
27d2c86106 | ||
|
|
12d9ddf8b6 | ||
|
|
ac8a2359b0 | ||
|
|
40abec2e90 | ||
|
|
1038351f4a | ||
|
|
b748764d79 | ||
|
|
ac5f30151f | ||
|
|
d54996b690 | ||
|
|
7b84a63a14 | ||
|
|
7cc44522fa | ||
|
|
2045d380c1 | ||
|
|
dc781bc2ef | ||
|
|
2e32297d5e | ||
|
|
5e8b1ff35d | ||
|
|
0c6325d5f2 | ||
|
|
ca86dd236e | ||
|
|
8358a8ae33 | ||
|
|
84ec754c65 | ||
|
|
bd90cd94d1 | ||
|
|
5da9ca3ae1 | ||
|
|
eef32ad695 | ||
|
|
acd0525513 | ||
|
|
8938c96409 | ||
|
|
6da6231c92 | ||
|
|
19f84d2188 | ||
|
|
20c2964242 | ||
|
|
eac84307bf | ||
|
|
a0dc40d2ca | ||
|
|
cbc831bb70 | ||
|
|
bacaea80a3 | ||
|
|
99a19d8d9e | ||
|
|
7f400470ff | ||
|
|
e26003f430 | ||
|
|
c7e3b12856 | ||
|
|
a123ff9f3f | ||
|
|
d98aa3f745 | ||
|
|
6f45a140ba | ||
|
|
3dab27323a | ||
|
|
3baf011be9 |
No files matched your search
@@ -1,20 +1,19 @@
|
|||||||
# postgres settings
|
# postgres settings
|
||||||
DB_HOST=localhost
|
DB_HOST=localhost
|
||||||
DB_PORT=5432
|
DB_PORT=5432
|
||||||
POSTGRES_PASSWORD=
|
#POSTGRES_PASSWORD=
|
||||||
POSTGRES_USER=
|
POSTGRES_USER=nocturne
|
||||||
POSTGRES_DB=nocturne
|
POSTGRES_DB=nocturne
|
||||||
|
POSTGRES_HOST=postgres
|
||||||
|
|
||||||
# backend settings
|
# backend settings
|
||||||
GIT_WORK_DIR=../blog_posts_git
|
#GIT_WORK_DIR=../blog_posts_git
|
||||||
LOG_ROOT_DIR=/var/log/nocturne
|
#LOG_ROOT_DIR=/var/log/nocturne
|
||||||
|
#BLOG_ENDPOINT=https://github.com/Lumither/blog-posts.git
|
||||||
BLOG_ENDPOINT=https://github.com/Lumither/blog-posts.git
|
BLOG_GIT_URL=https://github.com/Lumither/blog-posts.git
|
||||||
WORD_DIR=~/.nocturne
|
WORK_DIR=/nocturne
|
||||||
BACKEND_PORT=3001
|
#BACKEND_PORT=3001
|
||||||
|
|
||||||
# frontend settings
|
# frontend settings
|
||||||
API_LOCAL_URL=http://localhost:3001
|
API_LOCAL_URL=http://backend:3001
|
||||||
API_REMOTE_URL=https://lumither.com/api
|
API_REMOTE_URL=https://lumither.com/api
|
||||||
|
|
||||||
|
|
||||||
+10
-2
@@ -2,8 +2,16 @@
|
|||||||
.env.*
|
.env.*
|
||||||
/container/*/.env
|
/container/*/.env
|
||||||
|
|
||||||
# idea related
|
# IDE related
|
||||||
.idea
|
.idea
|
||||||
|
.vscode
|
||||||
|
|
||||||
# exclude post repo
|
# macOS system files
|
||||||
|
.DS_Store
|
||||||
|
**/.DS_Store
|
||||||
|
|
||||||
|
# exclude content repo
|
||||||
/blog_posts_git
|
/blog_posts_git
|
||||||
|
|
||||||
|
# rustc ICE dump
|
||||||
|
rustc-ice-*.txt
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
ddev:
|
ARCH := $(shell uname -m)
|
||||||
docker-compose -f dev.compose.yaml up --build
|
ifeq ($(ARCH), arm64)
|
||||||
|
ARCH := aarch64
|
||||||
|
endif
|
||||||
|
|
||||||
dclean:
|
.PHONY: up
|
||||||
docker-compose -f dev.compose.yaml rm -fsv
|
clean:
|
||||||
|
echo "todo"
|
||||||
|
|
||||||
@@ -1,36 +1,10 @@
|
|||||||
# `Nocturne: personal website & CMS`
|
# `Nocturne: Personal Website`
|
||||||
|
|
||||||
This is the code repo of my personal website, and [here](https://github.com/Lumither/blog-posts/) is the link of the
|
This is the code repo of my [personal website](https://lumither.com),
|
||||||
content repo.
|
and [here](https://github.com/Lumither/blog-posts/) is the link to the
|
||||||
|
content (blog posts etc.) repo.
|
||||||
|
|
||||||
Powered by Next.js and Rust, built with passion and love
|
Powered by Next.js and Rust, built with passion and love
|
||||||
|
|
||||||
<p align="center">
|
> [!NOTE]
|
||||||
<img src="https://avatars.githubusercontent.com/u/46409277?v=4" alt="GitHub Avatar"/>
|
> This is an unstable branch, checkout `release` for the (guaranteed) working version
|
||||||
</p>
|
|
||||||
|
|
||||||
*will add more here in the future
|
|
||||||
|
|
||||||
## Nav:
|
|
||||||
|
|
||||||
<todo>
|
|
||||||
|
|
||||||
## Todos:
|
|
||||||
|
|
||||||
### Backend
|
|
||||||
|
|
||||||
- [ ] Add: log system
|
|
||||||
- [ ] Add: search api
|
|
||||||
- [ ] Add: tag filter (api)
|
|
||||||
- [ ] Abandon: UUID mechanism
|
|
||||||
- [ ] Fix: db connection issue
|
|
||||||
|
|
||||||
### Frontend
|
|
||||||
|
|
||||||
- [ ] Page: Home
|
|
||||||
- [ ] Page: About
|
|
||||||
- [ ] Page: Tags
|
|
||||||
- [ ] Page: Archive
|
|
||||||
- [ ] Page: Search
|
|
||||||
- [ ] Add: Right sidebar
|
|
||||||
|
|
||||||
@@ -1 +1,3 @@
|
|||||||
/target
|
/target
|
||||||
|
|
||||||
|
/Cargo.lock
|
||||||
+4
-4
@@ -1,7 +1,7 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
resolver = "2"
|
resolver = "3"
|
||||||
members = [
|
members = [
|
||||||
"api_server",
|
"server",
|
||||||
"constants",
|
"macros",
|
||||||
"markdown"
|
"markdown",
|
||||||
]
|
]
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "api_server"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
constants = { path = "../constants" }
|
|
||||||
chrono = "0.4.38"
|
|
||||||
tracing = "0.1.40"
|
|
||||||
tracing-subscriber = "0.3.18"
|
|
||||||
tracing-appender = "0.2.3"
|
|
||||||
dotenv = "0.15.0"
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
use crate::logger;
|
|
||||||
use std::env;
|
|
||||||
use tracing_appender::non_blocking::WorkerGuard;
|
|
||||||
|
|
||||||
pub fn logger_init() -> (WorkerGuard, WorkerGuard) {
|
|
||||||
logger::init()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load_env() {
|
|
||||||
if let Some(env_file) = env::args().nth(1) {
|
|
||||||
match dotenv::from_filename(&env_file) {
|
|
||||||
Ok(_) => {
|
|
||||||
println!("[info] env loaded from {}, starting up", env_file);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
panic!("[fatal] failed to read {}: {}", env_file, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
println!("[info] no .env file referred, starting up")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
use std::{env, io};
|
|
||||||
|
|
||||||
use chrono::Local;
|
|
||||||
use tracing::info;
|
|
||||||
use tracing_appender::non_blocking::WorkerGuard;
|
|
||||||
use tracing_appender::{non_blocking, rolling};
|
|
||||||
use tracing_subscriber::fmt::format::Writer;
|
|
||||||
use tracing_subscriber::fmt::time::FormatTime;
|
|
||||||
use tracing_subscriber::fmt::MakeWriter;
|
|
||||||
use tracing_subscriber::layer::SubscriberExt;
|
|
||||||
use tracing_subscriber::{fmt, Registry};
|
|
||||||
|
|
||||||
struct LocalTimer;
|
|
||||||
|
|
||||||
impl FormatTime for LocalTimer {
|
|
||||||
fn format_time(&self, w: &mut Writer<'_>) -> std::fmt::Result {
|
|
||||||
write!(
|
|
||||||
w,
|
|
||||||
"{}",
|
|
||||||
Local::now().format(constants::time::DEFAULT_TIME_FORMAT)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type _LogWriter = dyn for<'writer> MakeWriter<'writer, Writer = dyn io::Write + 'static> + 'static;
|
|
||||||
|
|
||||||
pub fn init() -> (WorkerGuard, WorkerGuard) {
|
|
||||||
let log_path = env::var("LOG_ROOT_DIR").unwrap_or_else(|e| {
|
|
||||||
panic!("[fatal] failed to load LOG_ROOT_DIR: {}", e);
|
|
||||||
});
|
|
||||||
|
|
||||||
let log_path = format!("{}/backend", log_path);
|
|
||||||
|
|
||||||
let (info_log_writer, info_guard) = non_blocking(rolling::never(&log_path, "info.log"));
|
|
||||||
|
|
||||||
let (err_log_writer, error_guard) = non_blocking(rolling::never(&log_path, "error.log"));
|
|
||||||
|
|
||||||
let log_format = tracing_subscriber::fmt::format()
|
|
||||||
.with_level(true)
|
|
||||||
.with_target(true)
|
|
||||||
.with_source_location(true)
|
|
||||||
.with_timer(LocalTimer);
|
|
||||||
|
|
||||||
let subscribers = Registry::default()
|
|
||||||
.with(
|
|
||||||
fmt::Layer::default()
|
|
||||||
.with_writer(info_log_writer)
|
|
||||||
.with_ansi(false),
|
|
||||||
)
|
|
||||||
.with(
|
|
||||||
fmt::Layer::default()
|
|
||||||
.with_writer(err_log_writer)
|
|
||||||
.with_ansi(false),
|
|
||||||
)
|
|
||||||
.with(
|
|
||||||
fmt::Layer::default()
|
|
||||||
.with_writer(io::stdout)
|
|
||||||
.with_ansi(true)
|
|
||||||
.event_format(log_format),
|
|
||||||
);
|
|
||||||
|
|
||||||
tracing::subscriber::set_global_default(subscribers)
|
|
||||||
.expect("[fatal] Failed to setup logging system");
|
|
||||||
|
|
||||||
info!("Module Started: Logger");
|
|
||||||
|
|
||||||
(info_guard, error_guard)
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
mod init;
|
|
||||||
mod logger;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
init::load_env();
|
|
||||||
let _guards = init::logger_init();
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "backend"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
axum = "0.7.5"
|
|
||||||
dotenv = "0.15.0"
|
|
||||||
serde = { version = "1.0.199", features = ["derive"] }
|
|
||||||
serde_json = "1.0.116"
|
|
||||||
tokio = { version = "1.37.0", features = ["full"] }
|
|
||||||
uuid = { version = "1.8.0", features = ["v4", "fast-rng", "serde"] }
|
|
||||||
sqlx = { version = "0.7.4", features = ["runtime-tokio", "tls-native-tls", "postgres", "chrono", "uuid"] }
|
|
||||||
chrono = { version = "0.4.38", features = ["serde", "alloc"] }
|
|
||||||
regex = "1.10.4"
|
|
||||||
futures = { version = "0.3.30", features = ["executor"] }
|
|
||||||
tracing = "0.1.40"
|
|
||||||
tracing-subscriber = "0.3.18"
|
|
||||||
tracing-appender = "0.2.3"
|
|
||||||
log = "0.4.21"
|
|
||||||
sha2 = "0.10.8"
|
|
||||||
thiserror = "1.0.63"
|
|
||||||
base64 = "0.22.1"
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
use crate::constants::PAGE_SIZE;
|
|
||||||
use axum::extract::State;
|
|
||||||
use axum::Json;
|
|
||||||
use serde_json::{json, Value};
|
|
||||||
use sqlx::{query, PgPool, Row};
|
|
||||||
use tracing::error;
|
|
||||||
|
|
||||||
pub async fn get_page_count(
|
|
||||||
State(db_connection): State<PgPool>,
|
|
||||||
) -> Result<Json<Value>, Json<Value>> {
|
|
||||||
match query("select cast(ceil(count(*) / $1) as integer) as res from post")
|
|
||||||
.bind(PAGE_SIZE as f32)
|
|
||||||
.fetch_one(&db_connection)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(res) => {
|
|
||||||
let res: i32 = res.get("res");
|
|
||||||
Ok(Json::from(json!({"res": res})))
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!("Failed to get blog page count: {}", e.to_string());
|
|
||||||
Err(Json::from(json!({"error": e.to_string()})))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,154 +0,0 @@
|
|||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use axum::{
|
|
||||||
extract::{Path, State},
|
|
||||||
http::StatusCode,
|
|
||||||
Json,
|
|
||||||
};
|
|
||||||
use serde_json::{json, Value};
|
|
||||||
use sqlx::{Error, PgPool, query, Row};
|
|
||||||
use tracing::error;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub async fn get_post(
|
|
||||||
State(db_connection): State<PgPool>,
|
|
||||||
Path(post_id): Path<String>,
|
|
||||||
) -> Result<Json<Value>, (StatusCode, Json<Value>)> {
|
|
||||||
let post_id = match Uuid::from_str(post_id.as_str()) {
|
|
||||||
Ok(id) => id,
|
|
||||||
Err(e) => {
|
|
||||||
error!(
|
|
||||||
"Failed to parse UUID <{}>: {}",
|
|
||||||
post_id.as_str(),
|
|
||||||
e.to_string()
|
|
||||||
);
|
|
||||||
return Err((
|
|
||||||
StatusCode::BAD_REQUEST,
|
|
||||||
Json::from(json!(("error", e.to_string()))),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let post = match query(
|
|
||||||
r#"
|
|
||||||
SELECT jsonb_strip_nulls(to_jsonb(t.*)) - 'post_id' AS post
|
|
||||||
FROM post t
|
|
||||||
WHERE post_id = $1;
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.bind(post_id)
|
|
||||||
.fetch_one(&db_connection)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(value) => value.get::<Value, _>("post"),
|
|
||||||
Err(e) => {
|
|
||||||
error!(
|
|
||||||
"Database error on Reading `base post information` from table `post`: <{}>: {}",
|
|
||||||
post_id.to_string(),
|
|
||||||
e.to_string()
|
|
||||||
);
|
|
||||||
return match e {
|
|
||||||
Error::RowNotFound => Err((
|
|
||||||
StatusCode::BAD_REQUEST,
|
|
||||||
Json::from(json!({"error": e.to_string()})),
|
|
||||||
)),
|
|
||||||
_ => Err((
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
Json::from(json!({"error": e.to_string()})),
|
|
||||||
)),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let tags: Vec<_> = match query("SELECT tag FROM Tag WHERE post_id = $1;")
|
|
||||||
.bind(post_id)
|
|
||||||
.fetch_all(&db_connection)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(tags) => tags.iter().map(|tag| tag.get::<String, _>("tag")).collect(),
|
|
||||||
Err(e) => {
|
|
||||||
error!("Database query error :{}", e.to_string());
|
|
||||||
return Err((
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
Json::from(json!({"error": e.to_string()})),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let meta = match query(
|
|
||||||
r#"
|
|
||||||
SELECT jsonb_strip_nulls(to_jsonb(t.*)) - 'post_id' AS meta
|
|
||||||
FROM meta t
|
|
||||||
WHERE post_id = $1;
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.bind(post_id)
|
|
||||||
.fetch_one(&db_connection)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(value) => value.get::<Value, _>("meta"),
|
|
||||||
Err(e) => {
|
|
||||||
error!(
|
|
||||||
"Database error on Reading `{}` from table `{}`: <{}>: {}",
|
|
||||||
"post meta data",
|
|
||||||
"meta",
|
|
||||||
post_id.to_string(),
|
|
||||||
e.to_string()
|
|
||||||
);
|
|
||||||
return Err((
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
Json::from(json!({"error": e.to_string()})),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let adjacent = match query(r"
|
|
||||||
WITH target_post AS (SELECT t.date FROM meta t WHERE post_id = $1),
|
|
||||||
prev AS (SELECT post_id, t.title, t.sub_title, t.header_img
|
|
||||||
FROM meta t
|
|
||||||
WHERE t.date < (SELECT date FROM target_post)
|
|
||||||
ORDER BY t.date DESC
|
|
||||||
LIMIT 1),
|
|
||||||
next AS (SELECT post_id, t.title, t.sub_title, t.header_img
|
|
||||||
FROM meta t
|
|
||||||
WHERE t.date > (SELECT date FROM target_post)
|
|
||||||
ORDER BY t.date
|
|
||||||
LIMIT 1)
|
|
||||||
SELECT json_strip_nulls(json_build_object(
|
|
||||||
'prev',
|
|
||||||
(SELECT json_build_object('id', post_id, 'title', title, 'sub_title', sub_title, 'header_img', header_img)
|
|
||||||
FROM prev),
|
|
||||||
'next',
|
|
||||||
(SELECT json_build_object('id', post_id, 'title', title, 'sub_title', sub_title, 'header_img', header_img)
|
|
||||||
FROM next)
|
|
||||||
)) AS result;
|
|
||||||
"
|
|
||||||
)
|
|
||||||
.bind(post_id)
|
|
||||||
.fetch_one(&db_connection)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(res) => {res.get::<Value, _>("result")}
|
|
||||||
Err(e) => {
|
|
||||||
error!(
|
|
||||||
"Database error on Reading `{}` from table `{}`: <{}>: {}",
|
|
||||||
"adjoint posts data",
|
|
||||||
"meta",
|
|
||||||
post_id.to_string(),
|
|
||||||
e.to_string()
|
|
||||||
);
|
|
||||||
return Err((
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
Json::from(json!({"error": e.to_string()})),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut res = post.as_object().unwrap().to_owned();
|
|
||||||
res.insert("tags".to_string(), tags.into());
|
|
||||||
res.insert("meta".to_string(), meta);
|
|
||||||
res.insert("adj".to_string(), adjacent);
|
|
||||||
|
|
||||||
Ok(Json::from(json!(res)))
|
|
||||||
}
|
|
||||||
@@ -1,87 +0,0 @@
|
|||||||
use crate::constants::PAGE_SIZE;
|
|
||||||
use axum::{extract::Query, extract::State, http::StatusCode, Json};
|
|
||||||
use chrono::{DateTime, Utc};
|
|
||||||
use futures::future::join_all;
|
|
||||||
use serde::Deserialize;
|
|
||||||
use serde_json::{json, Map, Value};
|
|
||||||
use sqlx::{postgres::PgRow, query, PgPool, Row};
|
|
||||||
use tracing::error;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct Params {
|
|
||||||
page: Option<u32>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_post_list(
|
|
||||||
State(db_connection): State<PgPool>,
|
|
||||||
Query(params): Query<Params>,
|
|
||||||
) -> Result<Json<Value>, (StatusCode, Json<Value>)> {
|
|
||||||
let search_param_page = params.page.unwrap_or(1);
|
|
||||||
if search_param_page < 1 {
|
|
||||||
return Err((
|
|
||||||
StatusCode::BAD_REQUEST,
|
|
||||||
Json::from(json!({"error": "`page` should be greater than 0"})),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
let offset = PAGE_SIZE * (search_param_page - 1);
|
|
||||||
|
|
||||||
let post_list = sqlx::query("SELECT * FROM post ORDER BY first_update DESC LIMIT $1 OFFSET $2")
|
|
||||||
.bind(PAGE_SIZE as i32)
|
|
||||||
.bind(offset as i32)
|
|
||||||
.fetch_all(&db_connection)
|
|
||||||
.await;
|
|
||||||
let post_list = match post_list {
|
|
||||||
Ok(post_list) => post_list,
|
|
||||||
Err(e) => {
|
|
||||||
error!(
|
|
||||||
"Database query error on fetching post list: {}",
|
|
||||||
e.to_string()
|
|
||||||
);
|
|
||||||
return Err((
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
Json::from(json!({"error": e.to_string()})),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut res = Map::new();
|
|
||||||
let posts: Vec<_> = post_list
|
|
||||||
.into_iter()
|
|
||||||
.map(|post| analyze_post(&db_connection, post))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let posts = join_all(posts).await;
|
|
||||||
|
|
||||||
res.insert("posts".to_string(), Value::from(posts));
|
|
||||||
Ok(Json::from(Value::from(res)))
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn analyze_post(db_connection: &PgPool, post: PgRow) -> Value {
|
|
||||||
let post_id: Uuid = post.get("post_id");
|
|
||||||
let tags = fetch_post_tags(db_connection, post_id).await;
|
|
||||||
|
|
||||||
json!({
|
|
||||||
"post_id": post_id.to_string(),
|
|
||||||
"title": post.get::<String, _>("title"),
|
|
||||||
"category": post.get::<String, _>("category"),
|
|
||||||
"header_img": post.get::<String, _>("header_img"),
|
|
||||||
"sub_title": post.get::<String, _>("sub_title"),
|
|
||||||
"summary": post.get::<String, _>("summary"),
|
|
||||||
"last_update": post.get::<DateTime<Utc>, _>("last_update"),
|
|
||||||
"first_update": post.get::<DateTime<Utc>, _>("first_update"),
|
|
||||||
"tags": tags,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn fetch_post_tags(db_connection: &PgPool, post_id: Uuid) -> Vec<String> {
|
|
||||||
match query("SELECT tag FROM Tag WHERE post_id = $1")
|
|
||||||
.bind(post_id)
|
|
||||||
.fetch_all(db_connection)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(res) => res.iter().map(|tag| tag.get::<String, _>("tag")).collect(),
|
|
||||||
Err(_) => vec![],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
pub mod get_page_count;
|
|
||||||
pub mod get_post;
|
|
||||||
pub mod get_post_list;
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
pub mod get;
|
|
||||||
pub mod post;
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
pub mod refresh;
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
|
||||||
pub enum PostIdxError {
|
|
||||||
#[error("Failed to parse UUID <{}>: {}", id, err_msg)]
|
|
||||||
InvalidUUID { id: String, err_msg: String },
|
|
||||||
|
|
||||||
#[error(
|
|
||||||
"Failed to parse `last update time` for <{}> when creating post index: {}",
|
|
||||||
id,
|
|
||||||
err_msg
|
|
||||||
)]
|
|
||||||
InvalidLastUpdate { id: String, err_msg: String },
|
|
||||||
|
|
||||||
#[error(
|
|
||||||
"Failed to parse `first update time` for <{}> when creating post index: {}",
|
|
||||||
id,
|
|
||||||
err_msg
|
|
||||||
)]
|
|
||||||
InvalidFirstUpdate { id: String, err_msg: String },
|
|
||||||
|
|
||||||
#[error(
|
|
||||||
"Failed to parse `tag` for <{}> when creating post index: <{}>: {}",
|
|
||||||
id,
|
|
||||||
invalid_string,
|
|
||||||
err_msg
|
|
||||||
)]
|
|
||||||
InvalidTagString {
|
|
||||||
id: String,
|
|
||||||
invalid_string: String,
|
|
||||||
err_msg: String,
|
|
||||||
},
|
|
||||||
|
|
||||||
#[error(
|
|
||||||
"Database error on writing `{}` to table `{}`: <{}>: {}",
|
|
||||||
data_desc,
|
|
||||||
db_table,
|
|
||||||
id,
|
|
||||||
err_msg
|
|
||||||
)]
|
|
||||||
DBWriteFailure {
|
|
||||||
data_desc: String,
|
|
||||||
db_table: String,
|
|
||||||
id: String,
|
|
||||||
err_msg: String,
|
|
||||||
},
|
|
||||||
|
|
||||||
#[error(
|
|
||||||
"Database error on Reading `{}` from table `{}`: <{}>: {}",
|
|
||||||
data_desc,
|
|
||||||
db_table,
|
|
||||||
id,
|
|
||||||
err_msg
|
|
||||||
)]
|
|
||||||
DBReadFailure {
|
|
||||||
data_desc: String,
|
|
||||||
db_table: String,
|
|
||||||
id: String,
|
|
||||||
err_msg: String,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
mod errors;
|
|
||||||
pub mod refresh;
|
|
||||||
mod utils;
|
|
||||||
@@ -1,192 +0,0 @@
|
|||||||
use std::path::Path;
|
|
||||||
use std::str::FromStr;
|
|
||||||
use std::{env, fs};
|
|
||||||
|
|
||||||
use axum::extract::{Query, State};
|
|
||||||
use axum::http::StatusCode;
|
|
||||||
use axum::Json;
|
|
||||||
use futures::{stream, StreamExt};
|
|
||||||
use serde::Deserialize;
|
|
||||||
use serde_json::{json, Map, Value};
|
|
||||||
use sha2::{Digest, Sha256};
|
|
||||||
use sqlx::{query, PgPool};
|
|
||||||
use tracing::error;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
use crate::api::post::errors::refresh::PostIdxError;
|
|
||||||
use crate::api::post::utils::{build_post_idx, post};
|
|
||||||
use crate::model::blog::{POST_BASE_MODEL, POST_HASH_MODEL, POST_META_MODEL, TAG_MODEL};
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct Params {
|
|
||||||
/// refresh level
|
|
||||||
level: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn refresh(
|
|
||||||
State(db_connection): State<PgPool>,
|
|
||||||
Query(params): Query<Params>,
|
|
||||||
) -> Result<StatusCode, (StatusCode, Json<Value>)> {
|
|
||||||
let is_force_refresh = params.level.unwrap_or("".to_string()) == "force";
|
|
||||||
|
|
||||||
// search post
|
|
||||||
let data_repo_dir = match env::var("GIT_WORK_DIR") {
|
|
||||||
Ok(value) => value,
|
|
||||||
Err(_) => {
|
|
||||||
return Err((
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
Json::from(json!((
|
|
||||||
"error",
|
|
||||||
"error at parsing environment variable: GIT_WORK_DIR"
|
|
||||||
))),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let post_dir = Path::new(&data_repo_dir).join("posts");
|
|
||||||
|
|
||||||
if is_force_refresh {
|
|
||||||
if let Err(e) = query("DROP TABLE IF EXISTS hash, meta, post, tag;")
|
|
||||||
.execute(&db_connection)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
error!(
|
|
||||||
"DB query error when deleting table: `hash`, `meta`, `post`, `tag`: {}",
|
|
||||||
e.to_string()
|
|
||||||
);
|
|
||||||
return Err((
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
Json::from(json!(("error", e.to_string()))),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// generate post idx
|
|
||||||
let post_path = search_md(&post_dir);
|
|
||||||
|
|
||||||
let post_list_with_hash: Vec<(Map<_, _>, Vec<_>)> = post_path
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|post_path| match post::from_path(&post_path) {
|
|
||||||
Ok(post) => match fs::read(&post_path) {
|
|
||||||
Ok(bytes) => {
|
|
||||||
let mut hasher = Sha256::new();
|
|
||||||
hasher.update(bytes);
|
|
||||||
let hash = hasher.finalize().to_vec();
|
|
||||||
Some((post, hash))
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!(
|
|
||||||
"Failed to read file while hashing the post <{}>, skipping: {}",
|
|
||||||
post_path,
|
|
||||||
e.to_string()
|
|
||||||
);
|
|
||||||
None
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(e) => {
|
|
||||||
error!(
|
|
||||||
"Failed to parse post <{}>, skipping: {}",
|
|
||||||
post_path,
|
|
||||||
e.to_string()
|
|
||||||
);
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
// db create
|
|
||||||
query(POST_BASE_MODEL)
|
|
||||||
.execute(&db_connection)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
query(POST_META_MODEL)
|
|
||||||
.execute(&db_connection)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
query(TAG_MODEL).execute(&db_connection).await.unwrap();
|
|
||||||
query(POST_HASH_MODEL)
|
|
||||||
.execute(&db_connection)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let res: Vec<_> = stream::iter(post_list_with_hash)
|
|
||||||
.map(|(post, hash)| {
|
|
||||||
let db_clone = db_connection.clone();
|
|
||||||
async move { build_post_idx(&db_clone, post, hash).await }
|
|
||||||
})
|
|
||||||
.buffer_unordered(usize::MAX)
|
|
||||||
.collect()
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let errs: Vec<_> = res
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|res| res.err())
|
|
||||||
.map(|e| e.to_string())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
if !errs.is_empty() {
|
|
||||||
Err((
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
Json::from(json!({"error": errs})),
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
Ok(StatusCode::OK)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Search a dir, return a list of markdown files
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
/// * `entry`: dir path
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
/// Vec<String, Global>
|
|
||||||
/// * `String`: path of a markdown file
|
|
||||||
///
|
|
||||||
fn search_md(entry: &Path) -> Vec<String> {
|
|
||||||
if entry.ends_with(".git") {
|
|
||||||
return vec![];
|
|
||||||
}
|
|
||||||
if entry.is_dir() {
|
|
||||||
if let Ok(files) = entry.read_dir() {
|
|
||||||
files
|
|
||||||
.flat_map(|file| {
|
|
||||||
let file_path = file.unwrap().path();
|
|
||||||
search_md(file_path.as_path())
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
} else {
|
|
||||||
vec![]
|
|
||||||
}
|
|
||||||
} else if entry.extension().is_some_and(|ext| ext == "md") {
|
|
||||||
vec![entry.to_str().unwrap().to_string()]
|
|
||||||
} else {
|
|
||||||
vec![]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn build_post_idx(
|
|
||||||
db_connection: &PgPool,
|
|
||||||
post: Map<String, Value>,
|
|
||||||
hash: Vec<u8>,
|
|
||||||
) -> Result<(), PostIdxError> {
|
|
||||||
let meta = &post["meta"];
|
|
||||||
|
|
||||||
// extract post id
|
|
||||||
let post_id = match Uuid::from_str(post["post_id"].as_str().unwrap()) {
|
|
||||||
Ok(id) => id,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(PostIdxError::InvalidUUID {
|
|
||||||
id: post["post_id"].as_str().unwrap().to_string(),
|
|
||||||
err_msg: e.to_string(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if build_post_idx::hash(db_connection, post_id, hash).await? {
|
|
||||||
build_post_idx::base(db_connection, &post, post_id, meta).await?;
|
|
||||||
build_post_idx::tag(db_connection, post_id, meta).await?;
|
|
||||||
build_post_idx::meta(db_connection, post_id, meta).await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
@@ -1,288 +0,0 @@
|
|||||||
use base64::{Engine, engine::general_purpose};
|
|
||||||
use chrono::DateTime;
|
|
||||||
use serde_json::{Map, Value};
|
|
||||||
use sqlx::{PgPool, query, Row};
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
use crate::api::post::errors::refresh::PostIdxError;
|
|
||||||
use crate::constants::GLOBAL_TIME_FORMAT;
|
|
||||||
|
|
||||||
pub async fn base(
|
|
||||||
db_connection: &PgPool,
|
|
||||||
post: &Map<String, Value>,
|
|
||||||
post_id: Uuid,
|
|
||||||
meta: &Value,
|
|
||||||
) -> Result<(), PostIdxError> {
|
|
||||||
let title = post["title"].as_str();
|
|
||||||
let summary = post
|
|
||||||
.get("summary")
|
|
||||||
.and_then(|summary| summary.as_str())
|
|
||||||
.unwrap_or("");
|
|
||||||
let content = post["content"].as_str();
|
|
||||||
let last_update =
|
|
||||||
match DateTime::parse_from_str(post["last_update"].as_str().unwrap(), GLOBAL_TIME_FORMAT) {
|
|
||||||
Ok(time) => time,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(PostIdxError::InvalidLastUpdate {
|
|
||||||
id: post_id.to_string(),
|
|
||||||
err_msg: e.to_string(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let first_update = match DateTime::parse_from_str(
|
|
||||||
post["first_update"].as_str().unwrap(),
|
|
||||||
GLOBAL_TIME_FORMAT,
|
|
||||||
) {
|
|
||||||
Ok(time) => time,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(PostIdxError::InvalidFirstUpdate {
|
|
||||||
id: post_id.to_string(),
|
|
||||||
err_msg: e.to_string(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let sub_title = meta
|
|
||||||
.get("sub_title")
|
|
||||||
.and_then(|sub_title| sub_title.as_str())
|
|
||||||
.unwrap_or("");
|
|
||||||
let category = meta
|
|
||||||
.get("category")
|
|
||||||
.and_then(|category| category.as_str())
|
|
||||||
.unwrap_or("N/A");
|
|
||||||
let header_img = meta
|
|
||||||
.get("header_img")
|
|
||||||
.and_then(|header_img| header_img.as_str())
|
|
||||||
.unwrap_or("");
|
|
||||||
|
|
||||||
match query(
|
|
||||||
r##"
|
|
||||||
INSERT INTO Post (post_id, title, summary, content, last_update, first_update, sub_title, category, header_img)
|
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
|
||||||
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,
|
|
||||||
sub_title = EXCLUDED.sub_title,
|
|
||||||
category = EXCLUDED.category,
|
|
||||||
header_img = EXCLUDED.header_img;
|
|
||||||
"##,
|
|
||||||
)
|
|
||||||
.bind(post_id)
|
|
||||||
.bind(title)
|
|
||||||
.bind(summary)
|
|
||||||
.bind(content)
|
|
||||||
.bind(last_update)
|
|
||||||
.bind(first_update)
|
|
||||||
.bind(sub_title)
|
|
||||||
.bind(category)
|
|
||||||
.bind(header_img)
|
|
||||||
.execute(db_connection)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(e) => {
|
|
||||||
return Err(
|
|
||||||
PostIdxError::DBWriteFailure {
|
|
||||||
data_desc: "post base information".to_string(),
|
|
||||||
db_table: "Post".to_string(),
|
|
||||||
id: post_id.to_string(),
|
|
||||||
err_msg: e.to_string()
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn tag(db_connection: &PgPool, post_id: Uuid, meta: &Value) -> Result<(), PostIdxError> {
|
|
||||||
if let Some(tags_string) = meta.get("tags").to_owned() {
|
|
||||||
// parse tags string
|
|
||||||
let parsed: Value = match serde_json::from_str(tags_string.as_str().unwrap()) {
|
|
||||||
Ok(value) => value,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(PostIdxError::InvalidTagString {
|
|
||||||
id: post_id.to_string(),
|
|
||||||
invalid_string: tags_string.to_string().to_string(),
|
|
||||||
err_msg: e.to_string(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let string_array = parsed.as_array().unwrap();
|
|
||||||
let tags: Vec<String> = string_array
|
|
||||||
.iter()
|
|
||||||
.map(|value| value.as_str().unwrap().to_string())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
// remove old record
|
|
||||||
if let Err(e) = query("DELETE FROM Tag WHERE post_id = $1")
|
|
||||||
.bind(post_id)
|
|
||||||
.execute(db_connection)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
return Err(PostIdxError::DBWriteFailure {
|
|
||||||
data_desc: "post tag information".to_string(),
|
|
||||||
db_table: "Tag".to_string(),
|
|
||||||
id: post_id.to_string(),
|
|
||||||
err_msg: e.to_string(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// add new record
|
|
||||||
for tag in tags {
|
|
||||||
match query("INSERT INTO Tag VALUES ($1, $2);")
|
|
||||||
.bind(post_id)
|
|
||||||
.bind(tag)
|
|
||||||
.execute(db_connection)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(e) => {
|
|
||||||
return Err(PostIdxError::DBWriteFailure {
|
|
||||||
data_desc: "post tag information".to_string(),
|
|
||||||
db_table: "Tag".to_string(),
|
|
||||||
id: post_id.to_string(),
|
|
||||||
err_msg: e.to_string(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// # Arguments
|
|
||||||
///
|
|
||||||
/// * `db_connection`: database connection
|
|
||||||
/// * `post_id`: UUID of the post
|
|
||||||
/// * `hash`: Hex String
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
/// Result<bool, PostIdxError>
|
|
||||||
/// - `bool`: if the hash is different from database, return true, else false
|
|
||||||
pub async fn hash(
|
|
||||||
db_connection: &PgPool,
|
|
||||||
post_id: Uuid,
|
|
||||||
hash: Vec<u8>,
|
|
||||||
) -> Result<bool, PostIdxError> {
|
|
||||||
let base64_form = general_purpose::STANDARD.encode(hash);
|
|
||||||
match query("SELECT hash FROM Hash WHERE post_id = $1")
|
|
||||||
.bind(post_id)
|
|
||||||
.fetch_optional(db_connection)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(row) => {
|
|
||||||
let db_record: String = match row {
|
|
||||||
None => "".to_string(),
|
|
||||||
Some(row) => row.try_get("hash").unwrap_or("".to_string()),
|
|
||||||
};
|
|
||||||
if base64_form == db_record {
|
|
||||||
Ok(false)
|
|
||||||
} else {
|
|
||||||
if let Err(e) = query(
|
|
||||||
r#"
|
|
||||||
INSERT INTO Hash (post_id, hash) VALUES ($1, $2)
|
|
||||||
ON CONFLICT (post_id) DO UPDATE SET hash = EXCLUDED.hash"#,
|
|
||||||
)
|
|
||||||
.bind(post_id)
|
|
||||||
.bind(&base64_form)
|
|
||||||
.execute(db_connection)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
return Err(PostIdxError::DBWriteFailure {
|
|
||||||
data_desc: "post hash".to_string(),
|
|
||||||
db_table: "Hash".to_string(),
|
|
||||||
id: post_id.to_string(),
|
|
||||||
err_msg: e.to_string(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Ok(true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => Err(PostIdxError::DBReadFailure {
|
|
||||||
data_desc: "post hash".to_string(),
|
|
||||||
db_table: "Hash".to_string(),
|
|
||||||
id: post_id.to_string(),
|
|
||||||
err_msg: e.to_string(),
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn meta(db_connection: &PgPool, post_id: Uuid, meta: &Value) -> Result<(), PostIdxError> {
|
|
||||||
let mut err_trails = 5;
|
|
||||||
loop {
|
|
||||||
if let Err(e) = query(
|
|
||||||
format!(
|
|
||||||
r#"
|
|
||||||
DO
|
|
||||||
$$
|
|
||||||
DECLARE
|
|
||||||
_post_id UUID := '{}';
|
|
||||||
_meta_cols JSONB := '{}';
|
|
||||||
_exists BOOLEAN;
|
|
||||||
_col_name TEXT;
|
|
||||||
_col_value TEXT;
|
|
||||||
_sql TEXT;
|
|
||||||
_insert_columns TEXT := 'post_id';
|
|
||||||
_insert_values TEXT := quote_literal(_post_id);
|
|
||||||
_update_set TEXT := '';
|
|
||||||
_blacklist TEXT[] := ARRAY['tags'];
|
|
||||||
BEGIN
|
|
||||||
SELECT EXISTS(SELECT 1 FROM meta WHERE post_id = _post_id) INTO _exists;
|
|
||||||
|
|
||||||
FOR _col_name, _col_value IN SELECT key, value FROM jsonb_each_text(_meta_cols)
|
|
||||||
LOOP
|
|
||||||
IF _col_name = ANY(_blacklist) THEN
|
|
||||||
CONTINUE;
|
|
||||||
END IF;
|
|
||||||
|
|
||||||
IF NOT EXISTS (SELECT 1
|
|
||||||
FROM information_schema.columns
|
|
||||||
WHERE table_name = 'meta'
|
|
||||||
AND column_name = _col_name
|
|
||||||
-- may change in the future
|
|
||||||
AND table_schema = 'public') THEN
|
|
||||||
_sql := 'ALTER TABLE meta ADD COLUMN ' || quote_ident(_col_name) || ' TEXT';
|
|
||||||
EXECUTE _sql;
|
|
||||||
END IF;
|
|
||||||
_insert_columns := _insert_columns || ', ' || quote_ident(_col_name);
|
|
||||||
_insert_values := _insert_values || ', ' || quote_literal(_col_value);
|
|
||||||
_update_set := _update_set || ', ' || quote_ident(_col_name) || ' = ' || quote_literal(_col_value);
|
|
||||||
END LOOP;
|
|
||||||
|
|
||||||
_update_set := substring(_update_set FROM 3);
|
|
||||||
|
|
||||||
IF _exists THEN
|
|
||||||
_sql := 'UPDATE meta SET ' || _update_set || ' WHERE post_id = ' || quote_literal(_post_id);
|
|
||||||
EXECUTE _sql;
|
|
||||||
ELSE
|
|
||||||
_sql := 'INSERT INTO meta (' || _insert_columns || ') VALUES (' || _insert_values || ')';
|
|
||||||
EXECUTE _sql;
|
|
||||||
END IF;
|
|
||||||
END;
|
|
||||||
$$ LANGUAGE plpgsql;
|
|
||||||
"#, post_id.to_string().as_str(), meta.to_string()
|
|
||||||
).as_str()
|
|
||||||
)
|
|
||||||
.execute(db_connection)
|
|
||||||
.await {
|
|
||||||
if err_trails >= 0 {
|
|
||||||
err_trails -= 1;
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
return Err(PostIdxError::DBWriteFailure {
|
|
||||||
data_desc: "post meta data".to_string(),
|
|
||||||
db_table: "Meta".to_string(),
|
|
||||||
id: post_id.to_string(),
|
|
||||||
err_msg: e.to_string(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
pub mod build_post_idx;
|
|
||||||
pub mod post;
|
|
||||||
@@ -1,120 +0,0 @@
|
|||||||
use std::{env, fs};
|
|
||||||
use std::error::Error;
|
|
||||||
use std::process::Command;
|
|
||||||
use std::str;
|
|
||||||
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: &str) -> Result<Map<String, Value>, Box<dyn Error>> {
|
|
||||||
let mut res = Map::new();
|
|
||||||
|
|
||||||
// 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()
|
|
||||||
.into_iter()
|
|
||||||
.map(|(k, v)| (k, Value::from(v)))
|
|
||||||
.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) => 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));
|
|
||||||
|
|
||||||
// field `last_update`
|
|
||||||
let git_output = match Command::new("git")
|
|
||||||
.arg("--no-pager")
|
|
||||||
.arg("log")
|
|
||||||
.arg("-1")
|
|
||||||
.arg("--format=%cI")
|
|
||||||
.arg("--")
|
|
||||||
.current_dir(env::var("GIT_WORK_DIR")?)
|
|
||||||
.arg(path)
|
|
||||||
.output()
|
|
||||||
{
|
|
||||||
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(),
|
|
||||||
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()),
|
|
||||||
);
|
|
||||||
|
|
||||||
// field `first_update`
|
|
||||||
let first_update: DateTime<Utc> = match meta.get("date") {
|
|
||||||
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(
|
|
||||||
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(
|
|
||||||
"first_update".to_string(),
|
|
||||||
Value::from(first_update.format(GLOBAL_TIME_FORMAT).to_string()),
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(res)
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
pub const GLOBAL_TIME_FORMAT: &str = "%Y-%m-%dT%H:%M:%S%z";
|
|
||||||
|
|
||||||
pub const PAGE_SIZE: u32 = 6;
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
use std::{env, io};
|
|
||||||
|
|
||||||
use chrono::Local;
|
|
||||||
use tracing::info;
|
|
||||||
use tracing_appender::{non_blocking, rolling};
|
|
||||||
use tracing_appender::non_blocking::WorkerGuard;
|
|
||||||
use tracing_subscriber::{fmt, Registry};
|
|
||||||
use tracing_subscriber::fmt::format::Writer;
|
|
||||||
use tracing_subscriber::fmt::MakeWriter;
|
|
||||||
use tracing_subscriber::fmt::time::FormatTime;
|
|
||||||
use tracing_subscriber::layer::SubscriberExt;
|
|
||||||
|
|
||||||
use crate::constants::GLOBAL_TIME_FORMAT;
|
|
||||||
|
|
||||||
struct LocalTimer;
|
|
||||||
|
|
||||||
impl FormatTime for LocalTimer {
|
|
||||||
fn format_time(&self, w: &mut Writer<'_>) -> std::fmt::Result {
|
|
||||||
write!(w, "{}", Local::now().format(GLOBAL_TIME_FORMAT))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type _LogWriter = dyn for<'writer> MakeWriter<'writer, Writer = dyn io::Write + 'static> + 'static;
|
|
||||||
|
|
||||||
pub fn init() -> (WorkerGuard, WorkerGuard) {
|
|
||||||
let log_path = env::var("LOG_ROOT_DIR").unwrap_or_else(|e| {
|
|
||||||
panic!("[Fatal] failed to load LOG_ROOT_DIR: {}", e);
|
|
||||||
});
|
|
||||||
|
|
||||||
let log_path = format!("{}/backend", log_path);
|
|
||||||
|
|
||||||
let (info_log_writer, info_guard) = non_blocking(rolling::never(&log_path, "info.log"));
|
|
||||||
|
|
||||||
let (err_log_writer, error_guard) = non_blocking(rolling::never(&log_path, "error.log"));
|
|
||||||
|
|
||||||
let log_format = tracing_subscriber::fmt::format()
|
|
||||||
.with_level(true)
|
|
||||||
.with_target(true)
|
|
||||||
.with_source_location(true)
|
|
||||||
.with_timer(LocalTimer);
|
|
||||||
|
|
||||||
let subscribers = Registry::default()
|
|
||||||
.with(
|
|
||||||
fmt::Layer::default()
|
|
||||||
.with_writer(info_log_writer)
|
|
||||||
.with_ansi(false),
|
|
||||||
)
|
|
||||||
.with(
|
|
||||||
fmt::Layer::default()
|
|
||||||
.with_writer(err_log_writer)
|
|
||||||
.with_ansi(false),
|
|
||||||
)
|
|
||||||
.with(
|
|
||||||
fmt::Layer::default()
|
|
||||||
.with_writer(io::stdout)
|
|
||||||
.with_ansi(true)
|
|
||||||
.event_format(log_format),
|
|
||||||
);
|
|
||||||
|
|
||||||
tracing::subscriber::set_global_default(subscribers)
|
|
||||||
.expect("[Fatal] Failed to setup logging system");
|
|
||||||
|
|
||||||
info!("Module Started: Logger");
|
|
||||||
|
|
||||||
// error!("test");
|
|
||||||
(info_guard, error_guard)
|
|
||||||
}
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
use std::env;
|
|
||||||
use std::error::Error;
|
|
||||||
use std::num::ParseIntError;
|
|
||||||
use std::process::exit;
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use crate::api::{
|
|
||||||
get::{get_page_count::get_page_count, get_post::get_post, get_post_list::get_post_list},
|
|
||||||
post::refresh::refresh,
|
|
||||||
};
|
|
||||||
use axum::routing::{get, post};
|
|
||||||
use axum::Router;
|
|
||||||
use sqlx::postgres::{PgConnectOptions, PgPoolOptions};
|
|
||||||
use sqlx::ConnectOptions;
|
|
||||||
use tracing::{error, info, warn};
|
|
||||||
|
|
||||||
mod api;
|
|
||||||
mod constants;
|
|
||||||
mod logger;
|
|
||||||
mod markdown;
|
|
||||||
mod model;
|
|
||||||
|
|
||||||
#[tokio::main]
|
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
|
||||||
if let Some(env_file) = env::args().nth(1) {
|
|
||||||
match dotenv::from_filename(&env_file) {
|
|
||||||
Ok(_) => {
|
|
||||||
println!("[Info] env loaded from {}, starting up", env_file);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
panic!("[Fatal] failed to read {}: {}", env_file, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
println!("[info] no .env file referred, starting up")
|
|
||||||
}
|
|
||||||
|
|
||||||
let _guards = logger::init();
|
|
||||||
|
|
||||||
let port: u32 = match env::var("BACKEND_PORT") {
|
|
||||||
Ok(value) => value.parse().unwrap_or_else(|e: ParseIntError| {
|
|
||||||
warn!("Failed to parse BACKEND_PORT: {}", e.to_string());
|
|
||||||
3001
|
|
||||||
}),
|
|
||||||
Err(_) => 3001,
|
|
||||||
};
|
|
||||||
|
|
||||||
let db_connect_option = PgConnectOptions::from_str(&parse_db_uri())
|
|
||||||
.unwrap()
|
|
||||||
.disable_statement_logging();
|
|
||||||
|
|
||||||
let db_pool = match PgPoolOptions::new().connect_with(db_connect_option).await {
|
|
||||||
Ok(pool) => {
|
|
||||||
info!("Database connected");
|
|
||||||
pool
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!("Failed to load database: {}", e.to_string());
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let app = Router::new()
|
|
||||||
.route("/refresh_posts", post(refresh))
|
|
||||||
.route("/get_post_list", get(get_post_list))
|
|
||||||
.route("/get_page_count", get(get_page_count))
|
|
||||||
.route("/get_post/:post_id", get(get_post))
|
|
||||||
.with_state(db_pool);
|
|
||||||
|
|
||||||
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port))
|
|
||||||
.await
|
|
||||||
.unwrap_or_else(|e| {
|
|
||||||
error!("Failed to build TCP listener: {}", e.to_string());
|
|
||||||
exit(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
axum::serve(listener, app).await.unwrap_or_else(|e| {
|
|
||||||
error!("Failed to start axum: {}", e.to_string());
|
|
||||||
exit(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_db_uri() -> String {
|
|
||||||
let postgres_user = env::var("POSTGRES_USER").unwrap_or("".to_string());
|
|
||||||
let postgres_password = env::var("POSTGRES_PASSWORD").unwrap_or("".to_string());
|
|
||||||
let db_host = env::var("DB_HOST").unwrap_or("localhost".to_string());
|
|
||||||
let db_port = env::var("DB_PORT")
|
|
||||||
.unwrap_or("5432".to_string())
|
|
||||||
.to_string();
|
|
||||||
let postgres_db = env::var("POSTGRES_DB").unwrap_or("nocturne".to_string());
|
|
||||||
|
|
||||||
format!("postgresql://{postgres_user}:{postgres_password}@{db_host}:{db_port}/{postgres_db}")
|
|
||||||
}
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use regex::Regex;
|
|
||||||
|
|
||||||
/// return the meta info contained in the .md file front matter
|
|
||||||
pub fn parse_meta(md_content: &str) -> HashMap<String, String> {
|
|
||||||
let mut fields: HashMap<String, String> = HashMap::new();
|
|
||||||
let match_regex = Regex::new(r"(?s)---\n(.*\n)---\n").unwrap();
|
|
||||||
if let Some(front_matter) = match_regex.captures(md_content) {
|
|
||||||
let fm_content = front_matter.get(1).unwrap().as_str();
|
|
||||||
for line in fm_content.lines() {
|
|
||||||
if let Some((k, v)) = extract_kv(line) {
|
|
||||||
fields.insert(k, v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fields
|
|
||||||
}
|
|
||||||
|
|
||||||
fn extract_kv(line: &str) -> Option<(String, String)> {
|
|
||||||
let split: Vec<&str> = line.splitn(2, ':').map(|s| s.trim()).collect();
|
|
||||||
if split.len() == 2 {
|
|
||||||
let key = split[0].to_string();
|
|
||||||
let value = split[1].to_string();
|
|
||||||
Some((key, value))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod meta_test {
|
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
use crate::markdown::meta::parse_meta;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_parse_meta_front_matter() -> Result<(), Box<dyn Error>> {
|
|
||||||
let parsed_meta = parse_meta(
|
|
||||||
r##"
|
|
||||||
---
|
|
||||||
title: Hello World!
|
|
||||||
tags: [ "test", "hello_world" ]
|
|
||||||
---
|
|
||||||
"##,
|
|
||||||
);
|
|
||||||
dbg!(&parsed_meta);
|
|
||||||
assert_eq!(parsed_meta.get("title"), Some(&"Hello World!".to_string()));
|
|
||||||
assert_eq!(
|
|
||||||
parsed_meta.get("tags"),
|
|
||||||
Some(&r#"[ "test", "hello_world" ]"#.to_string())
|
|
||||||
);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
pub mod meta;
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS Hash
|
|
||||||
(
|
|
||||||
post_id UUID PRIMARY KEY,
|
|
||||||
hash VARCHAR(44)
|
|
||||||
);
|
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS Meta
|
|
||||||
(
|
|
||||||
post_id UUID PRIMARY KEY
|
|
||||||
);
|
|
||||||
|
|
||||||
DROP table meta;
|
|
||||||
|
|
||||||
DO
|
|
||||||
$$
|
|
||||||
DECLARE
|
|
||||||
_post_id UUID := '{}';
|
|
||||||
_meta_cols JSONB := '{}';
|
|
||||||
_exists BOOLEAN;
|
|
||||||
_col_name TEXT;
|
|
||||||
_col_value TEXT;
|
|
||||||
_sql TEXT;
|
|
||||||
_insert_columns TEXT := 'post_id';
|
|
||||||
_insert_values TEXT := quote_literal(_post_id);
|
|
||||||
_update_set TEXT := '';
|
|
||||||
_blacklist TEXT[] := ARRAY ['id', 'tags'];
|
|
||||||
BEGIN
|
|
||||||
SELECT EXISTS(SELECT 1 FROM meta WHERE post_id = _post_id) INTO _exists;
|
|
||||||
|
|
||||||
FOR _col_name, _col_value IN SELECT key, value FROM jsonb_each_text(_meta_cols)
|
|
||||||
LOOP
|
|
||||||
IF _col_name = ANY (_blacklist) THEN
|
|
||||||
CONTINUE;
|
|
||||||
END IF;
|
|
||||||
|
|
||||||
IF NOT EXISTS (SELECT 1
|
|
||||||
FROM information_schema.columns
|
|
||||||
WHERE table_name = 'meta'
|
|
||||||
AND column_name = _col_name
|
|
||||||
-- may change in the future
|
|
||||||
AND table_schema = 'public') THEN
|
|
||||||
_sql := 'ALTER TABLE meta ADD COLUMN ' || quote_ident(_col_name) || ' TEXT';
|
|
||||||
EXECUTE _sql;
|
|
||||||
END IF;
|
|
||||||
_insert_columns := _insert_columns || ', ' || quote_ident(_col_name);
|
|
||||||
_insert_values := _insert_values || ', ' || quote_literal(_col_value);
|
|
||||||
_update_set := _update_set || ', ' || quote_ident(_col_name) || ' = ' || quote_literal(_col_value);
|
|
||||||
END LOOP;
|
|
||||||
|
|
||||||
_update_set := substring(_update_set FROM 3);
|
|
||||||
|
|
||||||
IF _exists THEN
|
|
||||||
_sql := 'UPDATE meta SET ' || _update_set || ' WHERE post_id = ' || quote_literal(_post_id);
|
|
||||||
EXECUTE _sql;
|
|
||||||
ELSE
|
|
||||||
_sql := 'INSERT INTO meta (' || _insert_columns || ') VALUES (' || _insert_values || ')';
|
|
||||||
EXECUTE _sql;
|
|
||||||
END IF;
|
|
||||||
END;
|
|
||||||
$$ LANGUAGE plpgsql;
|
|
||||||
|
|
||||||
SELECT jsonb_strip_nulls(to_jsonb(t)) AS meta
|
|
||||||
FROM meta t
|
|
||||||
WHERE post_id = '8ca3fa2b-f040-448c-8885-81aab56db9bb';
|
|
||||||
|
|
||||||
WITH target_post AS (SELECT t.date FROM meta t WHERE post_id = '8ca3fa2b-f040-448c-8885-81aab56db9bb'),
|
|
||||||
prev AS (SELECT post_id, t.title, t.sub_title, t.header_img
|
|
||||||
FROM meta t
|
|
||||||
WHERE t.date < (SELECT date FROM target_post)
|
|
||||||
ORDER BY t.date DESC
|
|
||||||
LIMIT 1),
|
|
||||||
next AS (SELECT post_id, t.title, t.sub_title, t.header_img
|
|
||||||
FROM meta t
|
|
||||||
WHERE t.date > (SELECT date FROM target_post)
|
|
||||||
ORDER BY t.date
|
|
||||||
LIMIT 1)
|
|
||||||
SELECT json_strip_nulls(json_build_object(
|
|
||||||
'prev',
|
|
||||||
(SELECT json_build_object('id', post_id, 'title', title, 'sub_title', sub_title, 'header_img', header_img)
|
|
||||||
FROM prev),
|
|
||||||
'next',
|
|
||||||
(SELECT json_build_object('id', post_id, 'title', title, 'sub_title', sub_title, 'header_img', header_img)
|
|
||||||
FROM next)
|
|
||||||
)) AS result;
|
|
||||||
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
pub const POST_BASE_MODEL: &str = r##"
|
|
||||||
CREATE TABLE IF NOT EXISTS Post
|
|
||||||
(
|
|
||||||
post_id UUID PRIMARY KEY,
|
|
||||||
title VARCHAR(255) NOT NULL,
|
|
||||||
category VARCHAR(255) NOT NULL,
|
|
||||||
header_img TEXT,
|
|
||||||
summary TEXT,
|
|
||||||
sub_title TEXT,
|
|
||||||
content TEXT NOT NULL,
|
|
||||||
last_update TIMESTAMPTZ NOT NULL,
|
|
||||||
first_update TIMESTAMPTZ NOT NULL
|
|
||||||
);
|
|
||||||
"##;
|
|
||||||
|
|
||||||
// base mode, will add more cols in runtime
|
|
||||||
pub const POST_META_MODEL: &str = r##"
|
|
||||||
CREATE TABLE IF NOT EXISTS Meta
|
|
||||||
(
|
|
||||||
post_id UUID PRIMARY KEY
|
|
||||||
);
|
|
||||||
"##;
|
|
||||||
|
|
||||||
pub const POST_HASH_MODEL: &str = r##"
|
|
||||||
CREATE TABLE IF NOT EXISTS Hash
|
|
||||||
(
|
|
||||||
post_id UUID PRIMARY KEY,
|
|
||||||
hash VARCHAR(44)
|
|
||||||
);
|
|
||||||
"##;
|
|
||||||
|
|
||||||
pub const TAG_MODEL: &str = r##"
|
|
||||||
CREATE TABLE IF NOT EXISTS Tag
|
|
||||||
(
|
|
||||||
post_id UUID NOT NULL,
|
|
||||||
tag VARCHAR(255) NOT NULL
|
|
||||||
);
|
|
||||||
"##;
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS Post
|
|
||||||
(
|
|
||||||
post_id UUID PRIMARY KEY,
|
|
||||||
title VARCHAR(255) NOT NULL,
|
|
||||||
category VARCHAR(255) NOT NULL,
|
|
||||||
header_img TEXT,
|
|
||||||
summary TEXT,
|
|
||||||
sub_title TEXT,
|
|
||||||
content TEXT NOT NULL,
|
|
||||||
last_update TIMESTAMPTZ NOT NULL,
|
|
||||||
first_update TIMESTAMPTZ NOT NULL
|
|
||||||
);
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS Tag
|
|
||||||
(
|
|
||||||
post_id UUID NOT NULL,
|
|
||||||
tag VARCHAR(255) NOT NULL
|
|
||||||
);
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
pub mod blog;
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "macros"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
tracing = "0.1.40"
|
||||||
|
tracing-subscriber = "0.3.18"
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#[macro_export]
|
||||||
|
macro_rules! dev_consume {
|
||||||
|
($($var:ident),+) => {
|
||||||
|
$(let _ = $var;)*
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
/// Includes a UTF-8 encoded file as a string
|
||||||
|
///
|
||||||
|
/// The file is located relative to `CARGO_MANIFEST_DIR`.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```rust
|
||||||
|
/// use macros::include_str_from_crate_root;
|
||||||
|
///
|
||||||
|
/// let str: &str = include_str_from_crate_root!("/query/scheme.sql");
|
||||||
|
///
|
||||||
|
// todo: impl
|
||||||
|
// const PATH_PREFIX: &str = "/src";
|
||||||
|
// let str: &str = include_str_from_crate_root!("{}/query/scheme.sql", PATH_PREFIX);
|
||||||
|
/// ```
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! include_str_from_crate_root {
|
||||||
|
// ($path:literal $(, $($arg:tt)*)?) => {
|
||||||
|
// ($path:expr, $($args:tt)*) => {
|
||||||
|
// include_str!(concat!(env!("CARGO_MANIFEST_DIR"), format_args!($path, $($args)*)))
|
||||||
|
// };
|
||||||
|
($path:expr) => {
|
||||||
|
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), $path))
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
mod dev;
|
||||||
|
mod include_str;
|
||||||
|
mod log;
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
/// log the message at specific log level and exit(1)
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```rust
|
||||||
|
///
|
||||||
|
/// use macros::panic_with_log;
|
||||||
|
/// use tracing::Level;
|
||||||
|
///
|
||||||
|
/// panic_with_log!(Level::ERROR, "test panicked: {}", 42);
|
||||||
|
/// ```
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! panic_with_log {
|
||||||
|
($level:expr, $msg:literal $(, $($arg:tt)*)?) => {{
|
||||||
|
use std::process;
|
||||||
|
use tracing::event;
|
||||||
|
event!($level, $msg $(, $($arg)*)?);
|
||||||
|
process::exit(1);
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// log the message with `Level::ERROR` and exit(1)
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```rust
|
||||||
|
///
|
||||||
|
/// use macros::error_panic;
|
||||||
|
///
|
||||||
|
/// error_panic!("test panicked: {}", 42);
|
||||||
|
/// ```
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! error_panic {
|
||||||
|
($msg:literal $(, $($arg:tt)*)?) => {{
|
||||||
|
use std::process;
|
||||||
|
use tracing::{event, Level};
|
||||||
|
event!(Level::ERROR, $msg $(, $($arg)*)?);
|
||||||
|
process::exit(1);
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn test_panic_with_log() {
|
||||||
|
use std::io;
|
||||||
|
use tracing::Level;
|
||||||
|
use tracing_subscriber::prelude::*;
|
||||||
|
use tracing_subscriber::{Registry, fmt};
|
||||||
|
|
||||||
|
let format = tracing_subscriber::fmt::format()
|
||||||
|
.with_level(true)
|
||||||
|
.with_target(true)
|
||||||
|
.with_source_location(true);
|
||||||
|
|
||||||
|
let subscriber = Registry::default().with(
|
||||||
|
fmt::Layer::default()
|
||||||
|
.with_writer(io::stderr)
|
||||||
|
.event_format(format),
|
||||||
|
);
|
||||||
|
|
||||||
|
tracing::subscriber::set_global_default(subscriber).unwrap();
|
||||||
|
|
||||||
|
panic_with_log!(Level::ERROR, "Test panicked: {}", 42);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,14 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "markdown"
|
name = "markdown"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
uuid = { version = "1.10.0", features = ["v4", "fast-rng", "serde"] }
|
||||||
|
serde = { version = "1.0.209", features = ["derive"] }
|
||||||
|
serde_json = "1.0.128"
|
||||||
|
regex = "1.10.4"
|
||||||
|
yaml-rust2 = "0.10.0"
|
||||||
|
sha2 = "0.10.8"
|
||||||
|
base64 = "0.22.1"
|
||||||
|
thiserror = "2.0.3"
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
#[error("invalid front-matter: {}", msg)]
|
||||||
|
InvalidFrontMatter { msg: String },
|
||||||
|
|
||||||
|
#[error("fs error: {}", msg)]
|
||||||
|
FSError { msg: String },
|
||||||
|
}
|
||||||
+53
-10
@@ -1,14 +1,57 @@
|
|||||||
pub fn add(left: u64, right: u64) -> u64 {
|
pub mod error;
|
||||||
left + right
|
mod utils;
|
||||||
|
|
||||||
|
use std::{fs, path::Path, str::FromStr};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
error::Error,
|
||||||
|
utils::front_matter::{parse_front_matter, split_md_front_matter},
|
||||||
|
};
|
||||||
|
|
||||||
|
use base64::{Engine, engine::general_purpose};
|
||||||
|
use serde::Serialize;
|
||||||
|
use serde_json::Value;
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Serialize, Clone)]
|
||||||
|
pub struct MdFile {
|
||||||
|
pub meta: Value,
|
||||||
|
pub content: String,
|
||||||
|
pub hash: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
impl MdFile {
|
||||||
mod tests {
|
pub fn from_file(file_path: &Path) -> Result<MdFile, Error> {
|
||||||
use super::*;
|
let raw_file = match fs::read_to_string(file_path) {
|
||||||
|
Ok(file) => file,
|
||||||
#[test]
|
Err(e) => return Err(Error::FSError { msg: e.to_string() }),
|
||||||
fn it_works() {
|
};
|
||||||
let result = add(2, 2);
|
Self::from_str(&raw_file)
|
||||||
assert_eq!(result, 4);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromStr for MdFile {
|
||||||
|
type Err = Error;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
let bytes = s.as_bytes().to_vec();
|
||||||
|
let (front_matter_str, content) = split_md_front_matter(s);
|
||||||
|
let front_matter = match parse_front_matter(&front_matter_str) {
|
||||||
|
Ok(front_matter) => front_matter,
|
||||||
|
Err(e) => return Err(Error::InvalidFrontMatter { msg: e.to_string() }),
|
||||||
|
};
|
||||||
|
let hash = calculate_hash_base64(&bytes);
|
||||||
|
Ok(MdFile {
|
||||||
|
meta: front_matter,
|
||||||
|
content,
|
||||||
|
hash,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calculate_hash_base64(bytes: &Vec<u8>) -> String {
|
||||||
|
let mut hasher = Sha256::new();
|
||||||
|
hasher.update(bytes);
|
||||||
|
let hash = hasher.finalize().to_vec();
|
||||||
|
general_purpose::STANDARD.encode(hash)
|
||||||
|
}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
use crate::utils::yaml::yaml_to_serde;
|
||||||
|
|
||||||
|
use regex::Regex;
|
||||||
|
use serde_json::Value;
|
||||||
|
use yaml_rust2::YamlLoader;
|
||||||
|
|
||||||
|
pub fn split_md_front_matter(raw_file: &str) -> (String, String) {
|
||||||
|
let re = Regex::new(r"(?s)^(---\n.*?\n---\n)(.*)").unwrap();
|
||||||
|
if let Some(captures) = re.captures(raw_file) {
|
||||||
|
let front_matter = captures.get(1).map_or("", |m| m.as_str()).to_string();
|
||||||
|
let main_content = captures.get(2).map_or("", |m| m.as_str()).to_string();
|
||||||
|
(front_matter, main_content)
|
||||||
|
} else {
|
||||||
|
(String::new(), raw_file.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_front_matter(front_matter: &str) -> Result<Value, Box<dyn Error>> {
|
||||||
|
let parsed_f_matter = YamlLoader::load_from_str(front_matter)?;
|
||||||
|
if parsed_f_matter.is_empty() {
|
||||||
|
Ok(Value::String(String::new()))
|
||||||
|
} else {
|
||||||
|
Ok(yaml_to_serde(&parsed_f_matter[0]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::utils::front_matter::{parse_front_matter, split_md_front_matter};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn front_matter_to_string() {
|
||||||
|
let (front_matter, main_content) = split_md_front_matter(
|
||||||
|
r##"---
|
||||||
|
title: test_title
|
||||||
|
date: 0000-00-00+0000
|
||||||
|
category: test
|
||||||
|
id: bf03afd8-61b4-4e78-a34a-8b3ee39adf16
|
||||||
|
sub_title: sub_title
|
||||||
|
summary: summary
|
||||||
|
tags: [ "test1", "test2" ]
|
||||||
|
---
|
||||||
|
|
||||||
|
test body
|
||||||
|
|
||||||
|
"##,
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
front_matter,
|
||||||
|
r##"---
|
||||||
|
title: test_title
|
||||||
|
date: 0000-00-00+0000
|
||||||
|
category: test
|
||||||
|
id: bf03afd8-61b4-4e78-a34a-8b3ee39adf16
|
||||||
|
sub_title: sub_title
|
||||||
|
summary: summary
|
||||||
|
tags: [ "test1", "test2" ]
|
||||||
|
---
|
||||||
|
"##
|
||||||
|
);
|
||||||
|
assert_eq!(main_content, "\ntest body\n\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_front_matter_parser() {
|
||||||
|
parse_front_matter(
|
||||||
|
r##"---
|
||||||
|
title: test_title
|
||||||
|
date: 0000-00-00+0000
|
||||||
|
category: test
|
||||||
|
id: bf03afd8-61b4-4e78-a34a-8b3ee39adf16
|
||||||
|
sub_title: sub_title
|
||||||
|
summary: summary
|
||||||
|
tags: [ "test1", "test2" ]
|
||||||
|
---
|
||||||
|
"##,
|
||||||
|
)
|
||||||
|
.expect("Failed to parse front matter");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod front_matter;
|
||||||
|
mod yaml;
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
use serde_json::Value;
|
||||||
|
use std::str::FromStr;
|
||||||
|
use yaml_rust2::Yaml;
|
||||||
|
|
||||||
|
pub fn yaml_to_serde(yaml: &Yaml) -> Value {
|
||||||
|
match yaml {
|
||||||
|
Yaml::Null => Value::Null,
|
||||||
|
Yaml::Boolean(b) => Value::Bool(*b),
|
||||||
|
Yaml::Integer(i) => Value::Number(serde_json::Number::from(*i)),
|
||||||
|
Yaml::Real(s) => Value::Number(serde_json::Number::from_str(s).unwrap()),
|
||||||
|
Yaml::String(s) => Value::String(s.clone()),
|
||||||
|
Yaml::Array(arr) => Value::Array(arr.iter().map(yaml_to_serde).collect()),
|
||||||
|
Yaml::Hash(hash) => {
|
||||||
|
let obj: serde_json::map::Map<String, Value> = hash
|
||||||
|
.iter()
|
||||||
|
.map(|(k, v)| {
|
||||||
|
let key = match k {
|
||||||
|
Yaml::String(s) => s.clone(),
|
||||||
|
_ => "INVALID_KEY".to_string(),
|
||||||
|
};
|
||||||
|
(key, yaml_to_serde(v))
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
Value::Object(obj)
|
||||||
|
}
|
||||||
|
Yaml::Alias(_) => Value::Null,
|
||||||
|
Yaml::BadValue => Value::Null,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
[package]
|
||||||
|
name = "server"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
macros = { path = "../macros" }
|
||||||
|
markdown = { path = "../markdown" }
|
||||||
|
|
||||||
|
axum = { version = "0.8.1", features = ["macros"] }
|
||||||
|
tracing = { version = "0.1.41", features = ["log"] }
|
||||||
|
git2 = "0.20.0"
|
||||||
|
chrono = { version = "0.4.39", features = ["serde"] }
|
||||||
|
cron = "0.15.0"
|
||||||
|
sqlx = { version = "0.8.3", features = ["runtime-tokio", "tls-native-tls", "postgres", "chrono", "uuid"] }
|
||||||
|
serde_json = "1.0.133"
|
||||||
|
serde = { version = "1.0.215", features = ["derive"] }
|
||||||
|
tracing-subscriber = "0.3.18"
|
||||||
|
tracing-appender = "0.2.3"
|
||||||
|
dirs = "6.0.0"
|
||||||
|
thiserror = "2.0.11"
|
||||||
|
tokio = { version = "1.42.0", features = ["rt", "rt-multi-thread", "macros", "sync"] }
|
||||||
|
regex = "1.11.1"
|
||||||
|
dotenv = "0.15.0"
|
||||||
|
uuid = { version = "1.13.2", features = ["v4"] }
|
||||||
|
futures = "0.3.31"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
tempfile = "3.19.1"
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
git2 = "0.20.0"
|
||||||
|
chrono = "0.4.39"
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
use chrono::Utc;
|
||||||
|
use git2::Repository;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let repo = Repository::open("../..").expect("failed to open repository");
|
||||||
|
let commit = repo
|
||||||
|
.head()
|
||||||
|
.expect("failed to get HEAD")
|
||||||
|
.peel_to_commit()
|
||||||
|
.expect("failed to get HEAD commit");
|
||||||
|
let hash = commit.id().to_string();
|
||||||
|
let utc_timestamp = Utc::now().date_naive().format("%Y%m%d").to_string();
|
||||||
|
let pkg_version = env!("CARGO_PKG_VERSION");
|
||||||
|
let build_id = format!("{}:{}@{}", pkg_version, &hash[..8], utc_timestamp);
|
||||||
|
println!("cargo:rustc-env=BUILD_ID={}", build_id);
|
||||||
|
}
|
||||||
File renamed without changes.
@@ -0,0 +1,50 @@
|
|||||||
|
// todo: rebuild with static OnceCell
|
||||||
|
|
||||||
|
// Database related
|
||||||
|
pub mod database {
|
||||||
|
pub mod var_name {
|
||||||
|
pub const URI: &str = "DATABASE_URL";
|
||||||
|
pub const USERNAME: &str = "POSTGRES_USER";
|
||||||
|
pub const PASSWORD: &str = "POSTGRES_PASSWORD";
|
||||||
|
pub const HOST: &str = "POSTGRES_HOST";
|
||||||
|
pub const PORT: &str = "POSTGRES_PORT";
|
||||||
|
pub const DBNAME: &str = "POSTGRES_DB";
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod default_value {
|
||||||
|
pub const USERNAME: &str = "";
|
||||||
|
pub const PASSWORD: &str = "";
|
||||||
|
pub const HOST: &str = "localhost";
|
||||||
|
pub const PORT: &str = "5432";
|
||||||
|
pub const DBNAME: &str = "nocturne";
|
||||||
|
pub const DATABASE_TYPE: &str = "postgresql";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod server {
|
||||||
|
pub mod var_name {
|
||||||
|
pub const PORT: &str = "BACKEND_PORT";
|
||||||
|
pub const HOST: &str = "BACKEND_HOST";
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod default_value {
|
||||||
|
pub const PORT: u32 = 3001;
|
||||||
|
pub const HOST: &str = "0.0.0.0";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod general {
|
||||||
|
pub mod var_name {
|
||||||
|
pub const LOG_DIR: &str = "LOG_DIR";
|
||||||
|
pub const WORK_DIR: &str = "WORK_DIR";
|
||||||
|
pub const BLOG_GIT_URL: &str = "BLOG_GIT_URL";
|
||||||
|
pub const BLOG_GIT_REMOTE_BRANCH: &str = "BLOG_GIT_REMOTE_BRANCH";
|
||||||
|
pub const BLOG_GIT_REMOTE_NAME: &str = "BLOG_GIT_REMOTE_NAME";
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod default_value {
|
||||||
|
pub const WORK_DIR: &str = "~/.nocturne";
|
||||||
|
pub const BLOG_GIT_REMOTE_BRANCH: &str = "master";
|
||||||
|
pub const BLOG_GIT_REMOTE_NAME: &str = "origin";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
pub mod blog;
|
pub mod blog;
|
||||||
|
pub mod config;
|
||||||
pub mod time;
|
pub mod time;
|
||||||
@@ -1 +1,3 @@
|
|||||||
pub const DEFAULT_TIME_FORMAT: &str = "%Y-%m-%dT%H:%M:%S%z";
|
pub const DEFAULT_TIME_FORMAT: &str = "%Y-%m-%dT%H:%M:%S%z";
|
||||||
|
|
||||||
|
pub const DEFAULT_NAIVE_DATE_FORMAT: &str = "%Y-%m-%d";
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
use std::env;
|
||||||
|
use std::error::Error;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use crate::constants::config::database::{default_value, var_name};
|
||||||
|
|
||||||
|
use sqlx::postgres::{PgConnectOptions, PgPoolOptions};
|
||||||
|
use sqlx::{ConnectOptions, Pool, Postgres};
|
||||||
|
use tracing::info;
|
||||||
|
|
||||||
|
pub async fn init() -> Result<Pool<Postgres>, Box<dyn Error>> {
|
||||||
|
let db_connect_option = PgConnectOptions::from_str(&parse_db_uri())
|
||||||
|
.unwrap()
|
||||||
|
.disable_statement_logging();
|
||||||
|
Ok(PgPoolOptions::new()
|
||||||
|
.max_connections(1) // todo: temporary solution, to be fixed
|
||||||
|
.connect_with(db_connect_option)
|
||||||
|
.await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_db_uri() -> String {
|
||||||
|
env::var(var_name::URI).unwrap_or_else(|_| {
|
||||||
|
info!(
|
||||||
|
"`{}` env var is not set, \
|
||||||
|
using `{}`, `{}`, `{}`, `{}` and `{}`",
|
||||||
|
var_name::URI,
|
||||||
|
var_name::USERNAME,
|
||||||
|
var_name::PASSWORD,
|
||||||
|
var_name::HOST,
|
||||||
|
var_name::PORT,
|
||||||
|
var_name::DBNAME
|
||||||
|
);
|
||||||
|
let postgres_user = env::var(var_name::USERNAME).unwrap_or(default_value::USERNAME.into());
|
||||||
|
let postgres_password =
|
||||||
|
env::var(var_name::PASSWORD).unwrap_or(default_value::PASSWORD.into());
|
||||||
|
let db_host = env::var(var_name::HOST).unwrap_or(default_value::HOST.into());
|
||||||
|
let db_port = env::var(var_name::PORT).unwrap_or(default_value::PORT.into());
|
||||||
|
let postgres_db = env::var(var_name::DBNAME).unwrap_or(default_value::DBNAME.into());
|
||||||
|
let db_type = default_value::DATABASE_TYPE;
|
||||||
|
format!("{db_type}://{postgres_user}:{postgres_password}@{db_host}:{db_port}/{postgres_db}")
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
use std::{env, io};
|
||||||
|
|
||||||
|
use crate::constants::config::general::{default_value, var_name};
|
||||||
|
|
||||||
|
use chrono::Local;
|
||||||
|
use dirs::home_dir;
|
||||||
|
use tracing::info;
|
||||||
|
use tracing_appender::{non_blocking, non_blocking::WorkerGuard, rolling};
|
||||||
|
use tracing_subscriber::{
|
||||||
|
Layer, Registry, filter::LevelFilter, fmt, fmt::MakeWriter, fmt::format::Writer,
|
||||||
|
fmt::time::FormatTime, layer::SubscriberExt,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct LocalTimer;
|
||||||
|
|
||||||
|
impl FormatTime for LocalTimer {
|
||||||
|
fn format_time(&self, w: &mut Writer<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
w,
|
||||||
|
"{}",
|
||||||
|
Local::now().format(crate::constants::time::DEFAULT_TIME_FORMAT)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type _LogWriter = dyn for<'writer> MakeWriter<'writer, Writer = dyn io::Write + 'static> + 'static;
|
||||||
|
|
||||||
|
pub fn init() -> (WorkerGuard, WorkerGuard) {
|
||||||
|
let log_path = match env::var(var_name::LOG_DIR) {
|
||||||
|
Ok(path) => Path::new(&path).to_path_buf(),
|
||||||
|
Err(_) => {
|
||||||
|
let path = if let Ok(default_log_path) = env::var(var_name::WORK_DIR) {
|
||||||
|
expand_home_path(default_log_path)
|
||||||
|
} else {
|
||||||
|
let fallback_path = expand_home_path(default_value::WORK_DIR.to_string());
|
||||||
|
println!(
|
||||||
|
"[warn] unset env var `{}` and `{}`, trying to log on fallback path: {}/log",
|
||||||
|
var_name::LOG_DIR,
|
||||||
|
var_name::WORK_DIR,
|
||||||
|
fallback_path
|
||||||
|
);
|
||||||
|
fallback_path
|
||||||
|
};
|
||||||
|
Path::new(&path).join("log")
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let (info_log_writer, info_guard) = non_blocking(rolling::never(&log_path, "info.log"));
|
||||||
|
let (err_log_writer, error_guard) = non_blocking(rolling::never(&log_path, "error.log"));
|
||||||
|
|
||||||
|
let info_format = tracing_subscriber::fmt::format()
|
||||||
|
.with_level(true)
|
||||||
|
.with_target(true)
|
||||||
|
.with_source_location(true)
|
||||||
|
.with_ansi(false);
|
||||||
|
let err_format = tracing_subscriber::fmt::format()
|
||||||
|
.with_level(true)
|
||||||
|
.with_target(true)
|
||||||
|
.with_source_location(true)
|
||||||
|
.with_ansi(false);
|
||||||
|
let stdout_format = tracing_subscriber::fmt::format()
|
||||||
|
.with_level(true)
|
||||||
|
.with_target(true)
|
||||||
|
.with_source_location(true)
|
||||||
|
.with_timer(LocalTimer);
|
||||||
|
|
||||||
|
let subscribers = Registry::default()
|
||||||
|
.with(
|
||||||
|
fmt::Layer::default()
|
||||||
|
.with_writer(info_log_writer)
|
||||||
|
.event_format(info_format),
|
||||||
|
)
|
||||||
|
.with(
|
||||||
|
fmt::Layer::default()
|
||||||
|
.with_writer(err_log_writer)
|
||||||
|
.event_format(err_format)
|
||||||
|
.with_filter(LevelFilter::ERROR),
|
||||||
|
)
|
||||||
|
.with(
|
||||||
|
fmt::Layer::default()
|
||||||
|
.with_writer(io::stdout)
|
||||||
|
.with_ansi(true)
|
||||||
|
.event_format(stdout_format),
|
||||||
|
);
|
||||||
|
|
||||||
|
tracing::subscriber::set_global_default(subscribers)
|
||||||
|
.expect("[fatal] failed to setup logging system");
|
||||||
|
|
||||||
|
info!("logging to dir: {}", log_path.to_str().unwrap());
|
||||||
|
|
||||||
|
(info_guard, error_guard)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expand_home_path(path: String) -> String {
|
||||||
|
if path.starts_with('~') {
|
||||||
|
let home_path = home_dir()
|
||||||
|
.unwrap_or_else(|| panic!("[fatal] unable to locate home dir when parsing env `{}`, try to use absolute path instead", var_name::WORK_DIR));
|
||||||
|
home_path
|
||||||
|
.join(path.strip_prefix("~/").unwrap())
|
||||||
|
.into_os_string()
|
||||||
|
.into_string()
|
||||||
|
.unwrap()
|
||||||
|
} else {
|
||||||
|
path
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
use git2::opts::{set_server_connect_timeout_in_milliseconds, set_server_timeout_in_milliseconds};
|
||||||
|
use tracing::warn;
|
||||||
|
|
||||||
|
pub fn init() {
|
||||||
|
// git2 option
|
||||||
|
unsafe {
|
||||||
|
if let Err(e) = set_server_connect_timeout_in_milliseconds(10_000) {
|
||||||
|
warn!("failed to set git2 server connect timeout: {}", e);
|
||||||
|
}
|
||||||
|
if let Err(e) = set_server_timeout_in_milliseconds(60_000) {
|
||||||
|
warn!("failed to set git2 server timeout: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
mod database;
|
||||||
|
pub mod logger;
|
||||||
|
mod misc;
|
||||||
|
|
||||||
|
use std::{env, panic};
|
||||||
|
|
||||||
|
use crate::modules::{ModuleTree, blog::Blog};
|
||||||
|
use macros::error_panic;
|
||||||
|
|
||||||
|
use tracing::info;
|
||||||
|
|
||||||
|
pub fn load_env() {
|
||||||
|
if let Some(env_file) = env::args().nth(1) {
|
||||||
|
match dotenv::from_filename(&env_file) {
|
||||||
|
Ok(_) => {
|
||||||
|
println!("[info] env loaded from {}, starting up", env_file);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
panic!("[fatal] failed to read {}: {}", env_file, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("[info] no .env file referred, starting up")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn start() {
|
||||||
|
misc::init();
|
||||||
|
|
||||||
|
// connect database
|
||||||
|
let db_pool = match database::init().await {
|
||||||
|
Ok(pool) => {
|
||||||
|
info!("database connection pool initialized");
|
||||||
|
pool
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error_panic!("database connection pool failed to initialize: {}", e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// // schema init
|
||||||
|
// schema::init(&db_pool).await;
|
||||||
|
|
||||||
|
ModuleTree::new()
|
||||||
|
.add_module(Blog {
|
||||||
|
db_handler: db_pool,
|
||||||
|
})
|
||||||
|
.blocking_serve()
|
||||||
|
.await
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#[allow(dead_code)]
|
||||||
|
mod constants;
|
||||||
|
|
||||||
|
mod scheduler;
|
||||||
|
|
||||||
|
mod init;
|
||||||
|
mod modules;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
use crate::init::{logger, start};
|
||||||
|
|
||||||
|
const BUILD_ID: &str = env!("BUILD_ID");
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
println!("{}", BUILD_ID);
|
||||||
|
|
||||||
|
init::load_env();
|
||||||
|
let _guards = logger::init();
|
||||||
|
|
||||||
|
start().await;
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
// todo: remove exception tag
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub mod posts;
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
use crate::{
|
||||||
|
constants::blog::PAGE_SIZE,
|
||||||
|
err_resp_log,
|
||||||
|
utils::axum_response::{err_resp, succ_resp},
|
||||||
|
};
|
||||||
|
|
||||||
|
use axum::{
|
||||||
|
extract::{Query, State},
|
||||||
|
http::StatusCode,
|
||||||
|
response::IntoResponse,
|
||||||
|
};
|
||||||
|
use chrono::NaiveDate;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::json;
|
||||||
|
use sqlx::{FromRow, PgPool, query_as};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(FromRow, Serialize)]
|
||||||
|
struct PostBriefModel {
|
||||||
|
id: Uuid,
|
||||||
|
identifier: String,
|
||||||
|
title: String,
|
||||||
|
subtitle: String,
|
||||||
|
tags: Vec<String>,
|
||||||
|
category: String,
|
||||||
|
date_created: NaiveDate,
|
||||||
|
date_updated: Option<NaiveDate>,
|
||||||
|
header_img: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize)]
|
||||||
|
pub struct Pagination {
|
||||||
|
page: Option<u32>,
|
||||||
|
page_size: Option<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct RespPagination {
|
||||||
|
page: u32,
|
||||||
|
page_size: u32,
|
||||||
|
page_count: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct Response {
|
||||||
|
posts: Vec<PostBriefModel>,
|
||||||
|
pagination: RespPagination,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(FromRow)]
|
||||||
|
struct UuidRow {
|
||||||
|
id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
const GET_POST_WITH_BRIEF_MODEL: &str = include_str!("get_post_with_brief_model.sql");
|
||||||
|
|
||||||
|
pub async fn handler(
|
||||||
|
State(db): State<PgPool>,
|
||||||
|
Query(pagination): Query<Pagination>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
let page = pagination.page.unwrap_or(1);
|
||||||
|
let page_size = pagination.page_size.unwrap_or(PAGE_SIZE);
|
||||||
|
let page_count = match sqlx::query_scalar::<_, i64>("SELECT COUNT(*) FROM posts")
|
||||||
|
.fetch_one(&db)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(post_count) => ((post_count as f64) / (page_size as f64)).ceil() as u32,
|
||||||
|
Err(e) => return err_resp_log!(StatusCode::INTERNAL_SERVER_ERROR, &e.to_string()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let pagination = RespPagination {
|
||||||
|
page,
|
||||||
|
page_size,
|
||||||
|
page_count,
|
||||||
|
};
|
||||||
|
|
||||||
|
if page > page_count {
|
||||||
|
return err_resp(
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
"page number should not be greater than page count",
|
||||||
|
Some(json!({"pagination": pagination})),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let offset = page_size * (page - 1);
|
||||||
|
|
||||||
|
let ids = match query_as::<_, UuidRow>(
|
||||||
|
"SELECT id FROM posts ORDER BY date_created DESC LIMIT $1 OFFSET $2",
|
||||||
|
)
|
||||||
|
.bind(page_size as i32)
|
||||||
|
.bind(offset as i32)
|
||||||
|
.fetch_all(&db)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(res) => res.into_iter().map(|item| item.id).collect::<Vec<_>>(),
|
||||||
|
Err(e) => return err_resp_log!(StatusCode::INTERNAL_SERVER_ERROR, &e.to_string()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let posts = match query_as::<_, PostBriefModel>(GET_POST_WITH_BRIEF_MODEL)
|
||||||
|
.bind(&ids)
|
||||||
|
.fetch_all(&db)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(posts) => posts,
|
||||||
|
Err(e) => return err_resp_log!(StatusCode::INTERNAL_SERVER_ERROR, &e.to_string()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let resp = Response { posts, pagination };
|
||||||
|
succ_resp(StatusCode::OK, serde_json::to_value(resp).unwrap())
|
||||||
|
}
|
||||||
@@ -0,0 +1,132 @@
|
|||||||
|
use crate::{err_resp_log, utils::axum_response::succ_resp};
|
||||||
|
|
||||||
|
use axum::{
|
||||||
|
extract::{Path, State},
|
||||||
|
http::StatusCode,
|
||||||
|
response::IntoResponse,
|
||||||
|
};
|
||||||
|
use chrono::NaiveDate;
|
||||||
|
use futures::future::OptionFuture;
|
||||||
|
use serde_json::Value;
|
||||||
|
use sqlx::{FromRow, PgPool, query_as};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
const SELECT_POST_BY_UUID: &str = include_str!("get_post_by_uuid.sql");
|
||||||
|
const SELECT_POST_BY_STR_ID: &str = include_str!("get_post_by_str_id.sql");
|
||||||
|
const SELECT_POST_ADJACENT_IDS: &str = include_str!("get_post_adjacent_ids.sql");
|
||||||
|
const SELECT_POST_WITH_ADJACENT_MODEL: &str = include_str!("get_post_with_adjacent_model.sql");
|
||||||
|
|
||||||
|
#[derive(serde::Serialize, FromRow)]
|
||||||
|
struct SelectedPost {
|
||||||
|
id: Uuid,
|
||||||
|
identifier: String,
|
||||||
|
title: String,
|
||||||
|
subtitle: String,
|
||||||
|
status: String,
|
||||||
|
date_created: NaiveDate,
|
||||||
|
date_updated: Option<NaiveDate>,
|
||||||
|
category: String,
|
||||||
|
tags: Vec<String>,
|
||||||
|
content: String,
|
||||||
|
metadata: Option<Value>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize, FromRow)]
|
||||||
|
struct AdjacentPost {
|
||||||
|
id: Uuid,
|
||||||
|
identifier: String,
|
||||||
|
title: String,
|
||||||
|
subtitle: String,
|
||||||
|
date_created: NaiveDate,
|
||||||
|
category: String,
|
||||||
|
tags: Vec<String>,
|
||||||
|
header_img: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(FromRow, Debug)]
|
||||||
|
struct AdjacentIds {
|
||||||
|
prev: Option<Uuid>,
|
||||||
|
next: Option<Uuid>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize, Default)]
|
||||||
|
struct AdjacentPosts {
|
||||||
|
prev: Option<AdjacentPost>,
|
||||||
|
next: Option<AdjacentPost>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize)]
|
||||||
|
struct ResponseModel {
|
||||||
|
post: SelectedPost,
|
||||||
|
adjacent: AdjacentPosts,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn handler(
|
||||||
|
State(db_connection): State<PgPool>,
|
||||||
|
Path(identifier): Path<String>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
let post = match if let Ok(id) = identifier.parse::<Uuid>() {
|
||||||
|
select_post_by_uuid(&db_connection, id).await
|
||||||
|
} else {
|
||||||
|
select_post_by_str_id(&db_connection, &identifier).await
|
||||||
|
} {
|
||||||
|
Ok(post) => post,
|
||||||
|
Err(e) => {
|
||||||
|
return match e {
|
||||||
|
sqlx::Error::RowNotFound => {
|
||||||
|
err_resp_log!(StatusCode::NOT_FOUND, "no matching post")
|
||||||
|
}
|
||||||
|
_ => err_resp_log!(StatusCode::INTERNAL_SERVER_ERROR, &e.to_string()),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let adj_ids = query_as::<_, AdjacentIds>(SELECT_POST_ADJACENT_IDS)
|
||||||
|
.bind(post.id)
|
||||||
|
.fetch_one(&db_connection)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let adjacent_prev = OptionFuture::from(
|
||||||
|
adj_ids
|
||||||
|
.prev
|
||||||
|
.map(|id| select_post_with_adjacent_model(&db_connection, id)),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.flatten();
|
||||||
|
let adjacent_next = OptionFuture::from(
|
||||||
|
adj_ids
|
||||||
|
.next
|
||||||
|
.map(|id| select_post_with_adjacent_model(&db_connection, id)),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.flatten();
|
||||||
|
let adjacent = AdjacentPosts {
|
||||||
|
prev: adjacent_prev,
|
||||||
|
next: adjacent_next,
|
||||||
|
};
|
||||||
|
|
||||||
|
let response = ResponseModel { post, adjacent };
|
||||||
|
succ_resp(StatusCode::OK, serde_json::to_value(response).unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn select_post_by_uuid(db: &PgPool, id: Uuid) -> Result<SelectedPost, sqlx::Error> {
|
||||||
|
query_as::<_, SelectedPost>(SELECT_POST_BY_UUID)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_one(db)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn select_post_by_str_id(db: &PgPool, id: &str) -> Result<SelectedPost, sqlx::Error> {
|
||||||
|
query_as::<_, SelectedPost>(SELECT_POST_BY_STR_ID)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_one(db)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn select_post_with_adjacent_model(db: &PgPool, id: Uuid) -> Option<AdjacentPost> {
|
||||||
|
query_as(SELECT_POST_WITH_ADJACENT_MODEL)
|
||||||
|
.bind(id)
|
||||||
|
.fetch_optional(db)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
WITH target_create_date AS (SELECT p.date_created AS date FROM posts p WHERE p.id = $1)
|
||||||
|
SELECT (SELECT p.id
|
||||||
|
FROM posts p
|
||||||
|
WHERE p.date_created < t.date
|
||||||
|
ORDER BY p.date_created DESC
|
||||||
|
LIMIT 1) AS prev,
|
||||||
|
(SELECT p.id
|
||||||
|
FROM posts p
|
||||||
|
WHERE p.date_created > t.date
|
||||||
|
ORDER BY p.date_created
|
||||||
|
LIMIT 1) AS next
|
||||||
|
FROM target_create_date t
|
||||||
|
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
SELECT p.id,
|
||||||
|
p.identifier,
|
||||||
|
p.title,
|
||||||
|
p.subtitle,
|
||||||
|
p.status,
|
||||||
|
p.date_created,
|
||||||
|
p.date_updated,
|
||||||
|
c.name AS category,
|
||||||
|
(SELECT array_agg(t.name)
|
||||||
|
FROM tags t
|
||||||
|
JOIN post_tag pt ON pt.tag = t.id
|
||||||
|
WHERE pt.post = p.id) AS tags,
|
||||||
|
p.content,
|
||||||
|
(SELECT json_object_agg(m.meta_key, m.meta_value)
|
||||||
|
FROM metadata m
|
||||||
|
WHERE m.pid = p.id) AS metadata
|
||||||
|
FROM posts p
|
||||||
|
JOIN categories c ON c.id = p.category
|
||||||
|
WHERE p.identifier = $1;
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
SELECT p.id,
|
||||||
|
p.identifier,
|
||||||
|
p.title,
|
||||||
|
p.subtitle,
|
||||||
|
p.status,
|
||||||
|
p.date_created,
|
||||||
|
p.date_updated,
|
||||||
|
c.name AS category,
|
||||||
|
(SELECT array_agg(t.name)
|
||||||
|
FROM tags t
|
||||||
|
JOIN post_tag pt ON pt.tag = t.id
|
||||||
|
WHERE pt.post = p.id) AS tags,
|
||||||
|
p.content,
|
||||||
|
(SELECT json_object_agg(m.meta_key, m.meta_value)
|
||||||
|
FROM metadata m
|
||||||
|
WHERE m.pid = p.id) AS metadata
|
||||||
|
FROM posts p
|
||||||
|
JOIN categories c ON c.id = p.category
|
||||||
|
WHERE p.id = $1;
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
SELECT p.id,
|
||||||
|
identifier,
|
||||||
|
title,
|
||||||
|
subtitle,
|
||||||
|
date_created,
|
||||||
|
c.name AS category,
|
||||||
|
(SELECT array_agg(t.name)
|
||||||
|
FROM tags t
|
||||||
|
JOIN post_tag pt ON pt.tag = t.id
|
||||||
|
WHERE pt.post = p.id) AS tags,
|
||||||
|
(SELECT m.meta_value
|
||||||
|
FROM metadata m
|
||||||
|
WHERE m.pid = p.id
|
||||||
|
AND m.meta_key = 'header_img') AS header_img
|
||||||
|
FROM posts p
|
||||||
|
JOIN public.categories c on c.id = p.category
|
||||||
|
WHERE p.id = $1
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
SELECT p.id,
|
||||||
|
identifier,
|
||||||
|
title,
|
||||||
|
subtitle,
|
||||||
|
date_created,
|
||||||
|
date_updated,
|
||||||
|
c.name AS category,
|
||||||
|
(SELECT array_agg(t.name)
|
||||||
|
FROM tags t
|
||||||
|
JOIN post_tag pt ON pt.tag = t.id
|
||||||
|
WHERE pt.post = p.id) AS tags,
|
||||||
|
(SELECT m.meta_value
|
||||||
|
FROM metadata m
|
||||||
|
WHERE m.pid = p.id
|
||||||
|
AND m.meta_key = 'header_img') AS header_img
|
||||||
|
FROM posts p
|
||||||
|
JOIN public.categories c on c.id = p.category
|
||||||
|
WHERE p.id = ANY ($1)
|
||||||
|
ORDER BY p.date_created DESC;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod get;
|
||||||
|
pub mod get_identifier;
|
||||||
|
pub mod pagination;
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
use crate::{constants::blog::PAGE_SIZE, err_resp_log, utils::axum_response::succ_resp};
|
||||||
|
|
||||||
|
use axum::{extract::State, http::StatusCode, response::Response};
|
||||||
|
use serde_json::{Value, json};
|
||||||
|
use sqlx::{FromRow, PgPool, query_as};
|
||||||
|
|
||||||
|
#[derive(FromRow)]
|
||||||
|
struct DBPaginationRow {
|
||||||
|
page_count: i32,
|
||||||
|
post_count: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct PaginationResponse {
|
||||||
|
page_count: i32,
|
||||||
|
post_count: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<DBPaginationRow> for PaginationResponse {
|
||||||
|
fn from(value: DBPaginationRow) -> Self {
|
||||||
|
Self {
|
||||||
|
page_count: value.page_count,
|
||||||
|
post_count: value.post_count,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<PaginationResponse> for Value {
|
||||||
|
fn from(value: PaginationResponse) -> Self {
|
||||||
|
json!({
|
||||||
|
"page_count": value.page_count,
|
||||||
|
"post_count": value.post_count
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn handler(State(db_connection): State<PgPool>) -> Response<String> {
|
||||||
|
match query_as::<_, DBPaginationRow>(
|
||||||
|
r#"
|
||||||
|
SELECT
|
||||||
|
cast(ceil(count(*) / cast($1 AS FLOAT)) AS INTEGER) AS page_count,
|
||||||
|
count(*) AS post_count
|
||||||
|
FROM posts;
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(PAGE_SIZE as i64)
|
||||||
|
.fetch_one(&db_connection)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(res) => succ_resp(StatusCode::OK, PaginationResponse::from(res).into()),
|
||||||
|
Err(e) => err_resp_log!(StatusCode::INTERNAL_SERVER_ERROR, &e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,245 @@
|
|||||||
|
use crate::modules::blog::components::check_update::{
|
||||||
|
changes::{Change, Create, Delete, Move, Update},
|
||||||
|
error::Error,
|
||||||
|
utils::{parse_naive_date_str, parse_post_identifier},
|
||||||
|
};
|
||||||
|
|
||||||
|
use futures::{
|
||||||
|
FutureExt,
|
||||||
|
future::{BoxFuture, join_all},
|
||||||
|
};
|
||||||
|
use sqlx::{PgPool, Postgres, Row, Transaction, query};
|
||||||
|
use tracing::error;
|
||||||
|
|
||||||
|
const DELETE_POST_BY_ID: &str = include_str!("sql/delete_post_by_id.sql");
|
||||||
|
const GET_TAG_ID: &str = include_str!("sql/get_tag_id.sql");
|
||||||
|
const GET_CATEGORY_ID: &str = include_str!("sql/get_category_id.sql");
|
||||||
|
const CREATE_RETURN_TAG_ID: &str = include_str!("sql/create_return_tag_id.sql");
|
||||||
|
const CREATE_RETURN_CATEGORY_ID: &str = include_str!("sql/create_return_category_id.sql");
|
||||||
|
const CREATE_POST_GENERAL: &str = include_str!("sql/create_post_general.sql");
|
||||||
|
const CREATE_POST_TAGS: &str = include_str!("sql/create_post_tags.sql");
|
||||||
|
const CREATE_POST_META: &str = include_str!("sql/create_post_meta.sql");
|
||||||
|
|
||||||
|
pub async fn apply_deltas(db_conn: &PgPool, changes: Vec<Change>) {
|
||||||
|
let handlers = changes
|
||||||
|
.into_iter()
|
||||||
|
.map(|change| match change {
|
||||||
|
Change::Delete(del) => handle_delete(db_conn, del).boxed(),
|
||||||
|
Change::Move(mv) => handle_move(db_conn, mv).boxed(),
|
||||||
|
Change::Update(update) => handle_update(db_conn, update).boxed(),
|
||||||
|
Change::Create(create) => handle_create(db_conn, create).boxed(),
|
||||||
|
})
|
||||||
|
.collect::<Vec<BoxFuture<'_, ()>>>();
|
||||||
|
|
||||||
|
join_all(handlers).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_delete(db: &PgPool, del: Delete) {
|
||||||
|
if let Err(e) = query(DELETE_POST_BY_ID).bind(del.uuid).execute(db).await {
|
||||||
|
error!(
|
||||||
|
"failed to delete post: {}, {}",
|
||||||
|
del.uuid.to_string(),
|
||||||
|
e.to_string()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_move(db: &PgPool, mv: Move) {
|
||||||
|
handle_delete(
|
||||||
|
db,
|
||||||
|
Delete {
|
||||||
|
uuid: mv.uuid,
|
||||||
|
path: Some(mv.from),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
handle_create(
|
||||||
|
db,
|
||||||
|
Create {
|
||||||
|
uuid: mv.uuid,
|
||||||
|
path: mv.to,
|
||||||
|
payload: mv.payload,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_update(db: &PgPool, update: Update) {
|
||||||
|
handle_delete(
|
||||||
|
db,
|
||||||
|
Delete {
|
||||||
|
uuid: update.uuid,
|
||||||
|
path: Some(update.path.clone()),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
handle_create(
|
||||||
|
db,
|
||||||
|
Create {
|
||||||
|
uuid: update.uuid,
|
||||||
|
path: update.path,
|
||||||
|
payload: update.payload,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_create(db: &PgPool, create: Create) {
|
||||||
|
let mut tx = db.begin().await.unwrap();
|
||||||
|
|
||||||
|
let identifier = parse_post_identifier(&create.path).unwrap();
|
||||||
|
|
||||||
|
let tags_json = create.payload.meta["tags"].clone();
|
||||||
|
let tags: Vec<_> = if let Some(tags) = tags_json.as_array() {
|
||||||
|
tags.iter().filter_map(|tag| tag.as_str()).collect()
|
||||||
|
} else {
|
||||||
|
vec![]
|
||||||
|
};
|
||||||
|
let mut tag_ids = vec![];
|
||||||
|
for tag in tags {
|
||||||
|
match get_or_create_tag_id(&mut tx, tag).await {
|
||||||
|
Ok(tid) => tag_ids.push(tid),
|
||||||
|
Err(e) => {
|
||||||
|
error!("failed to fetch or create tag ({}): {}", tag, e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let category_json = create.payload.meta["category"].clone();
|
||||||
|
let category = category_json.as_str().unwrap_or("Uncategorized");
|
||||||
|
let category_id = match get_or_create_category_id(&mut tx, category).await {
|
||||||
|
Ok(id) => id,
|
||||||
|
Err(e) => {
|
||||||
|
error!(
|
||||||
|
"failed to fetch category id for post {}: {}",
|
||||||
|
&identifier, e
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const KEY_TO_SKIP: &[&str] = &[
|
||||||
|
"category", "status", "id", "date", "update", "tags", "title", "subtitle",
|
||||||
|
];
|
||||||
|
let (post_meta_key, post_meta_value): (Vec<String>, Vec<String>) =
|
||||||
|
if let Some(kv) = create.payload.meta.as_object() {
|
||||||
|
kv.iter()
|
||||||
|
.filter(|(k, _)| !KEY_TO_SKIP.contains(&&***k))
|
||||||
|
.map(|(k, v)| (k.clone(), v.as_str().unwrap().to_string()))
|
||||||
|
.collect()
|
||||||
|
} else {
|
||||||
|
(vec![], vec![])
|
||||||
|
};
|
||||||
|
|
||||||
|
let meta = create.payload.meta;
|
||||||
|
let post_id = create.uuid;
|
||||||
|
let post_identifier = &identifier;
|
||||||
|
let post_title = meta
|
||||||
|
.get("title")
|
||||||
|
.map(|t| t.as_str().unwrap())
|
||||||
|
.unwrap_or("Untitled");
|
||||||
|
let post_subtitle = meta
|
||||||
|
.get("subtitle")
|
||||||
|
.map(|t| t.as_str().unwrap())
|
||||||
|
.unwrap_or("");
|
||||||
|
let post_date_created = if let Some(Some(date)) = meta
|
||||||
|
.get("date")
|
||||||
|
.map(|d| parse_naive_date_str(d.as_str().unwrap_or("")))
|
||||||
|
{
|
||||||
|
date
|
||||||
|
} else {
|
||||||
|
error!("failed to parse `date` for post: {}", post_identifier);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let post_date_updated = meta
|
||||||
|
.get("update")
|
||||||
|
.map(|d| parse_naive_date_str(d.as_str().unwrap_or("")))
|
||||||
|
.unwrap_or(None);
|
||||||
|
let post_status = meta.get("status").map(|v| v.as_str()).unwrap_or(None);
|
||||||
|
let post_content = create.payload.content;
|
||||||
|
let post_hash = create.payload.hash;
|
||||||
|
|
||||||
|
if let Err(e) = query(CREATE_POST_GENERAL)
|
||||||
|
.bind(post_id)
|
||||||
|
.bind(post_identifier)
|
||||||
|
.bind(post_title)
|
||||||
|
.bind(post_subtitle)
|
||||||
|
.bind(post_date_created)
|
||||||
|
.bind(post_date_updated)
|
||||||
|
.bind(post_status)
|
||||||
|
.bind(post_content)
|
||||||
|
.bind(post_hash)
|
||||||
|
.bind(category_id)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
error!("failed to insert post {}: {}", &identifier, e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Err(e) = query(CREATE_POST_TAGS)
|
||||||
|
.bind(post_id)
|
||||||
|
.bind(tag_ids)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
error!("failed to insert post {}: {}", &identifier, e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Err(e) = query(CREATE_POST_META)
|
||||||
|
.bind(post_id)
|
||||||
|
.bind(post_meta_key)
|
||||||
|
.bind(post_meta_value)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
error!("failed to insert post {}: {}", &identifier, e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.commit().await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_or_create_tag_id(
|
||||||
|
db: &mut Transaction<'_, Postgres>,
|
||||||
|
tag_name: &str,
|
||||||
|
) -> Result<i32, Error> {
|
||||||
|
get_create_id(db, tag_name, GET_TAG_ID, CREATE_RETURN_TAG_ID).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_or_create_category_id(
|
||||||
|
db: &mut Transaction<'_, Postgres>,
|
||||||
|
category_name: &str,
|
||||||
|
) -> Result<i32, Error> {
|
||||||
|
get_create_id(
|
||||||
|
db,
|
||||||
|
category_name,
|
||||||
|
GET_CATEGORY_ID,
|
||||||
|
CREATE_RETURN_CATEGORY_ID,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_create_id(
|
||||||
|
db: &mut Transaction<'_, Postgres>,
|
||||||
|
field_name: &str,
|
||||||
|
fetch_query: &str,
|
||||||
|
create_fetch_query: &str,
|
||||||
|
) -> Result<i32, Error> {
|
||||||
|
if let Some(id) = query(fetch_query)
|
||||||
|
.bind(field_name)
|
||||||
|
.fetch_optional(&mut **db)
|
||||||
|
.await?
|
||||||
|
{
|
||||||
|
Ok(id.get("id"))
|
||||||
|
} else {
|
||||||
|
let id = query(create_fetch_query)
|
||||||
|
.bind(field_name)
|
||||||
|
.fetch_one(&mut **db)
|
||||||
|
.await?
|
||||||
|
.get("id");
|
||||||
|
Ok(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use markdown::MdFile;
|
||||||
|
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Change {
|
||||||
|
Delete(Delete),
|
||||||
|
Move(Move),
|
||||||
|
Update(Update),
|
||||||
|
Create(Create),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Delete {
|
||||||
|
pub uuid: Uuid,
|
||||||
|
pub path: Option<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Move {
|
||||||
|
pub uuid: Uuid,
|
||||||
|
pub from: PathBuf,
|
||||||
|
pub to: PathBuf,
|
||||||
|
pub payload: MdFile,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Update {
|
||||||
|
pub uuid: Uuid,
|
||||||
|
pub path: PathBuf,
|
||||||
|
pub payload: MdFile,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Create {
|
||||||
|
pub uuid: Uuid,
|
||||||
|
pub path: PathBuf,
|
||||||
|
pub payload: MdFile,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Default)]
|
||||||
|
pub struct CreateDelete2UpdateSlot {
|
||||||
|
pub create: Option<Create>,
|
||||||
|
pub delete: Option<Delete>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CreateDelete2UpdateSlot {
|
||||||
|
pub fn to_change(&self) -> Option<Change> {
|
||||||
|
match (&self.create, &self.delete) {
|
||||||
|
(Some(create), Some(delete)) => Some(Change::Move(Move {
|
||||||
|
uuid: create.uuid,
|
||||||
|
from: delete.path.clone()?,
|
||||||
|
to: create.path.clone(),
|
||||||
|
payload: create.payload.clone(),
|
||||||
|
})),
|
||||||
|
(Some(create), None) => Some(Change::Create(create.clone())),
|
||||||
|
(None, Some(delete)) => Some(Change::Delete(delete.clone())),
|
||||||
|
(None, None) => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
use markdown::error::Error as MarkdownError;
|
||||||
|
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
#[error("markdown file error")]
|
||||||
|
MdFile(#[from] MarkdownError),
|
||||||
|
|
||||||
|
#[error("db error")]
|
||||||
|
Db(#[from] sqlx::Error),
|
||||||
|
|
||||||
|
#[error("git error")]
|
||||||
|
Git2(#[from] git2::Error),
|
||||||
|
|
||||||
|
#[error("git remote url mismatch")]
|
||||||
|
GitUrlMismatch,
|
||||||
|
}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
pub mod apply;
|
||||||
|
pub mod changes;
|
||||||
|
mod error;
|
||||||
|
mod pull;
|
||||||
|
pub mod utils;
|
||||||
|
|
||||||
|
use std::{env, path::PathBuf, sync::Arc};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
constants::config::general::{default_value, var_name},
|
||||||
|
modules::blog::components::{
|
||||||
|
check_update::{apply::apply_deltas, pull::fetch_deltas},
|
||||||
|
static_rsc::GIT_WORK_DIR,
|
||||||
|
},
|
||||||
|
scheduler::task_func::AsyncTaskFunc,
|
||||||
|
};
|
||||||
|
use macros::panic_with_log;
|
||||||
|
|
||||||
|
use sqlx::PgPool;
|
||||||
|
use tracing::{Level, log::error, trace};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Args {
|
||||||
|
db_connection: PgPool,
|
||||||
|
git_work_dir: PathBuf,
|
||||||
|
git_url: String,
|
||||||
|
git_remote_name: String,
|
||||||
|
git_remote_branch: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn task(db_connection: PgPool) -> impl AsyncTaskFunc {
|
||||||
|
// clone & check update
|
||||||
|
let git_url = match env::var(var_name::BLOG_GIT_URL) {
|
||||||
|
Ok(url) => url,
|
||||||
|
Err(e) => {
|
||||||
|
panic_with_log!(
|
||||||
|
Level::ERROR,
|
||||||
|
"failed to read `{}`: {}",
|
||||||
|
var_name::BLOG_GIT_URL,
|
||||||
|
e
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let git_remote_name = env::var(var_name::BLOG_GIT_REMOTE_NAME)
|
||||||
|
.unwrap_or(default_value::BLOG_GIT_REMOTE_NAME.to_string());
|
||||||
|
let git_remote_branch = env::var(var_name::BLOG_GIT_REMOTE_BRANCH)
|
||||||
|
.unwrap_or(default_value::BLOG_GIT_REMOTE_BRANCH.to_string());
|
||||||
|
|
||||||
|
let arc_args = Arc::new(Args {
|
||||||
|
db_connection,
|
||||||
|
git_work_dir: GIT_WORK_DIR.clone(),
|
||||||
|
git_url,
|
||||||
|
git_remote_name,
|
||||||
|
git_remote_branch,
|
||||||
|
});
|
||||||
|
|
||||||
|
move || Box::pin(workflow(arc_args.clone()))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn workflow(args: Arc<Args>) {
|
||||||
|
let deltas = match fetch_deltas(
|
||||||
|
&args.git_url,
|
||||||
|
&args.git_remote_name,
|
||||||
|
&args.git_remote_branch,
|
||||||
|
&args.git_work_dir,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(updates) => updates,
|
||||||
|
Err(e) => {
|
||||||
|
error!("delta parsing error: {}", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if deltas.is_empty() {
|
||||||
|
trace!("blog git already up-to-date");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
apply_deltas(&args.db_connection, deltas).await;
|
||||||
|
}
|
||||||
@@ -0,0 +1,175 @@
|
|||||||
|
use std::{collections::HashMap, fs::remove_dir_all, path::Path, path::PathBuf};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
modules::blog::components::{
|
||||||
|
check_update::{
|
||||||
|
changes::{Change, Create, CreateDelete2UpdateSlot, Delete, Update},
|
||||||
|
error::Error,
|
||||||
|
utils::extract_post_id,
|
||||||
|
},
|
||||||
|
static_rsc::BLOG_POST_PATH_PATTERN,
|
||||||
|
},
|
||||||
|
utils::{git, git::FileDelta},
|
||||||
|
};
|
||||||
|
use markdown::MdFile;
|
||||||
|
|
||||||
|
use git2::{Delta, Repository};
|
||||||
|
use tracing::{error, warn};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub async fn fetch_deltas(
|
||||||
|
git_url: &str,
|
||||||
|
git_remote_name: &str,
|
||||||
|
git_remote_branch: &str,
|
||||||
|
git_work_dir: &PathBuf,
|
||||||
|
) -> Result<Vec<Change>, Error> {
|
||||||
|
let repo = match Repository::open(git_work_dir) {
|
||||||
|
Ok(repo) => repo,
|
||||||
|
Err(_) => match Repository::clone(git_url, git_work_dir) {
|
||||||
|
Ok(repo) => repo,
|
||||||
|
Err(e) => {
|
||||||
|
error!("failed to clone blog content git({}): {}", &git_url, e);
|
||||||
|
return Err(e.into());
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut remote = match repo.find_remote(git_remote_name) {
|
||||||
|
Ok(remote) => remote,
|
||||||
|
Err(e) => {
|
||||||
|
remove_dir_all(git_work_dir).unwrap();
|
||||||
|
warn!(
|
||||||
|
"cannot find git remote ({}), reinitializing: {}",
|
||||||
|
git_remote_name, e
|
||||||
|
);
|
||||||
|
return Err(e.into());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if remote.url().is_some_and(|url| url != git_url) {
|
||||||
|
remove_dir_all(git_work_dir).unwrap();
|
||||||
|
warn!(
|
||||||
|
"git remote url mismatch (expected: {}, actual: {}), reinitializing",
|
||||||
|
git_url,
|
||||||
|
remote.url().unwrap()
|
||||||
|
);
|
||||||
|
return Err(Error::GitUrlMismatch);
|
||||||
|
}
|
||||||
|
|
||||||
|
let fetched_commit = git::do_fetch(&repo, &[git_remote_branch], &mut remote)?;
|
||||||
|
let commit_deltas = git::calculate_diff(
|
||||||
|
&repo,
|
||||||
|
&repo.reference_to_annotated_commit(&repo.head()?)?,
|
||||||
|
&fetched_commit,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
// dbg!(&commit_deltas);
|
||||||
|
let mut changes: Vec<Change> = vec![];
|
||||||
|
let mut non_delete_deltas: Vec<FileDelta> = vec![];
|
||||||
|
|
||||||
|
// process deleted deltas before merging commit to prevent "file not found" os error
|
||||||
|
for delta in commit_deltas.into_iter() {
|
||||||
|
let relative_file_path = delta
|
||||||
|
.new_path
|
||||||
|
.clone()
|
||||||
|
.expect("cannot parse modified post `new_path`");
|
||||||
|
|
||||||
|
if !should_delta_tracked(&relative_file_path) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if delta.status == Delta::Deleted {
|
||||||
|
let file_path = &git_work_dir.join(relative_file_path);
|
||||||
|
if let Some(id) = extract_post_id(&MdFile::from_file(file_path)?) {
|
||||||
|
changes.push(Change::Delete(Delete {
|
||||||
|
uuid: id,
|
||||||
|
path: Some(file_path.clone()),
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
error!("failed to parse uuid for file {}", file_path.display());
|
||||||
|
// todo: add to unresolved error list
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
non_delete_deltas.push(delta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
git::do_merge(&repo, git_remote_branch, fetched_commit)?;
|
||||||
|
|
||||||
|
// process adds and modifies deltas
|
||||||
|
for delta in non_delete_deltas.into_iter() {
|
||||||
|
let file_path = &git_work_dir.join(
|
||||||
|
delta
|
||||||
|
.new_path
|
||||||
|
.expect("cannot parse modified post `new_path`"),
|
||||||
|
);
|
||||||
|
match delta.status {
|
||||||
|
Delta::Added => {
|
||||||
|
let md_file = MdFile::from_file(file_path)?;
|
||||||
|
if let Some(id) = extract_post_id(&md_file) {
|
||||||
|
changes.push(Change::Create(Create {
|
||||||
|
uuid: id,
|
||||||
|
path: file_path.clone(),
|
||||||
|
payload: md_file,
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
error!("failed to parse uuid for file {}", file_path.display());
|
||||||
|
// todo: add to unresolved error list
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Delta::Modified => {
|
||||||
|
let md_file = MdFile::from_file(file_path)?;
|
||||||
|
if let Some(id) = extract_post_id(&md_file) {
|
||||||
|
changes.push(Change::Update(Update {
|
||||||
|
uuid: id,
|
||||||
|
path: file_path.clone(),
|
||||||
|
payload: md_file,
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
error!("failed to parse uuid for file {}", file_path.display());
|
||||||
|
// todo: add to unresolved error list
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Delta::Deleted => continue,
|
||||||
|
_ => {
|
||||||
|
error!("unsupported delta type on file {}", file_path.display());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// merge adds and deletes deltas to moves
|
||||||
|
let changes = merge_changes(changes);
|
||||||
|
|
||||||
|
Ok(changes)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn merge_changes(changes: Vec<Change>) -> Vec<Change> {
|
||||||
|
let mut slots: HashMap<Uuid, CreateDelete2UpdateSlot> = HashMap::new();
|
||||||
|
let mut merged: Vec<Change> = Vec::new();
|
||||||
|
|
||||||
|
for change in changes {
|
||||||
|
match change {
|
||||||
|
Change::Create(create) => {
|
||||||
|
let id = create.uuid;
|
||||||
|
slots.entry(id).or_default().create = Some(create);
|
||||||
|
}
|
||||||
|
Change::Delete(delete) => {
|
||||||
|
let id = delete.uuid;
|
||||||
|
slots.entry(id).or_default().delete = Some(delete);
|
||||||
|
}
|
||||||
|
_ => merged.push(change),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for slot in slots.values() {
|
||||||
|
if let Some(change) = slot.to_change() {
|
||||||
|
merged.push(change);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
merged
|
||||||
|
}
|
||||||
|
|
||||||
|
fn should_delta_tracked(path: &Path) -> bool {
|
||||||
|
BLOG_POST_PATH_PATTERN.is_match(path.to_str().unwrap())
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
INSERT INTO posts
|
||||||
|
(id, identifier, title, subtitle, date_created, date_updated, status, content, hash, category)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, COALESCE($7, 'draft'), $8, $9, $10);
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
INSERT INTO metadata (pid, meta_key, meta_value)
|
||||||
|
SELECT $1, key, value
|
||||||
|
FROM UNNEST($2, $3) AS k(key, value);
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
INSERT INTO post_tag (post, tag)
|
||||||
|
SELECT $1, tag
|
||||||
|
FROM UNNEST($2) AS t(tag);
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
INSERT INTO categories(name)
|
||||||
|
VALUES ($1)
|
||||||
|
RETURNING id;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
INSERT INTO tags(name)
|
||||||
|
VALUES ($1)
|
||||||
|
RETURNING id;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
DELETE
|
||||||
|
FROM posts
|
||||||
|
WHERE id = $1;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
SELECT id
|
||||||
|
FROM categories
|
||||||
|
WHERE name = $1;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
SELECT id
|
||||||
|
FROM tags
|
||||||
|
WHERE name = $1;
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use crate::constants::{
|
||||||
|
config::general::{default_value, var_name},
|
||||||
|
time::DEFAULT_NAIVE_DATE_FORMAT,
|
||||||
|
};
|
||||||
|
|
||||||
|
use chrono::NaiveDate;
|
||||||
|
use dirs::home_dir;
|
||||||
|
use markdown::MdFile;
|
||||||
|
use tracing::warn;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub fn expand_path(path: String) -> String {
|
||||||
|
if path.starts_with('~') {
|
||||||
|
home_dir()
|
||||||
|
.map(|dir| {
|
||||||
|
dir.join(path.strip_prefix("~/").unwrap())
|
||||||
|
.into_os_string()
|
||||||
|
.into_string()
|
||||||
|
.unwrap()
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
warn!(
|
||||||
|
"failed to parse env var `{}`, using default value `{}`",
|
||||||
|
var_name::WORK_DIR,
|
||||||
|
default_value::WORK_DIR
|
||||||
|
);
|
||||||
|
default_value::WORK_DIR.to_string()
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn extract_post_id(post: &MdFile) -> Option<Uuid> {
|
||||||
|
match post.meta["id"].as_str() {
|
||||||
|
Some(id) => Uuid::parse_str(id).ok(),
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_post_identifier(path: &Path) -> Option<String> {
|
||||||
|
if let Some(comp) = path.components().nth_back(1) {
|
||||||
|
comp.as_os_str().to_str().map(|s| s.to_string())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_naive_date_str(date_str: &str) -> Option<NaiveDate> {
|
||||||
|
NaiveDate::parse_from_str(date_str, DEFAULT_NAIVE_DATE_FORMAT).ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::modules::blog::components::check_update::utils::parse_naive_date_str;
|
||||||
|
use chrono::NaiveDate;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_naive_date() {
|
||||||
|
assert_eq!(
|
||||||
|
NaiveDate::from_ymd_opt(2015, 1, 1),
|
||||||
|
parse_naive_date_str("2015-1-01")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
fs,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
str::FromStr,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::modules::blog::components::{
|
||||||
|
check_update::{
|
||||||
|
changes::{Change, Create, Delete, Update},
|
||||||
|
utils::extract_post_id,
|
||||||
|
},
|
||||||
|
consistency_guard::{error::Error, utils::rec_find_dir_entries},
|
||||||
|
static_rsc::BLOG_POST_PATH_PATTERN,
|
||||||
|
};
|
||||||
|
use markdown::MdFile;
|
||||||
|
|
||||||
|
use sqlx::{FromRow, PgPool, query_as};
|
||||||
|
use tracing::error;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
const FETCH_POST_HASH: &str = include_str!("sql/fetch_post_hash.sql");
|
||||||
|
|
||||||
|
#[derive(Debug, FromRow)]
|
||||||
|
struct PostIdHash {
|
||||||
|
id: Uuid,
|
||||||
|
hash: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct PostPath {
|
||||||
|
id: Uuid,
|
||||||
|
path: PathBuf,
|
||||||
|
payload: MdFile,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct DeltaSlot {
|
||||||
|
local: Option<PostPath>,
|
||||||
|
db: Option<PostIdHash>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DeltaSlot {
|
||||||
|
pub fn to_change(&self) -> Option<Change> {
|
||||||
|
match (&self.local, &self.db) {
|
||||||
|
(Some(local), Some(db)) => {
|
||||||
|
if local.payload.hash == db.hash {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(Change::Update(Update {
|
||||||
|
uuid: local.id,
|
||||||
|
path: local.path.clone(),
|
||||||
|
payload: local.payload.clone(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(Some(local), None) => Some(Change::Create(Create {
|
||||||
|
uuid: local.id,
|
||||||
|
path: local.path.clone(),
|
||||||
|
payload: local.payload.clone(),
|
||||||
|
})),
|
||||||
|
(None, Some(db)) => Some(Change::Delete(Delete {
|
||||||
|
uuid: db.id,
|
||||||
|
path: None,
|
||||||
|
})),
|
||||||
|
(None, None) => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn compare_db_local_post_hash(
|
||||||
|
db: &PgPool,
|
||||||
|
git_work_dir: &Path,
|
||||||
|
) -> Result<Vec<Change>, Error> {
|
||||||
|
let db_post_hashes = query_as::<_, PostIdHash>(FETCH_POST_HASH)
|
||||||
|
.fetch_all(db)
|
||||||
|
.await?;
|
||||||
|
let local_posts = fetch_local_posts(git_work_dir)?;
|
||||||
|
calculate_post_changes(local_posts, db_post_hashes)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fetch_local_posts(git_work_dir: &Path) -> Result<Vec<PostPath>, Error> {
|
||||||
|
let mut post_hashes = vec![];
|
||||||
|
let entries = rec_find_dir_entries(&git_work_dir.join("posts"))?;
|
||||||
|
|
||||||
|
for entry in entries {
|
||||||
|
let relative_pth = entry.strip_prefix(git_work_dir).unwrap();
|
||||||
|
if !BLOG_POST_PATH_PATTERN.is_match(relative_pth.to_str().unwrap()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let post_str = fs::read_to_string(&entry)?;
|
||||||
|
let post = MdFile::from_str(&post_str)?;
|
||||||
|
let post_id = match extract_post_id(&post) {
|
||||||
|
None => {
|
||||||
|
error!(
|
||||||
|
"failed to parse uuid for file {}, skipping",
|
||||||
|
&entry.display()
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Some(v) => v,
|
||||||
|
};
|
||||||
|
post_hashes.push(PostPath {
|
||||||
|
id: post_id,
|
||||||
|
path: entry,
|
||||||
|
payload: post,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(post_hashes)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calculate_post_changes(local: Vec<PostPath>, db: Vec<PostIdHash>) -> Result<Vec<Change>, Error> {
|
||||||
|
let mut slots: HashMap<Uuid, DeltaSlot> = HashMap::new();
|
||||||
|
for local_post in local {
|
||||||
|
let id = local_post.id;
|
||||||
|
slots.entry(id).or_default().local = Some(local_post);
|
||||||
|
}
|
||||||
|
for db_post in db {
|
||||||
|
let id = db_post.id;
|
||||||
|
slots.entry(id).or_default().db = Some(db_post);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut changes = vec![];
|
||||||
|
for slot in slots.values() {
|
||||||
|
if let Some(change) = slot.to_change() {
|
||||||
|
changes.push(change);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(changes)
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
use std::io;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
#[error("db error")]
|
||||||
|
Db(#[from] sqlx::Error),
|
||||||
|
|
||||||
|
#[error("io error")]
|
||||||
|
Io(#[from] io::Error),
|
||||||
|
|
||||||
|
#[error("invalid path: {0}")]
|
||||||
|
InvalidPath(PathBuf),
|
||||||
|
|
||||||
|
#[error("invalid md file")]
|
||||||
|
MdFile(#[from] markdown::error::Error),
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
mod checker;
|
||||||
|
mod error;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
use std::{path::PathBuf, sync::Arc};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
modules::blog::components::{
|
||||||
|
check_update::apply::apply_deltas, consistency_guard::checker::compare_db_local_post_hash,
|
||||||
|
static_rsc::GIT_WORK_DIR,
|
||||||
|
},
|
||||||
|
scheduler::task_func::AsyncTaskFunc,
|
||||||
|
};
|
||||||
|
|
||||||
|
use sqlx::PgPool;
|
||||||
|
use tracing::{error, trace};
|
||||||
|
|
||||||
|
struct Args {
|
||||||
|
db_connection: PgPool,
|
||||||
|
git_work_dir: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn task(db_connection: PgPool) -> impl AsyncTaskFunc {
|
||||||
|
let args = Arc::new(Args {
|
||||||
|
db_connection,
|
||||||
|
git_work_dir: GIT_WORK_DIR.clone(),
|
||||||
|
});
|
||||||
|
move || Box::pin(workflow(args.clone()))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn workflow(args: Arc<Args>) {
|
||||||
|
let db = &args.db_connection;
|
||||||
|
let git_work_dir = &args.git_work_dir;
|
||||||
|
|
||||||
|
let changes = match compare_db_local_post_hash(db, git_work_dir).await {
|
||||||
|
Ok(c) => c,
|
||||||
|
Err(e) => {
|
||||||
|
error!("failed to compare db and local post list: {}", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
apply_deltas(db, changes).await;
|
||||||
|
trace!("local-database consistency checked");
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
SELECT id, hash
|
||||||
|
FROM posts
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
use std::{
|
||||||
|
fs,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::modules::blog::components::consistency_guard::error::Error;
|
||||||
|
|
||||||
|
pub fn rec_find_dir_entries(entry: &Path) -> Result<Vec<PathBuf>, Error> {
|
||||||
|
if !entry.exists() {
|
||||||
|
return Err(Error::InvalidPath(entry.to_path_buf()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut result = vec![];
|
||||||
|
|
||||||
|
if !entry.is_dir() {
|
||||||
|
return Ok(vec![entry.to_path_buf()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for entry in fs::read_dir(entry)? {
|
||||||
|
let entry = entry?;
|
||||||
|
let path = entry.path();
|
||||||
|
|
||||||
|
if path.is_dir() {
|
||||||
|
result.extend(rec_find_dir_entries(&path)?);
|
||||||
|
} else {
|
||||||
|
result.push(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::fs::{self, File};
|
||||||
|
use tempfile::TempDir;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_valid_directory() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let dir_path = temp_dir.path();
|
||||||
|
|
||||||
|
fs::create_dir(dir_path.join("subdir")).unwrap();
|
||||||
|
File::create(dir_path.join("file1.txt")).unwrap();
|
||||||
|
File::create(dir_path.join("subdir/file2.txt")).unwrap();
|
||||||
|
|
||||||
|
let entries = rec_find_dir_entries(dir_path).unwrap();
|
||||||
|
assert_eq!(entries.len(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_non_existent_path() {
|
||||||
|
let non_existent = PathBuf::from("/non/existent/path");
|
||||||
|
assert!(matches!(
|
||||||
|
rec_find_dir_entries(&non_existent),
|
||||||
|
Err(Error::InvalidPath(_))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_single_file() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let file_path = temp_dir.path().join("test.txt");
|
||||||
|
File::create(&file_path).unwrap();
|
||||||
|
|
||||||
|
let entries = rec_find_dir_entries(&file_path).unwrap();
|
||||||
|
assert_eq!(entries.len(), 1);
|
||||||
|
assert_eq!(entries[0], file_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_empty_directory() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let entries = rec_find_dir_entries(temp_dir.path()).unwrap();
|
||||||
|
assert!(entries.is_empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
pub mod check_update;
|
||||||
|
pub mod consistency_guard;
|
||||||
|
pub mod rag;
|
||||||
|
mod static_rsc;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
use std::{
|
||||||
|
env,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
sync::LazyLock,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
constants::config::general::{default_value, var_name},
|
||||||
|
modules::blog::components::check_update::utils::expand_path,
|
||||||
|
};
|
||||||
|
|
||||||
|
use regex::Regex;
|
||||||
|
use tracing::warn;
|
||||||
|
|
||||||
|
pub static BLOG_POST_PATH_PATTERN: LazyLock<Regex> =
|
||||||
|
LazyLock::new(|| Regex::new(r"^posts/\d{4}/[a-zA-Z0-9-]+/index\.md$").unwrap());
|
||||||
|
|
||||||
|
pub static GIT_WORK_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
|
||||||
|
let path = if let Ok(default_work_path) = env::var(var_name::WORK_DIR) {
|
||||||
|
expand_path(default_work_path)
|
||||||
|
} else {
|
||||||
|
let fallback_path = expand_path(default_value::WORK_DIR.to_string());
|
||||||
|
warn!(
|
||||||
|
"unset env var `{}`, using default path: {}/blog_git",
|
||||||
|
var_name::WORK_DIR,
|
||||||
|
fallback_path
|
||||||
|
);
|
||||||
|
fallback_path
|
||||||
|
};
|
||||||
|
Path::new(&path).join("blog_git")
|
||||||
|
});
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
mod api;
|
||||||
|
mod components;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
modules::{
|
||||||
|
Module,
|
||||||
|
blog::{
|
||||||
|
api::{posts, posts::get, posts::pagination},
|
||||||
|
components::{check_update, consistency_guard},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
scheduler::tasks::{CronTask, async_basic::AsyncBasic},
|
||||||
|
};
|
||||||
|
|
||||||
|
use axum::{Router, routing::get};
|
||||||
|
use sqlx::PgPool;
|
||||||
|
|
||||||
|
const MOUNT_POINT: &str = "/blog";
|
||||||
|
|
||||||
|
pub struct Blog {
|
||||||
|
pub db_handler: PgPool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Module for Blog {
|
||||||
|
fn get_server_router(&self) -> Router {
|
||||||
|
let db_handler = self.db_handler.clone();
|
||||||
|
// todo: move resource init to new func
|
||||||
|
// todo: remove debug comment
|
||||||
|
Router::new()
|
||||||
|
.route("/posts", get(get::handler))
|
||||||
|
.route("/posts/{identifier}", get(posts::get_identifier::handler))
|
||||||
|
.route("/posts/pagination", get(pagination::handler))
|
||||||
|
.with_state(db_handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_mount_point(&self) -> &'static str {
|
||||||
|
MOUNT_POINT
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_cron_tasks(&self) -> Vec<(&str, Box<dyn CronTask>)> {
|
||||||
|
let db_handler = self.db_handler.clone();
|
||||||
|
vec![
|
||||||
|
(
|
||||||
|
"Blog Check Update",
|
||||||
|
AsyncBasic::new(check_update::task(db_handler.clone()), "*/5 * * * * *")
|
||||||
|
.unwrap()
|
||||||
|
.to_task(),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"Consistency Guard",
|
||||||
|
AsyncBasic::new(
|
||||||
|
consistency_guard::task(db_handler.clone()),
|
||||||
|
"*/15 * * * * *",
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.to_task(),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
CREATE TABLE tags
|
||||||
|
(
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
name VARCHAR(20) UNIQUE NOT NULL,
|
||||||
|
description TEXT DEFAULT ''
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE categories
|
||||||
|
(
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
name VARCHAR(20) UNIQUE NOT NULL,
|
||||||
|
description TEXT DEFAULT ''
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE post_status
|
||||||
|
(
|
||||||
|
name VARCHAR(20) PRIMARY KEY
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO post_status (name)
|
||||||
|
VALUES ('draft'),
|
||||||
|
('published'),
|
||||||
|
('planned'),
|
||||||
|
('hidden');
|
||||||
|
|
||||||
|
CREATE TABLE posts
|
||||||
|
(
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
identifier VARCHAR(100) UNIQUE NOT NULL,
|
||||||
|
title VARCHAR(255) NOT NULL,
|
||||||
|
subtitle VARCHAR(255) NOT NULL,
|
||||||
|
date_created DATE NOT NULL,
|
||||||
|
date_updated DATE,
|
||||||
|
status VARCHAR(20) REFERENCES post_status (name) NOT NULL,
|
||||||
|
content TEXT NOT NULL,
|
||||||
|
hash VARCHAR(44) NOT NULL,
|
||||||
|
category INTEGER REFERENCES categories (id) NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE post_tag
|
||||||
|
(
|
||||||
|
post UUID REFERENCES posts (id) ON DELETE CASCADE,
|
||||||
|
tag INTEGER REFERENCES tags (id) ON DELETE CASCADE,
|
||||||
|
|
||||||
|
CONSTRAINT post_tag_pk PRIMARY KEY (post, tag)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE metadata
|
||||||
|
(
|
||||||
|
pid UUID REFERENCES posts (id) ON DELETE CASCADE NOT NULL,
|
||||||
|
meta_key VARCHAR(50) NOT NULL,
|
||||||
|
meta_value TEXT NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT metadata_pk PRIMARY KEY (pid, meta_key)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- index
|
||||||
|
CREATE INDEX idx_tags_name ON tags (name);
|
||||||
|
CREATE INDEX idx_categories_name ON categories (name);
|
||||||
|
CREATE INDEX idx_posts_identifier ON posts (identifier);
|
||||||
|
CREATE INDEX idx_posts_status ON posts (status);
|
||||||
|
CREATE INDEX idx_posts_category ON posts (category);
|
||||||
|
CREATE INDEX idx_posts_date_created ON posts (date_created);
|
||||||
|
CREATE INDEX idx_post_tag_post ON post_tag (post);
|
||||||
|
CREATE INDEX idx_post_tag_tag ON post_tag (tag);
|
||||||
|
CREATE INDEX idx_metadata_pid ON metadata (pid);
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
use std::{env, num::ParseIntError};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
constants::config::server::{default_value, var_name},
|
||||||
|
scheduler::{Scheduler, tasks::CronTask},
|
||||||
|
};
|
||||||
|
use macros::error_panic;
|
||||||
|
|
||||||
|
use axum::Router;
|
||||||
|
use tokio::net::TcpListener;
|
||||||
|
use tracing::{info, warn};
|
||||||
|
|
||||||
|
pub mod blog;
|
||||||
|
|
||||||
|
// todo: merge `get_server_router` & `get_mount_point`, make Option
|
||||||
|
pub trait Module {
|
||||||
|
fn get_server_router(&self) -> Router;
|
||||||
|
|
||||||
|
fn get_mount_point(&self) -> &'static str;
|
||||||
|
|
||||||
|
fn get_cron_tasks(&self) -> Vec<(&str, Box<dyn CronTask>)>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ModuleTree {
|
||||||
|
pub modules: Vec<Box<dyn Module>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ModuleTree {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
ModuleTree { modules: vec![] }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_module(&mut self, module: impl Module + 'static) -> &mut Self {
|
||||||
|
self.modules.push(Box::new(module));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn blocking_serve(&self) {
|
||||||
|
let mut scheduler = Scheduler::new();
|
||||||
|
let mut app = Router::new();
|
||||||
|
|
||||||
|
let port: u32 = match env::var(var_name::PORT) {
|
||||||
|
Ok(value) => value.parse().unwrap_or_else(|e: ParseIntError| {
|
||||||
|
warn!(
|
||||||
|
"failed to parse `{}`, using default port: {}",
|
||||||
|
var_name::PORT,
|
||||||
|
e.to_string()
|
||||||
|
);
|
||||||
|
default_value::PORT
|
||||||
|
}),
|
||||||
|
Err(_) => default_value::PORT,
|
||||||
|
};
|
||||||
|
|
||||||
|
let listener = match TcpListener::bind(format!("{}:{}", default_value::HOST, port)).await {
|
||||||
|
Ok(listener) => {
|
||||||
|
info!("server starting on {}:{}", default_value::HOST, port);
|
||||||
|
listener
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error_panic!(
|
||||||
|
"failed to start server on {}:{}: {}",
|
||||||
|
default_value::HOST,
|
||||||
|
port,
|
||||||
|
e.to_string()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
for module in &self.modules {
|
||||||
|
let _ = scheduler.insert_list(module.get_cron_tasks());
|
||||||
|
app = app.nest(module.get_mount_point(), module.get_server_router());
|
||||||
|
}
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
scheduler.start().await;
|
||||||
|
});
|
||||||
|
|
||||||
|
axum::serve(listener, app).await.unwrap_or_else(|e| {
|
||||||
|
error_panic!("failed to start axum: {}", e.to_string());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
use std::{
|
||||||
|
error::Error,
|
||||||
|
fmt::{Debug, Display, Formatter},
|
||||||
|
sync::PoisonError,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::scheduler::error::SchedulerError::MutexPoison;
|
||||||
|
|
||||||
|
// todo: refactor with thiserror
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub enum SchedulerError {
|
||||||
|
IdNotFound,
|
||||||
|
IdAlreadyRunning,
|
||||||
|
MutexPoison(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> From<PoisonError<T>> for SchedulerError {
|
||||||
|
fn from(value: PoisonError<T>) -> Self {
|
||||||
|
MutexPoison(value.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Debug for SchedulerError {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{}", self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for SchedulerError {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
match self {
|
||||||
|
SchedulerError::IdNotFound => {
|
||||||
|
"SchedulerError::IdNotFound".to_string()
|
||||||
|
}
|
||||||
|
SchedulerError::IdAlreadyRunning => {
|
||||||
|
"SchedulerError::IdAlreadyRunning".to_string()
|
||||||
|
}
|
||||||
|
MutexPoison(str) => {
|
||||||
|
format!("SchedulerError::MutexPoison: {}", str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error for SchedulerError {}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
use std::{collections::HashMap, sync::Arc};
|
||||||
|
|
||||||
|
use crate::scheduler::{error::SchedulerError, tasks::CronTask, utils::execute};
|
||||||
|
|
||||||
|
use futures::future::join_all;
|
||||||
|
use tracing::warn;
|
||||||
|
|
||||||
|
pub mod error;
|
||||||
|
pub mod task_func;
|
||||||
|
pub mod tasks;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
pub type TaskPool = HashMap<String, Arc<Box<dyn CronTask>>>;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Scheduler {
|
||||||
|
task_pool: TaskPool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Scheduler {
|
||||||
|
/// Start all tasks in the scheduler
|
||||||
|
pub async fn start(&self) {
|
||||||
|
let fut = self
|
||||||
|
.task_pool
|
||||||
|
.clone()
|
||||||
|
.into_iter()
|
||||||
|
.map(|(name, task)| execute(name, task))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
// self.runner.run_all().await
|
||||||
|
join_all(fut).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert(&mut self, task: (&str, Box<dyn CronTask>)) -> Result<&Self, SchedulerError> {
|
||||||
|
let (k, v) = task;
|
||||||
|
if self.task_pool.insert(k.into(), v.into()).is_some() {
|
||||||
|
warn!("task list duplicated key detected: {}", k);
|
||||||
|
}
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert_list(
|
||||||
|
&mut self,
|
||||||
|
tasks: Vec<(&str, Box<dyn CronTask>)>,
|
||||||
|
) -> Result<&Self, SchedulerError> {
|
||||||
|
for task in tasks {
|
||||||
|
self.insert(task)?;
|
||||||
|
}
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
use std::pin::Pin;
|
||||||
|
|
||||||
|
pub trait TaskFunc: Fn() + Send + Sync + 'static {}
|
||||||
|
|
||||||
|
impl<T> TaskFunc for T where T: Fn() + Send + Sync + Clone + 'static {}
|
||||||
|
|
||||||
|
pub trait AsyncTaskFunc:
|
||||||
|
Fn() -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync + 'static
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> AsyncTaskFunc for T where
|
||||||
|
T: Fn() -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync + Clone + 'static
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
use std::{error::Error, pin::Pin, str::FromStr};
|
||||||
|
|
||||||
|
use crate::{scheduler::task_func::AsyncTaskFunc, scheduler::tasks::CronTask};
|
||||||
|
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use cron::Schedule;
|
||||||
|
|
||||||
|
pub struct AsyncBasic {
|
||||||
|
schedule: Schedule,
|
||||||
|
func: Box<dyn AsyncTaskFunc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsyncBasic {
|
||||||
|
pub fn new<F>(func: F, cron_expr: &str) -> Result<Self, Box<dyn Error>>
|
||||||
|
where
|
||||||
|
F: AsyncTaskFunc,
|
||||||
|
{
|
||||||
|
Ok(AsyncBasic {
|
||||||
|
schedule: Schedule::from_str(cron_expr)?,
|
||||||
|
func: Box::new(func),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_task(self) -> Box<dyn CronTask> {
|
||||||
|
Box::new(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CronTask for AsyncBasic {
|
||||||
|
fn get_next_execution(&self) -> Option<DateTime<Utc>> {
|
||||||
|
self.schedule.upcoming(Utc).next()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&self) -> Pin<Box<(dyn Future<Output = ()> + Send)>> {
|
||||||
|
Box::pin((self.func)())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
use std::{error::Error, pin::Pin, str::FromStr};
|
||||||
|
|
||||||
|
use crate::scheduler::{task_func::TaskFunc, tasks::CronTask};
|
||||||
|
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use cron::Schedule;
|
||||||
|
|
||||||
|
pub struct BasicTask {
|
||||||
|
schedule: Schedule,
|
||||||
|
func: Box<dyn TaskFunc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BasicTask {
|
||||||
|
pub fn new<F>(func: F, cron_expr: &str) -> Result<Self, Box<dyn Error>>
|
||||||
|
where
|
||||||
|
F: TaskFunc,
|
||||||
|
{
|
||||||
|
Ok(BasicTask {
|
||||||
|
schedule: Schedule::from_str(cron_expr)?,
|
||||||
|
func: Box::new(func),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_task(self) -> Box<dyn CronTask> {
|
||||||
|
Box::new(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CronTask for BasicTask {
|
||||||
|
fn get_next_execution(&self) -> Option<DateTime<Utc>> {
|
||||||
|
self.schedule.upcoming(Utc).next()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
|
||||||
|
Box::pin(async { (self.func)() })
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
use std::pin::Pin;
|
||||||
|
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub mod async_basic;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub mod basic;
|
||||||
|
|
||||||
|
pub trait CronTask: Send + Sync {
|
||||||
|
fn get_next_execution(&self) -> Option<DateTime<Utc>>;
|
||||||
|
fn call(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
use std::{sync::Arc, time::Duration};
|
||||||
|
|
||||||
|
use crate::scheduler::{error::SchedulerError, tasks::CronTask};
|
||||||
|
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use tokio::time::{Sleep, sleep};
|
||||||
|
|
||||||
|
pub fn duration_from_now(future: DateTime<Utc>) -> Duration {
|
||||||
|
future
|
||||||
|
.signed_duration_since(Utc::now())
|
||||||
|
.to_std()
|
||||||
|
.unwrap_or(Duration::ZERO)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sleeper_from_now(future: DateTime<Utc>) -> Sleep {
|
||||||
|
sleep(duration_from_now(future))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused_variables)]
|
||||||
|
pub async fn execute(name: String, func: Arc<Box<dyn CronTask>>) -> Result<(), SchedulerError> {
|
||||||
|
// todo: stats
|
||||||
|
while let Some(next_execution) = func.get_next_execution() {
|
||||||
|
let sleeper = sleeper_from_now(next_execution);
|
||||||
|
tokio::pin!(sleeper);
|
||||||
|
tokio::select! {
|
||||||
|
_ = sleeper => {
|
||||||
|
func.call().await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
use axum::{
|
||||||
|
http::{StatusCode, header::CONTENT_TYPE},
|
||||||
|
response::Response,
|
||||||
|
};
|
||||||
|
use serde_json::{Value, json};
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! err_resp_log {
|
||||||
|
($code:expr, $msg:expr) => {{
|
||||||
|
use tracing::error;
|
||||||
|
use $crate::utils::axum_response::err_resp;
|
||||||
|
|
||||||
|
error!("{}", $msg);
|
||||||
|
err_resp($code, $msg, None)
|
||||||
|
}};
|
||||||
|
($code:expr, $msg:expr, $body:expr) => {{
|
||||||
|
use tracing::error;
|
||||||
|
use $crate::utils::axum_response::err_resp;
|
||||||
|
|
||||||
|
error!("{}", $msg);
|
||||||
|
err_resp($code, $msg, Some($body))
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn err_resp(code: StatusCode, err_msg: &str, body: Option<Value>) -> Response<String> {
|
||||||
|
let mut payload = json!({
|
||||||
|
"status": "error",
|
||||||
|
"msg": err_msg,
|
||||||
|
});
|
||||||
|
if let Some(body) = body {
|
||||||
|
payload["data"] = body;
|
||||||
|
}
|
||||||
|
|
||||||
|
resp_json(code, payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn succ_resp(code: StatusCode, payload: Value) -> Response<String> {
|
||||||
|
let payload = json!({
|
||||||
|
"status": "success",
|
||||||
|
"data": payload
|
||||||
|
});
|
||||||
|
resp_json(code, payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn resp_json(code: StatusCode, payload: Value) -> Response<String> {
|
||||||
|
Response::builder()
|
||||||
|
.status(code)
|
||||||
|
.header(CONTENT_TYPE, "application/json")
|
||||||
|
.body(payload.to_string())
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
@@ -0,0 +1,166 @@
|
|||||||
|
/// git operations
|
||||||
|
/// based on https://github.com/rust-lang/git2-rs/blob/master/examples/pull.rs
|
||||||
|
/// https://github.com/rust-lang/git2-rs/commit/f3b87baed1e33d6c2d94fe1fa6aa6503a071d837
|
||||||
|
///
|
||||||
|
use git2::{
|
||||||
|
AnnotatedCommit, AutotagOption, Delta, FetchOptions, Reference, Remote, Repository, build,
|
||||||
|
};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use tracing::{error, warn};
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
|
pub struct FileDelta {
|
||||||
|
pub old_path: Option<PathBuf>,
|
||||||
|
pub new_path: Option<PathBuf>,
|
||||||
|
pub status: Delta,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn do_fetch<'a>(
|
||||||
|
repo: &'a Repository,
|
||||||
|
refs: &[&str],
|
||||||
|
remote: &'a mut Remote,
|
||||||
|
) -> Result<AnnotatedCommit<'a>, git2::Error> {
|
||||||
|
let mut fetch_option = FetchOptions::new();
|
||||||
|
|
||||||
|
fetch_option.download_tags(AutotagOption::All);
|
||||||
|
remote.fetch(refs, Some(&mut fetch_option), None)?;
|
||||||
|
|
||||||
|
let fetch_head = repo.find_reference("FETCH_HEAD")?;
|
||||||
|
repo.reference_to_annotated_commit(&fetch_head)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fast_forward(
|
||||||
|
repo: &Repository,
|
||||||
|
lb: &mut Reference,
|
||||||
|
rc: &AnnotatedCommit,
|
||||||
|
) -> Result<(), git2::Error> {
|
||||||
|
let name = match lb.name() {
|
||||||
|
Some(s) => s.to_string(),
|
||||||
|
None => String::from_utf8_lossy(lb.name_bytes()).to_string(),
|
||||||
|
};
|
||||||
|
let msg = format!("fast-forward({}) -> {}", name, rc.id());
|
||||||
|
lb.set_target(rc.id(), &msg)?;
|
||||||
|
repo.set_head(&name)?;
|
||||||
|
repo.checkout_head(Some(build::CheckoutBuilder::default().force()))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn normal_merge(
|
||||||
|
repo: &Repository,
|
||||||
|
local: &AnnotatedCommit,
|
||||||
|
remote: &AnnotatedCommit,
|
||||||
|
) -> Result<(), git2::Error> {
|
||||||
|
let local_tree = repo.find_commit(local.id())?.tree()?;
|
||||||
|
let remote_tree = repo.find_commit(remote.id())?.tree()?;
|
||||||
|
let ancestor = repo
|
||||||
|
.find_commit(repo.merge_base(local.id(), remote.id())?)?
|
||||||
|
.tree()?;
|
||||||
|
let mut idx = repo.merge_trees(&ancestor, &local_tree, &remote_tree, None)?;
|
||||||
|
|
||||||
|
if idx.has_conflicts() {
|
||||||
|
warn!("merge conflicts detected");
|
||||||
|
repo.checkout_index(Some(&mut idx), None)?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let result_tree = repo.find_tree(idx.write_tree_to(repo)?)?;
|
||||||
|
|
||||||
|
let msg = format!("merge({}) -> {}", remote.id(), local.id());
|
||||||
|
let sig = repo.signature()?;
|
||||||
|
let local_commit = repo.find_commit(local.id())?;
|
||||||
|
let remote_commit = repo.find_commit(remote.id())?;
|
||||||
|
|
||||||
|
let _merge_commit = repo.commit(
|
||||||
|
Some("HEAD"),
|
||||||
|
&sig,
|
||||||
|
&sig,
|
||||||
|
&msg,
|
||||||
|
&result_tree,
|
||||||
|
&[&local_commit, &remote_commit],
|
||||||
|
)?;
|
||||||
|
repo.checkout_head(None)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn do_merge<'a>(
|
||||||
|
repo: &'a Repository,
|
||||||
|
remote_branch: &str,
|
||||||
|
fetch_commit: AnnotatedCommit<'a>,
|
||||||
|
) -> Result<(), git2::Error> {
|
||||||
|
let analysis = repo.merge_analysis(&[&fetch_commit])?;
|
||||||
|
let head_commit = repo.reference_to_annotated_commit(&repo.head()?)?;
|
||||||
|
|
||||||
|
if analysis.0.is_fast_forward() {
|
||||||
|
let ref_name = format!("refs/heads/{}", remote_branch);
|
||||||
|
match repo.find_reference(&ref_name) {
|
||||||
|
Ok(mut r) => {
|
||||||
|
fast_forward(repo, &mut r, &fetch_commit)?;
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
repo.reference(
|
||||||
|
&ref_name,
|
||||||
|
fetch_commit.id(),
|
||||||
|
true,
|
||||||
|
&format!("({}) -> {}", remote_branch, fetch_commit.id()),
|
||||||
|
)?;
|
||||||
|
repo.set_head(&ref_name)?;
|
||||||
|
repo.checkout_head(Some(
|
||||||
|
build::CheckoutBuilder::default()
|
||||||
|
.allow_conflicts(true)
|
||||||
|
.conflict_style_merge(true)
|
||||||
|
.force(),
|
||||||
|
))?;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} else if analysis.0.is_normal() {
|
||||||
|
normal_merge(repo, &head_commit, &fetch_commit)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn calculate_diff(
|
||||||
|
repo: &Repository,
|
||||||
|
local: &AnnotatedCommit,
|
||||||
|
remote: &AnnotatedCommit,
|
||||||
|
) -> Result<Vec<FileDelta>, git2::Error> {
|
||||||
|
let local_tree = repo.find_commit(local.id())?.tree()?;
|
||||||
|
let remote_tree = repo.find_commit(remote.id())?.tree()?;
|
||||||
|
let diff = repo.diff_tree_to_tree(Some(&local_tree), Some(&remote_tree), None)?;
|
||||||
|
|
||||||
|
let mut updated_files: Vec<FileDelta> = Vec::new();
|
||||||
|
|
||||||
|
if let Err(e) = diff.foreach(
|
||||||
|
&mut |d, _| {
|
||||||
|
updated_files.push(FileDelta {
|
||||||
|
old_path: d.old_file().path().map(PathBuf::from),
|
||||||
|
new_path: d.new_file().path().map(PathBuf::from),
|
||||||
|
status: d.status(),
|
||||||
|
});
|
||||||
|
true
|
||||||
|
},
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
) {
|
||||||
|
error!("error at calculating diff: {:?}", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(updated_files.into_iter().collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn sync(
|
||||||
|
remote_name: &str,
|
||||||
|
remote_branch: &str,
|
||||||
|
repo: &Repository,
|
||||||
|
) -> Result<Vec<FileDelta>, git2::Error> {
|
||||||
|
let mut remote = repo.find_remote(remote_name)?;
|
||||||
|
let fetch_commit = do_fetch(repo, &[remote_branch], &mut remote)?;
|
||||||
|
let delta = calculate_diff(
|
||||||
|
repo,
|
||||||
|
&repo.reference_to_annotated_commit(&repo.head()?)?,
|
||||||
|
&fetch_commit,
|
||||||
|
);
|
||||||
|
do_merge(repo, remote_branch, fetch_commit)?;
|
||||||
|
delta
|
||||||
|
}
|
||||||
Loaded 100 of 176 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user