feat: add http status

This commit is contained in:
2020-11-08 00:25:45 +08:00
parent 228e006fb5
commit 7650b3c939
3 changed files with 88 additions and 30 deletions

View File

@@ -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>,
}

View File

@@ -62,7 +62,7 @@ impl SigningKeyPair {
Ok(s) => s,
};
match File::create(file) {
Err(e) => return Err(rust_util::new_box_ioerror(&format!("File create: {}, failed: {}", file, e))),
Err(e) => Err(rust_util::new_box_ioerror(&format!("File create: {}, failed: {}", file, e))),
Ok(mut f) => {
match f.write_all(ser_bytes.as_bytes()) {
Err(e) => Err(rust_util::new_box_ioerror(&format!("File create: {}, failed: {}", file, e))),

View File

@@ -1,5 +1,6 @@
use std::fs::File;
use std::io::Read;
use serde::{ Serialize, Deserialize };
use crate::sig::SigningKeyPair;
use rust_util::XResult;
@@ -7,6 +8,66 @@ lazy_static::lazy_static! {
static ref SIGNING_KEY_PAIR: Option<SigningKeyPair> = load_signing_key_pair_ok();
}
#[derive(Serialize, Deserialize, Default)]
pub struct HttpResult {
pub status: u16,
pub message: String,
pub result: Option<String>,
pub js_hash: Option<String>,
}
impl HttpResult {
pub fn new() -> Self {
Self { ..Default::default() }
}
pub fn new_200(message: String, result: Option<String>) -> Self {
Self {
status: 200,
message,
result,
..Default::default()
}
}
pub fn new_400(message: String, result: Option<String>) -> Self {
Self {
status: 400,
message,
result,
..Default::default()
}
}
pub fn new_500(message: String, result: Option<String>) -> Self {
Self {
status: 200,
message,
result,
..Default::default()
}
}
pub fn status(&mut self, status: u16) -> &mut Self {
self.status = status;
self
}
pub fn message(&mut self, message: String) -> &mut Self {
self.message = message;
self
}
pub fn result(&mut self, result: String) -> &mut Self {
self.result = Some(result);
self
}
pub fn js_hash(&mut self, js_hash: String) -> &mut Self {
self.js_hash = Some(js_hash);
self
}
pub fn to_string(&self) -> String {
serde_json::to_string(&self).map(|mut s| { s.push('\n'); s }).unwrap_or_else(|e| format!("HttpResult to JSON failed: {}", e))
}
pub fn to_string_pretty(&self) -> String {
serde_json::to_string_pretty(&self).map(|mut s| { s.push('\n'); s }).unwrap_or_else(|e| format!("HttpResult to JSON failed: {}", e))
}
}
pub fn get_signing_public_key() -> Option<String> {
SIGNING_KEY_PAIR.as_ref().map(|key_pair| hex::encode(&key_pair.public_key()))
}