feat: add call_fn
This commit is contained in:
@@ -30,43 +30,47 @@ async fn main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let list_js = warp::path("list_js").map(|| {
|
let list_js = warp::path("list_js").map(|| {
|
||||||
let mut jss = vec![];
|
call_fn(|| -> XResult<String> {
|
||||||
if let Ok(read_dir) = std::fs::read_dir("js") {
|
let mut jss = vec![];
|
||||||
for entry in read_dir {
|
if let Ok(read_dir) = std::fs::read_dir("js") {
|
||||||
if let Ok(dir_entry) = entry {
|
for entry in read_dir {
|
||||||
if let Ok(file_name) = dir_entry.file_name().into_string() {
|
if let Ok(dir_entry) = entry {
|
||||||
if file_name.ends_with(".js") {
|
if let Ok(file_name) = dir_entry.file_name().into_string() {
|
||||||
let chars = file_name.chars();
|
if file_name.ends_with(".js") {
|
||||||
jss.push(chars.take(file_name.chars().count() - 3).collect::<String>());
|
let chars = file_name.chars();
|
||||||
|
jss.push(chars.take(file_name.chars().count() - 3).collect::<String>());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
Ok(serde_json::to_string_pretty(&jss).unwrap() + "\n")
|
||||||
serde_json::to_string_pretty(&jss).unwrap() + "\n"
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let get_js = warp::path!("get_js" / String).map(|js_hex| {
|
let get_js = warp::path!("get_js" / String).map(|js_hex| {
|
||||||
let js_fn = format!("js/{}.js", js_hex);
|
call_fn(|| -> XResult<String> {
|
||||||
let js_sig_fn = format!("js/{}.sig.json", js_hex);
|
let js_fn = format!("js/{}.js", js_hex);
|
||||||
let js = read_file(&js_fn).unwrap();
|
let js_sig_fn = format!("js/{}.sig.json", js_hex);
|
||||||
let js_sig = read_file(&js_sig_fn).unwrap();
|
let js = read_file(&js_fn)?;
|
||||||
|
let js_sig = read_file(&js_sig_fn)?;
|
||||||
|
|
||||||
let signed_message: SignedMessage = serde_json::from_str(&js_sig).unwrap();
|
let signed_message: SignedMessage = serde_json::from_str(&js_sig)?;
|
||||||
|
|
||||||
let mut result = HashMap::new();
|
let mut result = HashMap::new();
|
||||||
result.insert("js", js);
|
result.insert("js", js);
|
||||||
result.insert("sig", js_sig);
|
result.insert("sig", js_sig);
|
||||||
result.insert("verify", signed_message.verify(&load_signing_key_pair().unwrap().public_key()).to_string());
|
result.insert("verify", signed_message.verify(&load_signing_key_pair()?.public_key()).to_string());
|
||||||
|
|
||||||
serde_json::to_string_pretty(&result).unwrap() + "\n"
|
Ok(serde_json::to_string_pretty(&result)? + "\n")
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let call_js = warp::post()
|
let call_js = warp::post()
|
||||||
.and(warp::path!("call_js" / String))
|
.and(warp::path!("call_js" / String))
|
||||||
.and(warp::body::json())
|
.and(warp::body::json())
|
||||||
.map(|js_hex, call_js_body: CallJSBody| {
|
.map(|js_hex, call_js_body: CallJSBody| {
|
||||||
let the_fn = || -> XResult<String> {
|
call_fn(|| -> XResult<String> {
|
||||||
let js_fn = format!("js/{}.js", js_hex);
|
let js_fn = format!("js/{}.js", js_hex);
|
||||||
let js_sig_fn = format!("js/{}.sig.json", js_hex);
|
let js_sig_fn = format!("js/{}.sig.json", js_hex);
|
||||||
let js = read_file(&js_fn)?;
|
let js = read_file(&js_fn)?;
|
||||||
@@ -100,16 +104,7 @@ async fn main() {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
Ok(http_status.to_string_pretty())
|
Ok(http_status.to_string_pretty())
|
||||||
};
|
})
|
||||||
match the_fn() {
|
|
||||||
Err(e) => {
|
|
||||||
HttpResult::new_500(
|
|
||||||
"Script call failed!".to_owned(),
|
|
||||||
Some(format!("{}", e))
|
|
||||||
).to_string_pretty()
|
|
||||||
},
|
|
||||||
Ok(r) => r,
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
println!("Listen at 127.0.0.1:8888 ...");
|
println!("Listen at 127.0.0.1:8888 ...");
|
||||||
@@ -121,3 +116,15 @@ async fn main() {
|
|||||||
.run(([127, 0, 0, 1], 8888))
|
.run(([127, 0, 0, 1], 8888))
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn call_fn<F>(f: F) -> String where F: FnOnce() -> XResult<String> {
|
||||||
|
match f() {
|
||||||
|
Err(e) => {
|
||||||
|
HttpResult::new_500(
|
||||||
|
"Call fn failed!".to_owned(),
|
||||||
|
Some(format!("{}", e))
|
||||||
|
).to_string_pretty()
|
||||||
|
},
|
||||||
|
Ok(r) => r,
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user