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
.rhaifiles - 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§
- Compiled
Script - A compiled Rhai script.
- Request
Binding - Request binding for Rhai scripts.
- Response
Binding - Response builder for Rhai scripts.
- Rhai
Engine - Rhai engine with Armature bindings.
- Rhai
Engine Builder - Builder for RhaiEngine.
- Script
Cache - Cache for compiled scripts.
- Script
Context - Context for script execution.
- Script
Handler - A script-based HTTP handler.
- Script
Loader - Script loader for file operations.
- Script
Middleware - A script-based middleware.
- Script
Router - A script-based router.
Enums§
- Rhai
Error - 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.