feat: add http status
This commit is contained in:
@@ -10,6 +10,12 @@ pub use sig::*;
|
||||
pub use rpc::*;
|
||||
pub use util::*;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct CallJSBody {
|
||||
method: String,
|
||||
params: Option<String>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let get_public_key = warp::path("get_signing_public_key").map(|| {
|
||||
@@ -18,7 +24,7 @@ async fn main() {
|
||||
Some(public_key) => {
|
||||
let mut map = HashMap::new();
|
||||
map.insert("public_key", hex::encode(&public_key));
|
||||
serde_json::to_string_pretty(&map).unwrap_or_else(|e| format!("get public key error: {}", e))
|
||||
serde_json::to_string_pretty(&map).unwrap_or_else(|e| format!("get public key error: {}", e)) + "\n"
|
||||
},
|
||||
}
|
||||
});
|
||||
@@ -37,7 +43,7 @@ async fn main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
serde_json::to_string_pretty(&jss).unwrap()
|
||||
serde_json::to_string_pretty(&jss).unwrap() + "\n"
|
||||
});
|
||||
|
||||
let get_js = warp::path!("get_js" / String).map(|js_hex| {
|
||||
@@ -53,7 +59,7 @@ async fn main() {
|
||||
result.insert("sig", js_sig);
|
||||
result.insert("verify", signed_message.verify(&load_signing_key_pair().unwrap().public_key()).to_string());
|
||||
|
||||
serde_json::to_string(&result).unwrap()
|
||||
serde_json::to_string_pretty(&result).unwrap() + "\n"
|
||||
});
|
||||
|
||||
let call_js = warp::post()
|
||||
@@ -68,11 +74,10 @@ async fn main() {
|
||||
|
||||
let signed_message: SignedMessage = serde_json::from_str(&js_sig)?;
|
||||
if !signed_message.verify(&load_signing_key_pair()?.public_key()) {
|
||||
let mut result = HashMap::new();
|
||||
result.insert("status", "400".to_owned());
|
||||
result.insert("message", "Script verify failed!".to_owned());
|
||||
result.insert("js_hash", format!("{}", js_hex));
|
||||
return Ok(serde_json::to_string_pretty(&result)?);
|
||||
return Ok(HttpResult::new_400(
|
||||
"Script verify failed!".to_owned(),
|
||||
Some(format!("{}", js_hex))
|
||||
).to_string_pretty());
|
||||
}
|
||||
|
||||
let context = QuickJSContext::new()?;
|
||||
@@ -80,29 +85,28 @@ async fn main() {
|
||||
|
||||
let r = context.call_fn(&call_js_body.method, &call_js_body.params.unwrap_or_else(|| "[]".to_owned()));
|
||||
|
||||
let mut result = HashMap::new();
|
||||
result.insert("js_hash", format!("{}", js_hex));
|
||||
let mut http_status = HttpResult::new();
|
||||
http_status.js_hash(format!("{}", js_hex));
|
||||
match r {
|
||||
Err(e) => {
|
||||
result.insert("status", "500".to_owned());
|
||||
result.insert("message", "Script call failed!".to_owned());
|
||||
result.insert("result", format!("{}", e));
|
||||
http_status.status(500)
|
||||
.message("Script call failed!".to_owned())
|
||||
.result(format!("{}", e));
|
||||
},
|
||||
Ok(r) => {
|
||||
result.insert("status", "200".to_owned());
|
||||
result.insert("message", "Script call successed!".to_owned());
|
||||
result.insert("result", r.into_string().unwrap_or_else(|| "null".to_owned()));
|
||||
http_status.status(200)
|
||||
.message("Script call successed!".to_owned())
|
||||
.result(r.into_string().unwrap_or_else(|| "null".to_owned()));
|
||||
},
|
||||
}
|
||||
Ok(serde_json::to_string_pretty(&result)?)
|
||||
Ok(http_status.to_string_pretty())
|
||||
};
|
||||
match the_fn() {
|
||||
Err(e) => {
|
||||
let mut result = HashMap::new();
|
||||
result.insert("status", "500".to_owned());
|
||||
result.insert("message", "Script call failed!".to_owned());
|
||||
result.insert("result", format!("{}", e));
|
||||
serde_json::to_string_pretty(&result).unwrap_or_else(|e| format!("JSON ser error: {}", e))
|
||||
HttpResult::new_500(
|
||||
"Script call failed!".to_owned(),
|
||||
Some(format!("{}", e))
|
||||
).to_string_pretty()
|
||||
},
|
||||
Ok(r) => r,
|
||||
}
|
||||
@@ -117,10 +121,3 @@ async fn main() {
|
||||
.run(([127, 0, 0, 1], 8888))
|
||||
.await;
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct CallJSBody {
|
||||
method: String,
|
||||
params: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user