Source for https://www.val.town/v/saolsen/example_rust_http_val For build instructions see https://gist.github.com/saolsen/d273bb1baba5e912e4dc2b187511affa For usage see https://www.val.town/v/saolsen/use_example_rust_http_val
Created
December 20, 2023 21:36
-
-
Save saolsen/294683088bae9a8f9a8cf93e2b392729 to your computer and use it in GitHub Desktop.
Rust Http Val
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "rust_http_val" | |
version = "0.1.0" | |
edition = "2018" | |
[lib] | |
name="rust_http_val" | |
path="val.rs" | |
crate-type = ["cdylib", "rlib"] | |
[dependencies] | |
wasm-bindgen = "0.2.84" | |
serde = { version = "1.0", features = ["derive"] } | |
serde_json = "1.0.108" | |
[profile.release] | |
lto = true | |
opt-level = "s" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use serde_json::json; | |
use wasm_bindgen::prelude::*; | |
#[wasm_bindgen] | |
extern "C" { | |
#[wasm_bindgen(js_namespace = console)] | |
fn log(s: &str); | |
} | |
#[wasm_bindgen] | |
pub fn handler(method: &str, url: &str, _body: &str) -> Result<String, JsValue> { | |
log("Handling request from Rust!"); | |
let response = json!({ | |
"message": "Hello from Rust, it works!", | |
"request": { | |
"url": url, | |
"method": method, | |
} | |
}); | |
Ok(serde_json::to_string_pretty(&response).unwrap()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment