Skip to content

motya-proxy/motya-sdk-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 Quick Start

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
    }
);

🛠️ Prerequisites & Setup

Motya SDK leverages the modern WASI Preview 2 standard. To compile your plugins, you need a recent Rust toolchain.

1. Install the Target

Rust 1.82+ supports the native wasm32-wasip2 target. Add it via rustup:

rustup target add wasm32-wasip2

2. Configure Cargo.toml

To generate a valid WebAssembly dynamic library, you must configure your crate type. Add this to your Cargo.toml:

[lib]
crate-type = ["cdylib"]

3. Build

Compile your plugin:

cargo build --release --target wasm32-wasip2

The resulting file will be located at target/wasm32-wasip2/release/your_plugin_name.wasm.

See the wit-bindgen repository for further information.

⚙️ Configuration & Running

Once you have your .wasm file, you need to configure Motya to load it. Motya uses KDL for configuration.

    1. Define the Plugin: In the definitions block, map a name to your .wasm file path.
    1. 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"
            }
        }
    }
}

🚀 Running Motya

Start the server pointing to your configuration file:

motya -- config-kdl=/path/to/motya.kdl

About

The Rust SDK for developing WebAssembly plugins for Motya

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages