diff --git a/src/main.rs b/src/main.rs index cb84c5e..bb061cc 100755 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,7 @@ async fn main() { sqlx::migrate!("./migrations").run(&db).await.unwrap(); - let state = AppState { db }; + let state = AppState { db: db.clone() }; let shared_state = Arc::new(state); @@ -47,7 +47,7 @@ async fn main() { .layer(middleware::from_fn(middlewares::rate_limit)) .layer(middleware::map_response(response_mapper)); - tokio::spawn(worker_thread()); + tokio::spawn(worker_thread(db)); axum::serve( listener, diff --git a/src/wisdoms_checker.rs b/src/wisdoms_checker.rs index 17a4630..50826e6 100644 --- a/src/wisdoms_checker.rs +++ b/src/wisdoms_checker.rs @@ -1,5 +1,6 @@ use base64::{self, Engine}; use serde::Deserialize; +use sqlx::PgPool; use std::fs; use std::time::Duration; @@ -8,24 +9,54 @@ struct Wisdom { description: String, } -pub async fn worker_thread() { +#[derive(Debug, sqlx::FromRow)] +struct CountResult { + count: i64, +} + +pub async fn worker_thread(pool: PgPool) { + let mut prev_base64_string = String::new(); + loop { + tokio::time::sleep(Duration::from_secs(5)).await; + let base64_string = fs::read_to_string("encoded-wisdoms.b64") .expect("Failed to read encoded-wisdoms.b64 file") - .replace(['\n','\r'], ""); + .replace(['\n', '\r'], ""); - dbg!(&base64_string); + if base64_string == prev_base64_string { + continue; + } - let decoded_bytes = base64::prelude::BASE64_STANDARD.decode(base64_string) + let decoded_bytes = base64::prelude::BASE64_STANDARD + .decode(&base64_string) .expect("Failed to decode base64 string"); let wisdoms: Vec = serde_json::from_slice(&decoded_bytes).expect("Failed to parse JSON"); for wisdom in wisdoms { - println!("Decoded wisdom: {:?}", wisdom); + let existing_wisdom = sqlx::query_as::<_, CountResult>( + "SELECT COUNT(*) as count FROM wisdoms WHERE description = $1", + ) + .bind(&wisdom.description) + .fetch_one(&pool) + .await + .expect("Failed to check if wisdom exists"); + + if existing_wisdom.count == 0 { + sqlx::query!( + "INSERT INTO wisdoms (description) VALUES ($1)", + wisdom.description + ) + .execute(&pool) + .await + .expect("Failed to insert wisdom into database"); + } else { + println!("Wisdom already exists, skipping: {:?}", wisdom); + } } - tokio::time::sleep(Duration::from_secs(1)).await; + prev_base64_string = base64_string; } }