Crate armature_rhai

Crate armature_rhai 

Source
Expand description

§Armature Rhai

Rhai scripting language integration for Armature applications.

Write HTTP handlers, middleware, and business logic in Rhai scripts while leveraging Armature’s high-performance Rust core.

§Features

  • Script Handlers: Define HTTP handlers in .rhai files
  • Hot Reload: Automatic script reloading during development
  • Type-Safe Bindings: Access HTTP requests, responses, and Armature features
  • Sandboxed Execution: Safe script execution with configurable limits
  • Performance: Compiled scripts with caching for production

§Quick Start

use armature_rhai::{RhaiEngine, ScriptRouter};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create Rhai engine with Armature bindings
    let engine = RhaiEngine::new()
        .with_max_operations(100_000)
        .with_scripts_dir("./scripts")
        .build()?;

    // Create router with script-based handlers
    let router = ScriptRouter::new(engine)
        .route("/", "handlers/index.rhai")
        .route("/users/:id", "handlers/users.rhai")
        .route("/api/*", "handlers/api.rhai");

    // Run server
    router.serve("0.0.0.0:8080").await?;
    Ok(())
}

§Script Example

// handlers/users.rhai

// Access request data
let user_id = request.param("id");
let method = request.method();

// Handle different methods
if method == "GET" {
    // Return JSON response
    response.json(#{
        id: user_id,
        name: "Alice",
        email: "alice@example.com"
    })
} else if method == "PUT" {
    let body = request.json();
    // Process update...
    response.ok()
} else {
    response.method_not_allowed()
}

§Hot Reload (Development)

Enable the hot-reload feature for automatic script reloading:

let engine = RhaiEngine::new()
    .with_hot_reload(true)
    .build()?;

Re-exports§

pub use rhai;

Structs§

CompiledScript
A compiled Rhai script.
RequestBinding
Request binding for Rhai scripts.
ResponseBinding
Response builder for Rhai scripts.
RhaiEngine
Rhai engine with Armature bindings.
RhaiEngineBuilder
Builder for RhaiEngine.
ScriptCache
Cache for compiled scripts.
ScriptContext
Context for script execution.
ScriptHandler
A script-based HTTP handler.
ScriptLoader
Script loader for file operations.
ScriptMiddleware
A script-based middleware.
ScriptRouter
A script-based router.

Enums§

RhaiError
Errors that can occur during Rhai script execution.

Functions§

register_armature_api
Register all Armature API bindings with the Rhai engine.

Type Aliases§

Result
Result type for Rhai operations.