feat: update continer

This commit is contained in:
2022-07-17 11:38:27 +08:00
parent 2d6b1e3a1b
commit 4e88c90fd4
3 changed files with 735 additions and 6 deletions

View File

@@ -22,14 +22,23 @@ pub struct MyContainer;
impl Container for MyContainer {
fn fetch(&mut self, s: &str) -> String {
// // format!("FETCHED: {}", s)
// let r = "{\"result\":\"{}\"}".into();
// println!(">>>> {}", r);
// r
println!("fetch arguments: {}", s);
let url: String = s.chars().skip(1).take(s.len() - 2).collect();
println!("fetch arguments URL: {}", url);
let r = match reqwest::blocking::get(&url) {
Err(e) => return serde_json::to_string(&FetchResult {
error: Some(serde_json::to_string(&JsResult {
message: format!("failed: {}", e)
}).expect("to json failed.4")),
result: None,
}).expect("to json failed.3"),
Ok(r) => r,
};
serde_json::to_string(&FetchResult {
error: None,
result: Some(serde_json::to_string(&JsResult {
message: format!("inputs: {}", s),
message: format!("fetched: {:?}", r.text()),
}).expect("to json failed.1")),
}).expect("to json failed.2")
}
@@ -49,7 +58,7 @@ fn main() -> Result<()> {
let a = exports.eval_javascript(&mut store, r##"
function hi(name) { return "hi: " + name; }
let a = [];
a.push(fetch('https://www.google.com/'));
a.push(fetch('https://hatter.ink/ip.action'));
for (let i = 0; i < 3; i++) { a.push(i); }
a.push({
id: 1, name: 'hatter'