diff --git a/Cargo.lock b/Cargo.lock index fef1b726..191ae166 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4412,6 +4412,7 @@ dependencies = [ "serde", "serde_json", "structopt", + "tokio", ] [[package]] diff --git a/crates/web3_account_server/Cargo.toml b/crates/web3_account_server/Cargo.toml index b4b07c4f..9b8d5926 100644 --- a/crates/web3_account_server/Cargo.toml +++ b/crates/web3_account_server/Cargo.toml @@ -17,3 +17,4 @@ log = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } structopt = { workspace = true } +tokio = { workspace = true } diff --git a/crates/web3_account_server/src/main.rs b/crates/web3_account_server/src/main.rs index a3d9ac53..2f0d37e9 100644 --- a/crates/web3_account_server/src/main.rs +++ b/crates/web3_account_server/src/main.rs @@ -5,7 +5,7 @@ use actix_web_httpauth::middleware::HttpAuthentication; use std::env; use std::fs::OpenOptions; use std::io::{BufReader, BufWriter}; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc}; use structopt::StructOpt; fn read_results(file_name: &str) -> Vec { @@ -54,7 +54,7 @@ fn get(file_name: &str) -> std::io::Result> { #[derive(Clone)] struct AppState { - lock: Arc>, + lock: Arc>, file_name: String, } @@ -83,7 +83,7 @@ pub struct CliOptions { } async fn add_to_queue(data: web::Data, item: String) -> impl Responder { - let _lock = data.lock.lock().unwrap(); + let _lock = data.lock.lock().await; let Ok(private_key) = hex::decode(item.replace("0x", "")) else { return HttpResponse::BadRequest().body("Invalid item type"); }; @@ -101,14 +101,14 @@ async fn add_to_queue(data: web::Data, item: String) -> impl Responder } async fn count(data: web::Data) -> impl Responder { - let _lock = data.lock.lock().unwrap(); + let _lock = data.lock.lock().await; let file_name = &data.file_name; let results = read_results(file_name); HttpResponse::Ok().body(results.len().to_string()) } async fn get_from_queue(data: web::Data) -> impl Responder { - let _lock = data.lock.lock().unwrap(); + let _lock = data.lock.lock().await; match get(&data.file_name) { Ok(Some(item)) => HttpResponse::Ok().body(item), Ok(None) => HttpResponse::BadRequest().body("Queue is empty"), @@ -152,7 +152,7 @@ async fn main() -> std::io::Result<()> { // Load the queue from file or create a new one let app_state = AppState { - lock: Arc::new(Mutex::new(())), + lock: Arc::new(tokio::sync::Mutex::new(())), file_name: args.file_name, };