Skip to content

Commit

Permalink
Fixes to server
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed Sep 25, 2024
1 parent 2efc549 commit 6e90d5e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/web3_account_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
name = "web3_account_server"
license = "MIT"
repository = "https://github.com/scx1332/"
version = "0.4.8"
version = "0.4.9"

[dependencies]
actix-web = { workspace = true }
Expand Down
63 changes: 33 additions & 30 deletions crates/web3_account_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,48 @@ use std::io::{BufReader, BufWriter};
use std::sync::{Arc, Mutex};
use structopt::StructOpt;

fn add(item: String, file_name: &str) -> bool {
let mut results: Vec<String> = {
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 {
Vec::new()
}
};
fn read_results(file_name: &str) -> Vec<String> {
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 {
Vec::new()
}
}

fn add(item: String, file_name: &str) -> std::io::Result<bool> {
let mut results = read_results(file_name);
if results.contains(&item) {
return false;
return Ok(false);
}
results.push(item);
let file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(file_name)
.unwrap();
.inspect_err(|e| log::error!("Error opening file: {}", e))?;
let writer = BufWriter::new(file);
serde_json::to_writer(writer, &results).unwrap();
true
Ok(true)
}

fn get(file_name: &str) -> Option<String> {
let mut results: Vec<String> = {
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())
};
fn get(file_name: &str) -> std::io::Result<Option<String>> {
let mut results = read_results(file_name);
// get first item
if results.is_empty() {
return None;
return Ok(None);
}
let item = results.remove(0);

// remove first item
let file = OpenOptions::new()
.write(true)
.truncate(true)
.open(file_name)
.unwrap();
.open(file_name)?;
let writer = BufWriter::new(file);
serde_json::to_writer(writer, &results).unwrap();
Some(item)
Ok(Some(item))
}

#[derive(Clone)]
Expand Down Expand Up @@ -93,10 +90,13 @@ async fn add_to_queue(data: web::Data<AppState>, item: String) -> impl Responder
if private_key.len() != 32 {
return HttpResponse::BadRequest().body("Invalid item length");
}
if add(hex::encode(private_key), &data.file_name) {
HttpResponse::Ok().body("Added to the queue")
} else {
HttpResponse::Ok().body("Item already in the queue")
match add(hex::encode(private_key), &data.file_name) {
Ok(true) => HttpResponse::Ok().body("Added to the queue"),
Ok(false) => HttpResponse::Ok().body("Item already in the queue"),
Err(e) => {
log::error!("Error adding item: {}", e);
HttpResponse::InternalServerError().finish()
}
}
}

Expand All @@ -113,10 +113,13 @@ async fn count(data: web::Data<AppState>) -> impl Responder {

async fn get_from_queue(data: web::Data<AppState>) -> impl Responder {
let _lock = data.lock.lock().unwrap();
if let Some(item) = get(&data.file_name) {
HttpResponse::Ok().body(item)
} else {
HttpResponse::BadRequest().body("Queue is empty")
match get(&data.file_name) {
Ok(Some(item)) => HttpResponse::Ok().body(item),
Ok(None) => HttpResponse::BadRequest().body("Queue is empty"),
Err(e) => {
log::error!("Error getting item: {}", e);
HttpResponse::InternalServerError().finish()
}
}
}

Expand Down

0 comments on commit 6e90d5e

Please sign in to comment.