use motya_sdk::{client::{Config, Filter, FilterType}, register_plugin};
struct MyFilter {
part: String
}
impl MyFilter {
pub fn new(cfg: Config) -> Self {
Self {
part: cfg.get("forbidden").cloned().unwrap_or("".into()),
}
}
}
impl Filter for MyFilter {
fn filter(&mut self) -> Result<bool, String> {
if context::get_path().contains(&self.part) {
return Ok(true);
}
Ok(false)
}
}
struct LogFilter;
impl LogFilter { fn new(_: Config) -> Self { Self } }
impl Filter for LogFilter {
fn on_response(&mut self) -> Result<(), String> {
logger::info(&format!("path: {}", context::get_path()));
Ok(())
}
}
register_plugin!(
"my_filter" => {
kind: FilterType::Filter,
factory: MyFilter::new
},
"response_logger" => {
kind: FilterType::Response,
factory: LogFilter::new
}
);Motya SDK leverages the modern WASI Preview 2 standard. To compile your plugins, you need a recent Rust toolchain.
Rust 1.82+ supports the native wasm32-wasip2 target. Add it via rustup:
rustup target add wasm32-wasip2To generate a valid WebAssembly dynamic library, you must configure your crate type. Add this to your Cargo.toml:
[lib]
crate-type = ["cdylib"]Compile your plugin:
cargo build --release --target wasm32-wasip2The resulting file will be located at target/wasm32-wasip2/release/your_plugin_name.wasm.
See the wit-bindgen repository for further information.
Once you have your .wasm file, you need to configure Motya to load it. Motya uses KDL for configuration.
-
- Define the Plugin: In the definitions block, map a name to your .wasm file path.
-
- Use the Chain: In your service connectors, reference the filter using the format "plugin_name.filter_name".
Example motya.kdl:
system {
threads-per-service 8
daemonize #false
pid-file "/tmp/motya.pidfile"
upgrade-socket "/tmp/motya-upgrade.sock"
}
definitions {
plugins {
// 1. Load the module
plugin {
name "example-plugin" // Namespace for this module
load path="/path/to/target/wasm32-wasip2/release/my_plugin.wasm"
// or even
// load url="http://example.com/my_plugin.wasm"
}
}
}
services {
MyService {
listeners {
"0.0.0.0:8080"
}
connectors {
section "/" {
// 2. Apply the filter
// Syntax: "plugin_definition_name.filter_registered_name"
use-chain "example-plugin.my_filter"
return code="200" response="OK"
}
}
}
}Start the server pointing to your configuration file:
motya -- config-kdl=/path/to/motya.kdl