diff --git a/Cargo.lock b/Cargo.lock index 15b3b6c8..6884bd59 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -46,9 +46,9 @@ dependencies = [ [[package]] name = "actix-cors" -version = "0.6.5" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0346d8c1f762b41b458ed3145eea914966bb9ad20b9be0d6d463b20d45586370" +checksum = "f9e772b3bcafe335042b5db010ab7c09013dad6eac4915c91d8d50902769f331" dependencies = [ "actix-utils", "actix-web", @@ -283,6 +283,21 @@ dependencies = [ "syn 2.0.67", ] +[[package]] +name = "actix-web-httpauth" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456348ed9dcd72a13a1f4a660449fafdecee9ac8205552e286809eb5b0b29bd3" +dependencies = [ + "actix-utils", + "actix-web", + "base64 0.22.1", + "futures-core", + "futures-util", + "log", + "pin-project-lite", +] + [[package]] name = "actix_derive" version = "0.6.1" @@ -4384,6 +4399,20 @@ dependencies = [ "url", ] +[[package]] +name = "web3_account_server" +version = "0.4.8" +dependencies = [ + "actix-web", + "actix-web-httpauth", + "env_logger", + "hex", + "log", + "serde", + "serde_json", + "structopt", +] + [[package]] name = "web3_test_proxy" version = "0.4.8" diff --git a/crates/web3_account_server/Cargo.toml b/crates/web3_account_server/Cargo.toml index 638ba9bc..0e163daf 100644 --- a/crates/web3_account_server/Cargo.toml +++ b/crates/web3_account_server/Cargo.toml @@ -14,3 +14,4 @@ hex = { workspace = true } log = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } +structopt = { workspace = true } diff --git a/crates/web3_account_server/README.md b/crates/web3_account_server/README.md index ea51c61f..4e0dc5d7 100644 --- a/crates/web3_account_server/README.md +++ b/crates/web3_account_server/README.md @@ -1 +1,2 @@ -# yagna_account_server \ No newline at end of file +# web3_account_server + diff --git a/crates/web3_account_server/src/main.rs b/crates/web3_account_server/src/main.rs index 5138b695..755c7e57 100644 --- a/crates/web3_account_server/src/main.rs +++ b/crates/web3_account_server/src/main.rs @@ -6,10 +6,11 @@ use std::env; use std::fs::OpenOptions; use std::io::{BufReader, BufWriter}; use std::sync::{Arc, Mutex}; +use structopt::StructOpt; -fn add(item: String) -> bool { +fn add(item: String, file_name: &str) -> bool { let mut results: Vec = { - if let Ok(file) = OpenOptions::new().read(true).open("data.json") { + if let Ok(file) = OpenOptions::new().read(true).open(file_name) { let reader = BufReader::new(file); serde_json::from_reader(reader).unwrap_or_else(|_| Vec::new()) } else { @@ -24,16 +25,16 @@ fn add(item: String) -> bool { .write(true) .create(true) .truncate(true) - .open("data.json") + .open(file_name) .unwrap(); let writer = BufWriter::new(file); serde_json::to_writer(writer, &results).unwrap(); true } -fn get() -> Option { +fn get(file_name: &str) -> Option { let mut results: Vec = { - let file = OpenOptions::new().read(true).open("data.json").unwrap(); + let file = OpenOptions::new().read(true).open(file_name).unwrap(); let reader = BufReader::new(file); serde_json::from_reader(reader).unwrap_or_else(|_| Vec::new()) }; @@ -44,7 +45,7 @@ fn get() -> Option { let item = results.remove(0); // remove first item - let file = OpenOptions::new().write(true).open("data.json").unwrap(); + let file = OpenOptions::new().write(true).open(file_name).unwrap(); let writer = BufWriter::new(file); serde_json::to_writer(writer, &results).unwrap(); Some(item) @@ -53,6 +54,31 @@ fn get() -> Option { #[derive(Clone)] struct AppState { lock: Arc>, + file_name: String, +} + +#[derive(Debug, StructOpt, Clone)] +pub struct CliOptions { + #[structopt( + long = "http-port", + help = "Port number of the server", + default_value = "8080" + )] + pub http_port: u16, + + #[structopt( + long = "http-addr", + help = "Bind address of the server", + default_value = "127.0.0.1" + )] + pub http_addr: String, + + #[structopt( + long = "file-name", + help = "Name of the file to store the queue", + default_value = "data.json" + )] + pub file_name: String, } async fn add_to_queue(data: web::Data, item: String) -> impl Responder { @@ -72,8 +98,9 @@ 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 file_name = &data.file_name; let results: Vec = { - let file = OpenOptions::new().read(true).open("data.json").unwrap(); + let file = OpenOptions::new().read(true).open(file_name).unwrap(); let reader = BufReader::new(file); serde_json::from_reader(reader).unwrap_or_else(|_| Vec::new()) }; @@ -118,11 +145,12 @@ async fn main() -> std::io::Result<()> { env::var("RUST_LOG").unwrap_or("info".to_string()), ); env_logger::init(); - + let args = CliOptions::from_args(); // Load the queue from file or create a new one let app_state = AppState { lock: Arc::new(Mutex::new(())), + file_name: args.file_name, }; HttpServer::new(move || { @@ -136,7 +164,7 @@ async fn main() -> std::io::Result<()> { .route("/add", web::post().to(add_to_queue)) .route("/get", web::get().to(get_from_queue)) }) - .bind("127.0.0.1:8080")? + .bind(format!("{}:{}", args.http_addr, args.http_port))? .workers(1) .run() .await