added cors to public, modified import_posts task, updated task log sql queries
This commit is contained in:
parent
a42f827cf7
commit
2d00604c6f
@ -8,7 +8,7 @@ authors = ["Wyatt J. Miller <wyatt@wyattjmiller.com"]
|
||||
|
||||
[dependencies]
|
||||
axum = "0.7.6"
|
||||
tower-http = { version = "0.6.1", features = ["trace"] }
|
||||
tower-http = { version = "0.6.1", features = ["trace", "cors"] }
|
||||
tower_governor = "0.4.2"
|
||||
tokio = { version = "1.40.0", features = ["full"] }
|
||||
sqlx = { version = "0.8.2", features = [
|
||||
|
@ -1,4 +1,4 @@
|
||||
use axum::Router;
|
||||
use axum::{http::Method, Router};
|
||||
use config::config;
|
||||
use sqlx::{postgres::PgPoolOptions, PgPool};
|
||||
use std::fs::File;
|
||||
@ -7,7 +7,10 @@ use std::time::Duration;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::signal;
|
||||
use tower_governor::{governor::GovernorConfigBuilder, GovernorLayer};
|
||||
use tower_http::trace::{self, TraceLayer};
|
||||
use tower_http::{
|
||||
cors::{Any, CorsLayer},
|
||||
trace::{self, TraceLayer},
|
||||
};
|
||||
use tracing_subscriber::{filter, layer::SubscriberExt, prelude::*, util::SubscriberInitExt};
|
||||
|
||||
mod config;
|
||||
@ -55,6 +58,11 @@ async fn main() {
|
||||
)
|
||||
.init();
|
||||
|
||||
let cors = CorsLayer::new()
|
||||
.allow_methods(Any)
|
||||
.allow_headers(Any)
|
||||
.allow_origin(Any);
|
||||
|
||||
// if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string()) != "development" {
|
||||
//println!("we're not in development, starting up the rate limiter");
|
||||
//let governor_conf = Arc::new(
|
||||
@ -101,11 +109,13 @@ async fn main() {
|
||||
"/authors",
|
||||
routes::authors::AuthorsRoute::routes(&app_state),
|
||||
)
|
||||
.layer(CorsLayer::permissive())
|
||||
.layer(
|
||||
TraceLayer::new_for_http()
|
||||
.make_span_with(trace::DefaultMakeSpan::new().level(tracing::Level::INFO))
|
||||
.on_response(trace::DefaultOnResponse::new().level(tracing::Level::INFO)),
|
||||
);
|
||||
// .layer(cors);
|
||||
//.layer(GovernorLayer {
|
||||
// config: governor_conf,
|
||||
//});
|
||||
|
@ -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(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user