Crates.io | easegress-sdk |
lib.rs | easegress-sdk |
version | 0.1.0 |
source | src |
created_at | 2023-02-24 07:48:28.677191 |
updated_at | 2023-02-24 07:48:28.677191 |
description | A Rust SDK for Easegress. |
homepage | |
repository | https://github.com/megaease/easegress-rust-sdk |
max_upload_size | |
id | 793397 |
size | 37,346 |
This is the Rust SDK for Easegress. It can be used to extend the ability of Easegress.
The following steps assume that Git and Rust are installed.
git clone https://github.com/LokiWager/easegress-demo
src/lib.rs
. Please check the examples
directory for more examples.use std::collections::HashMap;
use std::time::Duration;
use easegress_sdk::*;
use easegress_macros::easegress_object;
// define your own struct.
#[easegress_object]
struct AddRequestHeader;
// implement Program trait for your own struct.
#[easegress_object]
impl Program for AddRequestHeader {
fn new(_params: HashMap<String, String>) -> Self {
Self {}
}
fn run(&self) -> i32 {
let v = request::get_header("Foo".to_string());
if v.len() > 10 {
log(LogLevel::Warning, format!("The length of Foo is greater than 10"));
}
if v.len() > 0 {
request::add_header("Wasm-Added".to_string(), v.clone());
}
request::set_body("I have a new body now".as_bytes().to_vec());
0
}
}
Note
You need to implement the Program
trait on your own struct.
Additionally, the #[easegress_object]
attribute macro must be applied to both your struct definition and the trait impl block for it.
Only one struct with #[easegress_object]
attribute macro is allowed.
wasm32-unknown-unknown
target.rustup target add wasm32-unknown-unknown
cargo build --target wasm32-unknown-unknown --release
If success, it will generate easegress_demo.wasm
at the target/wasm32-unknown-unknown/release
folder.
Please refer to the documentation of WasmHost
for deploying and executing the compiled Wasm code.