added cors to public, modified import_posts task, updated task log sql queries
This commit is contained in:
@ -1,10 +1,10 @@
|
||||
use std::fs;
|
||||
use std::path;
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde::Deserializer;
|
||||
use crate::utils::task_log;
|
||||
use serde::{Deserialize, Deserializer};
|
||||
|
||||
pub fn register<'a>(pool: &'a sqlx::Pool<sqlx::Postgres>) {
|
||||
pub fn register(pool: &sqlx::Pool<sqlx::Postgres>) {
|
||||
let p = pool.clone();
|
||||
tokio::spawn(async move {
|
||||
import_posts("/app", &p).await;
|
||||
@ -13,6 +13,7 @@ pub fn register<'a>(pool: &'a sqlx::Pool<sqlx::Postgres>) {
|
||||
|
||||
async fn import_posts(dir_path: &str, pool: &sqlx::Pool<sqlx::Postgres>) {
|
||||
println!("hello from import_posts");
|
||||
let task = task_log::start(1, pool).await.unwrap();
|
||||
let entries = fs::read_dir(dir_path).unwrap();
|
||||
|
||||
let options = MarkdownOptions {
|
||||
@ -41,7 +42,11 @@ async fn import_posts(dir_path: &str, pool: &sqlx::Pool<sqlx::Postgres>) {
|
||||
);
|
||||
let file_md_contents = process_read_file(file_path, &options);
|
||||
let content = markdown::to_html(&file_md_contents);
|
||||
let metadata = crate::utils::front_matter::YamlFrontMatter::parse::<MarkdownMetadata>(&content).unwrap();
|
||||
let metadata =
|
||||
crate::utils::front_matter::YamlFrontMatter::parse::<MarkdownMetadata>(
|
||||
&content,
|
||||
)
|
||||
.unwrap();
|
||||
let title = metadata.metadata.title;
|
||||
|
||||
sqlx::query_as::<_, InsertPosts>(
|
||||
@ -57,6 +62,10 @@ async fn import_posts(dir_path: &str, pool: &sqlx::Pool<sqlx::Postgres>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
task_log::update(task.task_id, String::from("Completed"), pool)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn process_read_file<P: AsRef<path::Path>>(path: P, md_opts: &MarkdownOptions) -> String {
|
||||
@ -92,7 +101,7 @@ struct MarkdownMetadata {
|
||||
|
||||
fn deserialize_datetime<'de, D>(deserializer: D) -> Result<chrono::DateTime<chrono::Utc>, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = String::deserialize(deserializer)?;
|
||||
chrono::DateTime::parse_from_rfc3339(&s)
|
||||
|
@ -1,11 +1,30 @@
|
||||
use crate::TaskStatus;
|
||||
use crate::TaskLog;
|
||||
|
||||
pub async fn start(task_id: i32, pool: &sqlx::Pool<sqlx::Postgres>) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let _ = sqlx::query_as!(TaskLog, "INSERT INTO logs (task_id, created_at, task_status) VALUES ($1, now(), 'pending')", task_id).fetch_one(pool).await;
|
||||
Ok(())
|
||||
pub async fn start(
|
||||
task_id: i32,
|
||||
pool: &sqlx::Pool<sqlx::Postgres>,
|
||||
) -> Result<TaskLog, sqlx::Error> {
|
||||
sqlx::query_as!(
|
||||
TaskLog,
|
||||
"INSERT INTO logs (task_id, created_at, task_status) VALUES ($1, now(), 'pending') RETURNING task_id, log_id, created_at, task_status, finished_at",
|
||||
task_id
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn update(task_id: i32, task_status: String, pool: &sqlx::Pool<sqlx::Postgres>) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let _ = sqlx::query_as!(TaskLog, "UPDATE logs SET task_status = $1 WHERE task_id = $2", task_status, task_id).fetch_one(pool).await;
|
||||
pub async fn update(
|
||||
task_id: i32,
|
||||
task_status: String,
|
||||
pool: &sqlx::Pool<sqlx::Postgres>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let _ = sqlx::query_as!(
|
||||
TaskLog,
|
||||
"UPDATE logs SET task_status = $1 WHERE task_id = $2",
|
||||
task_status,
|
||||
task_id
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await;
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user