Easegress Rust SDK

This is the Rust SDK for Easegress. It can be used to extend the ability of Easegress.

Prerequisites

The following steps assume that Git and Rust are installed.

Local Development

  1. Clone the repo.

sh git clone https://github.com/LokiWager/easegress-demo

  1. Implement your extension in src/lib.rs. Please check the examples directory for more examples.

```rust use std::collections::HashMap; use std::time::Duration; use easegresssdk::*; use easegressmacros::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) -> 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

  1. Add wasm32-unknown-unknown target.

bash rustup target add wasm32-unknown-unknown

  1. Build with this command

bash cargo build --target wasm32-unknown-unknown --release

If success, it will generate easegress_demo.wasm at the target/wasm32-unknown-unknown/release folder.

Deploy and execute

Please refer to the documentation of WasmHost for deploying and executing the compiled Wasm code.