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]
|
[dependencies]
|
||||||
axum = "0.7.6"
|
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"
|
tower_governor = "0.4.2"
|
||||||
tokio = { version = "1.40.0", features = ["full"] }
|
tokio = { version = "1.40.0", features = ["full"] }
|
||||||
sqlx = { version = "0.8.2", features = [
|
sqlx = { version = "0.8.2", features = [
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use axum::Router;
|
use axum::{http::Method, Router};
|
||||||
use config::config;
|
use config::config;
|
||||||
use sqlx::{postgres::PgPoolOptions, PgPool};
|
use sqlx::{postgres::PgPoolOptions, PgPool};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
@ -7,7 +7,10 @@ use std::time::Duration;
|
|||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use tokio::signal;
|
use tokio::signal;
|
||||||
use tower_governor::{governor::GovernorConfigBuilder, GovernorLayer};
|
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};
|
use tracing_subscriber::{filter, layer::SubscriberExt, prelude::*, util::SubscriberInitExt};
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
@ -55,6 +58,11 @@ async fn main() {
|
|||||||
)
|
)
|
||||||
.init();
|
.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" {
|
// if std::env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string()) != "development" {
|
||||||
//println!("we're not in development, starting up the rate limiter");
|
//println!("we're not in development, starting up the rate limiter");
|
||||||
//let governor_conf = Arc::new(
|
//let governor_conf = Arc::new(
|
||||||
@ -101,14 +109,16 @@ async fn main() {
|
|||||||
"/authors",
|
"/authors",
|
||||||
routes::authors::AuthorsRoute::routes(&app_state),
|
routes::authors::AuthorsRoute::routes(&app_state),
|
||||||
)
|
)
|
||||||
|
.layer(CorsLayer::permissive())
|
||||||
.layer(
|
.layer(
|
||||||
TraceLayer::new_for_http()
|
TraceLayer::new_for_http()
|
||||||
.make_span_with(trace::DefaultMakeSpan::new().level(tracing::Level::INFO))
|
.make_span_with(trace::DefaultMakeSpan::new().level(tracing::Level::INFO))
|
||||||
.on_response(trace::DefaultOnResponse::new().level(tracing::Level::INFO)),
|
.on_response(trace::DefaultOnResponse::new().level(tracing::Level::INFO)),
|
||||||
);
|
);
|
||||||
//.layer(GovernorLayer {
|
// .layer(cors);
|
||||||
// config: governor_conf,
|
//.layer(GovernorLayer {
|
||||||
//});
|
// config: governor_conf,
|
||||||
|
//});
|
||||||
|
|
||||||
// run it with hyper
|
// run it with hyper
|
||||||
let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path;
|
use std::path;
|
||||||
|
|
||||||
use serde::Deserialize;
|
use crate::utils::task_log;
|
||||||
use serde::Deserializer;
|
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();
|
let p = pool.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
import_posts("/app", &p).await;
|
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>) {
|
async fn import_posts(dir_path: &str, pool: &sqlx::Pool<sqlx::Postgres>) {
|
||||||
println!("hello from import_posts");
|
println!("hello from import_posts");
|
||||||
|
let task = task_log::start(1, pool).await.unwrap();
|
||||||
let entries = fs::read_dir(dir_path).unwrap();
|
let entries = fs::read_dir(dir_path).unwrap();
|
||||||
|
|
||||||
let options = MarkdownOptions {
|
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 file_md_contents = process_read_file(file_path, &options);
|
||||||
let content = markdown::to_html(&file_md_contents);
|
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;
|
let title = metadata.metadata.title;
|
||||||
|
|
||||||
sqlx::query_as::<_, InsertPosts>(
|
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 {
|
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>
|
fn deserialize_datetime<'de, D>(deserializer: D) -> Result<chrono::DateTime<chrono::Utc>, D::Error>
|
||||||
where
|
where
|
||||||
D: serde::Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
{
|
{
|
||||||
let s = String::deserialize(deserializer)?;
|
let s = String::deserialize(deserializer)?;
|
||||||
chrono::DateTime::parse_from_rfc3339(&s)
|
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>> {
|
pub async fn start(
|
||||||
let _ = sqlx::query_as!(TaskLog, "INSERT INTO logs (task_id, created_at, task_status) VALUES ($1, now(), 'pending')", task_id).fetch_one(pool).await;
|
task_id: i32,
|
||||||
Ok(())
|
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>> {
|
pub async fn update(
|
||||||
let _ = sqlx::query_as!(TaskLog, "UPDATE logs SET task_status = $1 WHERE task_id = $2", task_status, task_id).fetch_one(pool).await;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user