Skip to content

Commit

Permalink
feat: add wisdoms checker
Browse files Browse the repository at this point in the history
  • Loading branch information
nikola-bozin-org committed May 3, 2024
1 parent eb3dd11 commit 7f6ce12
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod middlewares;
mod models;
mod routes;
mod state;
mod wisdoms_checker;

use db::connect;
use state::AppState;
Expand All @@ -14,6 +15,8 @@ use axum::{middleware, Extension, Router};
use middlewares::response_mapper;
use tokio::net::TcpListener;

use crate::wisdoms_checker::worker_thread;

#[tokio::main]
async fn main() {
let _ = dotenv::dotenv();
Expand Down Expand Up @@ -44,6 +47,8 @@ async fn main() {
.layer(middleware::from_fn(middlewares::rate_limit))
.layer(middleware::map_response(response_mapper));

tokio::spawn(worker_thread());

axum::serve(
listener,
router.into_make_service_with_connect_info::<SocketAddr>(),
Expand Down
31 changes: 31 additions & 0 deletions src/wisdoms_checker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use base64::{self, Engine};
use serde::Deserialize;
use std::fs;
use std::time::Duration;

#[derive(Debug, Deserialize)]
struct Wisdom {
description: String,
}

pub async fn worker_thread() {
loop {
let base64_string = fs::read_to_string("encoded-wisdoms.b64")
.expect("Failed to read encoded-wisdoms.b64 file")
.replace(['\n','\r'], "");

dbg!(&base64_string);

let decoded_bytes = base64::prelude::BASE64_STANDARD.decode(base64_string)
.expect("Failed to decode base64 string");

let wisdoms: Vec<Wisdom> =
serde_json::from_slice(&decoded_bytes).expect("Failed to parse JSON");

for wisdom in wisdoms {
println!("Decoded wisdom: {:?}", wisdom);
}

tokio::time::sleep(Duration::from_secs(1)).await;
}
}

0 comments on commit 7f6ce12

Please sign in to comment.