mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 10:26:21 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
99a19d8d9e | ||
|
|
7f400470ff |
No files matched your search
@@ -1,25 +1,24 @@
|
||||
use std::env;
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
use dirs::home_dir;
|
||||
|
||||
pub fn task() {
|
||||
pub fn task(db_connection: &Pool<Postgres>) {
|
||||
// get dir
|
||||
let git_work_dir_path = &env::var("GIT_WORK_DIR").unwrap_or_else(|e| {
|
||||
if let Ok(default_log_path) = env::var("WORK_DIR") {
|
||||
if default_log_path.starts_with('~') {
|
||||
let home_path = home_dir().unwrap_or_else(|| panic!("Cannot find home directory"));
|
||||
home_path
|
||||
.join(default_log_path.strip_prefix("~/").unwrap())
|
||||
.into_os_string()
|
||||
.into_string()
|
||||
.expect(
|
||||
"[fatal] failed to parse env `WORK_DIR`, try to use absolute path instead",
|
||||
)
|
||||
} else {
|
||||
default_log_path
|
||||
}
|
||||
} else {
|
||||
panic!("[fatal] failed to parse env `GIT_WORK_DIR`: {}", e);
|
||||
}
|
||||
});
|
||||
// let git_work_dir_path = &env::var("GIT_WORK_DIR").unwrap_or_else(|e| {
|
||||
// if let Ok(default_log_path) = env::var("WORK_DIR") {
|
||||
// if default_log_path.starts_with('~') {
|
||||
// let home_path = home_dir().unwrap_or_else(|| panic!("Cannot find home directory"));
|
||||
// home_path
|
||||
// .join(default_log_path.strip_prefix("~/").unwrap())
|
||||
// .into_os_string()
|
||||
// .into_string()
|
||||
// .expect(
|
||||
// "[fatal] failed to parse env `WORK_DIR`, try to use absolute path instead",
|
||||
// )
|
||||
// } else {
|
||||
// default_log_path
|
||||
// }
|
||||
// } else {
|
||||
// panic!("[fatal] failed to parse env `GIT_WORK_DIR`: {}", e);
|
||||
// }
|
||||
// });
|
||||
println!("check update");
|
||||
}
|
||||
@@ -1 +1,14 @@
|
||||
use crate::scheduler::tasks::basic::BasicTask;
|
||||
use crate::scheduler::tasks::Task;
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
pub mod check_update;
|
||||
|
||||
pub fn get_tasks(db_connection: Pool<Postgres>) -> Vec<Box<dyn Task>> {
|
||||
let db_conn_clone = db_connection.clone();
|
||||
vec![
|
||||
BasicTask::new(move || check_update::task(&db_conn_clone), "* * * * * *")
|
||||
.unwrap()
|
||||
.to_task(),
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
use crate::blog;
|
||||
use crate::scheduler::Scheduler;
|
||||
use sqlx::{Pool, Postgres};
|
||||
|
||||
pub fn start(db_pool: Pool<Postgres>) {
|
||||
let scheduler = Scheduler::new();
|
||||
let _ = vec![blog::cron::get_tasks(db_pool.clone())]
|
||||
.into_iter()
|
||||
.map(|list| scheduler.insert_list(list))
|
||||
.collect::<Vec<_>>();
|
||||
scheduler.start().unwrap()
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
mod cron;
|
||||
mod database;
|
||||
pub mod logger;
|
||||
mod schema;
|
||||
pub mod server;
|
||||
|
||||
use macros::panic_with_log;
|
||||
@@ -22,6 +24,7 @@ pub fn load_env() {
|
||||
}
|
||||
|
||||
pub async fn start() {
|
||||
// connect database
|
||||
let db_pool = match database::init().await {
|
||||
Ok(pool) => {
|
||||
info!("database connection pool initialized");
|
||||
@@ -36,6 +39,13 @@ pub async fn start() {
|
||||
}
|
||||
};
|
||||
|
||||
// schema init
|
||||
schema::init(&db_pool).await;
|
||||
|
||||
// load cron task
|
||||
cron::start(db_pool.clone());
|
||||
|
||||
// api serve
|
||||
match server::start(db_pool).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
use sqlx::{query, Pool, Postgres};
|
||||
use tracing::info;
|
||||
|
||||
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 model, will add more cols at runtime
|
||||
const POST_META_MODEL: &str = r##"
|
||||
CREATE TABLE IF NOT EXISTS Meta
|
||||
(
|
||||
post_id UUID PRIMARY KEY
|
||||
);
|
||||
"##;
|
||||
|
||||
const POST_HASH_MODEL: &str = r##"
|
||||
CREATE TABLE IF NOT EXISTS Hash
|
||||
(
|
||||
post_id UUID PRIMARY KEY,
|
||||
hash VARCHAR(44)
|
||||
);
|
||||
"##;
|
||||
|
||||
const TAG_MODEL: &str = r##"
|
||||
CREATE TABLE IF NOT EXISTS Tag
|
||||
(
|
||||
post_id UUID NOT NULL,
|
||||
tag VARCHAR(255) NOT NULL
|
||||
);
|
||||
"##;
|
||||
|
||||
pub async fn init(db_connection: &Pool<Postgres>) {
|
||||
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();
|
||||
info!("database schema initialized");
|
||||
}
|
||||
@@ -157,6 +157,10 @@ impl Scheduler {
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub fn insert_list(&self, tasks: Vec<Box<dyn Task>>) -> Result<Vec<Uuid>, SchedulerError> {
|
||||
tasks.into_iter().map(|t| self.insert(t)).collect()
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
@@ -15,11 +15,11 @@ use crate::utils::front_matter::{parse_front_matter, split_md_front_matter};
|
||||
|
||||
#[derive(Default, Debug, Serialize)]
|
||||
pub struct MdFile {
|
||||
file_id: Uuid,
|
||||
file_name: String,
|
||||
meta: Value,
|
||||
content: String,
|
||||
hash: String,
|
||||
pub file_id: Uuid,
|
||||
pub file_name: String,
|
||||
pub meta: Value,
|
||||
pub content: String,
|
||||
pub hash: String,
|
||||
}
|
||||
|
||||
impl MdFile {
|
||||
|
||||
Reference in new issue
Block a user