Skip to content

Commit cb2b4da

Browse files
authored
Fix issues with private key server
1 parent 2efc549 commit cb2b4da

File tree

3 files changed

+39
-37
lines changed

3 files changed

+39
-37
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/web3_account_server/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ edition = "2021"
55
name = "web3_account_server"
66
license = "MIT"
77
repository = "https://github.com/scx1332/"
8-
version = "0.4.8"
8+
version = "0.4.11"
99

1010
[dependencies]
11+
actix-cors = { workspace = true }
1112
actix-web = { workspace = true }
1213
actix-web-httpauth = { workspace = true }
1314
env_logger = { workspace = true }

crates/web3_account_server/src/main.rs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,48 @@ use std::io::{BufReader, BufWriter};
88
use std::sync::{Arc, Mutex};
99
use structopt::StructOpt;
1010

11-
fn add(item: String, file_name: &str) -> bool {
12-
let mut results: Vec<String> = {
13-
if let Ok(file) = OpenOptions::new().read(true).open(file_name) {
14-
let reader = BufReader::new(file);
15-
serde_json::from_reader(reader).unwrap_or_else(|_| Vec::new())
16-
} else {
17-
Vec::new()
18-
}
19-
};
11+
fn read_results(file_name: &str) -> Vec<String> {
12+
if let Ok(file) = OpenOptions::new().read(true).open(file_name) {
13+
let reader = BufReader::new(file);
14+
serde_json::from_reader(reader).unwrap_or_else(|_| Vec::new())
15+
} else {
16+
Vec::new()
17+
}
18+
}
19+
20+
fn add(item: String, file_name: &str) -> std::io::Result<bool> {
21+
let mut results = read_results(file_name);
2022
if results.contains(&item) {
21-
return false;
23+
return Ok(false);
2224
}
2325
results.push(item);
2426
let file = OpenOptions::new()
2527
.write(true)
2628
.create(true)
2729
.truncate(true)
2830
.open(file_name)
29-
.unwrap();
31+
.inspect_err(|e| log::error!("Error opening file: {}", e))?;
3032
let writer = BufWriter::new(file);
3133
serde_json::to_writer(writer, &results).unwrap();
32-
true
34+
Ok(true)
3335
}
3436

35-
fn get(file_name: &str) -> Option<String> {
36-
let mut results: Vec<String> = {
37-
let file = OpenOptions::new().read(true).open(file_name).unwrap();
38-
let reader = BufReader::new(file);
39-
serde_json::from_reader(reader).unwrap_or_else(|_| Vec::new())
40-
};
37+
fn get(file_name: &str) -> std::io::Result<Option<String>> {
38+
let mut results = read_results(file_name);
4139
// get first item
4240
if results.is_empty() {
43-
return None;
41+
return Ok(None);
4442
}
4543
let item = results.remove(0);
4644

4745
// remove first item
4846
let file = OpenOptions::new()
4947
.write(true)
5048
.truncate(true)
51-
.open(file_name)
52-
.unwrap();
49+
.open(file_name)?;
5350
let writer = BufWriter::new(file);
5451
serde_json::to_writer(writer, &results).unwrap();
55-
Some(item)
52+
Ok(Some(item))
5653
}
5754

5855
#[derive(Clone)]
@@ -93,30 +90,32 @@ async fn add_to_queue(data: web::Data<AppState>, item: String) -> impl Responder
9390
if private_key.len() != 32 {
9491
return HttpResponse::BadRequest().body("Invalid item length");
9592
}
96-
if add(hex::encode(private_key), &data.file_name) {
97-
HttpResponse::Ok().body("Added to the queue")
98-
} else {
99-
HttpResponse::Ok().body("Item already in the queue")
93+
match add(hex::encode(private_key), &data.file_name) {
94+
Ok(true) => HttpResponse::Ok().body("Added to the queue"),
95+
Ok(false) => HttpResponse::Ok().body("Item already in the queue"),
96+
Err(e) => {
97+
log::error!("Error adding item: {}", e);
98+
HttpResponse::InternalServerError().finish()
99+
}
100100
}
101101
}
102102

103103
async fn count(data: web::Data<AppState>) -> impl Responder {
104104
let _lock = data.lock.lock().unwrap();
105105
let file_name = &data.file_name;
106-
let results: Vec<String> = {
107-
let file = OpenOptions::new().read(true).open(file_name).unwrap();
108-
let reader = BufReader::new(file);
109-
serde_json::from_reader(reader).unwrap_or_else(|_| Vec::new())
110-
};
106+
let results = read_results(file_name);
111107
HttpResponse::Ok().body(results.len().to_string())
112108
}
113109

114110
async fn get_from_queue(data: web::Data<AppState>) -> impl Responder {
115111
let _lock = data.lock.lock().unwrap();
116-
if let Some(item) = get(&data.file_name) {
117-
HttpResponse::Ok().body(item)
118-
} else {
119-
HttpResponse::BadRequest().body("Queue is empty")
112+
match get(&data.file_name) {
113+
Ok(Some(item)) => HttpResponse::Ok().body(item),
114+
Ok(None) => HttpResponse::BadRequest().body("Queue is empty"),
115+
Err(e) => {
116+
log::error!("Error getting item: {}", e);
117+
HttpResponse::InternalServerError().finish()
118+
}
120119
}
121120
}
122121

@@ -164,6 +163,7 @@ async fn main() -> std::io::Result<()> {
164163
.app_data(web::Data::new(app_state.clone()))
165164
.wrap(actix_web::middleware::Logger::default())
166165
.wrap(auth)
166+
.wrap(actix_cors::Cors::permissive())
167167
.route("/count", web::get().to(count))
168168
.route("/add", web::post().to(add_to_queue))
169169
.route("/get", web::get().to(get_from_queue))

0 commit comments

Comments
 (0)