sgl-model-gateway/examples/wasm/README.md
This directory contains example WASM middleware components demonstrating how to implement custom middleware for sgl-model-gateway using the WebAssembly Component Model.
API key authentication middleware that validates API keys for requests to /api and /v1 paths.
Features:
Authorization header or x-api-key header401 Unauthorized for missing or invalid keysOnRequest onlyUse case: Protect API endpoints with API key authentication.
Request tracking and status code conversion middleware.
Features:
x-request-id, x-wasm-processed, x-processed-at, x-api-route)500 errors to 503 for better client handlingOnRequest and OnResponseUse case: Request tracing and error status code conversion.
Rate limiting middleware with configurable limits.
Features:
429 Too Many Requests when limit exceededOnRequest onlyNote: This is a simplified demonstration with per-instance state. For production, use router-level rate limiting with shared state.
Use case: Protect against request flooding and abuse.
Each example includes its own README with detailed build and deployment instructions. See individual example directories for:
All examples require:
wasm32-wasip2 target: rustup target add wasm32-wasip2wasm-tools: cargo install wasm-tools--enable-wasm)cd examples/wasm
for example in wasm-guest-auth wasm-guest-logging wasm-guest-ratelimit; do
echo "Building $example..."
cd $example && ./build.sh && cd ..
done
You can deploy all three modules together:
curl -X POST http://localhost:3000/wasm \
-H "Content-Type: application/json" \
-d '{
"modules": [
{
"name": "auth-middleware",
"file_path": "/path/to/wasm_guest_auth.component.wasm",
"module_type": "Middleware",
"attach_points": [{"Middleware": "OnRequest"}]
},
{
"name": "logging-middleware",
"file_path": "/path/to/wasm_guest_logging.component.wasm",
"module_type": "Middleware",
"attach_points": [{"Middleware": "OnRequest"}, {"Middleware": "OnResponse"}]
},
{
"name": "ratelimit-middleware",
"file_path": "/path/to/wasm_guest_ratelimit.component.wasm",
"module_type": "Middleware",
"attach_points": [{"Middleware": "OnRequest"}]
}
]
}'
Modules execute in the order they are deployed. If a module returns Reject, subsequent modules won't execute.