feat: add http status
This commit is contained in:
@@ -10,6 +10,12 @@ pub use sig::*;
|
|||||||
pub use rpc::*;
|
pub use rpc::*;
|
||||||
pub use util::*;
|
pub use util::*;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct CallJSBody {
|
||||||
|
method: String,
|
||||||
|
params: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let get_public_key = warp::path("get_signing_public_key").map(|| {
|
let get_public_key = warp::path("get_signing_public_key").map(|| {
|
||||||
@@ -18,7 +24,7 @@ async fn main() {
|
|||||||
Some(public_key) => {
|
Some(public_key) => {
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
map.insert("public_key", hex::encode(&public_key));
|
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| {
|
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("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().unwrap().public_key()).to_string());
|
||||||
|
|
||||||
serde_json::to_string(&result).unwrap()
|
serde_json::to_string_pretty(&result).unwrap() + "\n"
|
||||||
});
|
});
|
||||||
|
|
||||||
let call_js = warp::post()
|
let call_js = warp::post()
|
||||||
@@ -68,11 +74,10 @@ async fn main() {
|
|||||||
|
|
||||||
let signed_message: SignedMessage = serde_json::from_str(&js_sig)?;
|
let signed_message: SignedMessage = serde_json::from_str(&js_sig)?;
|
||||||
if !signed_message.verify(&load_signing_key_pair()?.public_key()) {
|
if !signed_message.verify(&load_signing_key_pair()?.public_key()) {
|
||||||
let mut result = HashMap::new();
|
return Ok(HttpResult::new_400(
|
||||||
result.insert("status", "400".to_owned());
|
"Script verify failed!".to_owned(),
|
||||||
result.insert("message", "Script verify failed!".to_owned());
|
Some(format!("{}", js_hex))
|
||||||
result.insert("js_hash", format!("{}", js_hex));
|
).to_string_pretty());
|
||||||
return Ok(serde_json::to_string_pretty(&result)?);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let context = QuickJSContext::new()?;
|
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 r = context.call_fn(&call_js_body.method, &call_js_body.params.unwrap_or_else(|| "[]".to_owned()));
|
||||||
|
|
||||||
let mut result = HashMap::new();
|
let mut http_status = HttpResult::new();
|
||||||
result.insert("js_hash", format!("{}", js_hex));
|
http_status.js_hash(format!("{}", js_hex));
|
||||||
match r {
|
match r {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
result.insert("status", "500".to_owned());
|
http_status.status(500)
|
||||||
result.insert("message", "Script call failed!".to_owned());
|
.message("Script call failed!".to_owned())
|
||||||
result.insert("result", format!("{}", e));
|
.result(format!("{}", e));
|
||||||
},
|
},
|
||||||
Ok(r) => {
|
Ok(r) => {
|
||||||
result.insert("status", "200".to_owned());
|
http_status.status(200)
|
||||||
result.insert("message", "Script call successed!".to_owned());
|
.message("Script call successed!".to_owned())
|
||||||
result.insert("result", r.into_string().unwrap_or_else(|| "null".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() {
|
match the_fn() {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let mut result = HashMap::new();
|
HttpResult::new_500(
|
||||||
result.insert("status", "500".to_owned());
|
"Script call failed!".to_owned(),
|
||||||
result.insert("message", "Script call failed!".to_owned());
|
Some(format!("{}", e))
|
||||||
result.insert("result", format!("{}", e));
|
).to_string_pretty()
|
||||||
serde_json::to_string_pretty(&result).unwrap_or_else(|e| format!("JSON ser error: {}", e))
|
|
||||||
},
|
},
|
||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
}
|
}
|
||||||
@@ -117,10 +121,3 @@ async fn main() {
|
|||||||
.run(([127, 0, 0, 1], 8888))
|
.run(([127, 0, 0, 1], 8888))
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
|
||||||
struct CallJSBody {
|
|
||||||
method: String,
|
|
||||||
params: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ impl SigningKeyPair {
|
|||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
};
|
};
|
||||||
match File::create(file) {
|
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) => {
|
Ok(mut f) => {
|
||||||
match f.write_all(ser_bytes.as_bytes()) {
|
match f.write_all(ser_bytes.as_bytes()) {
|
||||||
Err(e) => 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))),
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
use serde::{ Serialize, Deserialize };
|
||||||
use crate::sig::SigningKeyPair;
|
use crate::sig::SigningKeyPair;
|
||||||
use rust_util::XResult;
|
use rust_util::XResult;
|
||||||
|
|
||||||
@@ -7,6 +8,66 @@ lazy_static::lazy_static! {
|
|||||||
static ref SIGNING_KEY_PAIR: Option<SigningKeyPair> = load_signing_key_pair_ok();
|
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> {
|
pub fn get_signing_public_key() -> Option<String> {
|
||||||
SIGNING_KEY_PAIR.as_ref().map(|key_pair| hex::encode(&key_pair.public_key()))
|
SIGNING_KEY_PAIR.as_ref().map(|key_pair| hex::encode(&key_pair.public_key()))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user