Expand description
Lambda LightWeight HTTP Router (lambda-lw-http-router) is a lightweight routing library for AWS Lambda HTTP events.
It provides a simple and efficient way to define routes and handlers for AWS Lambda functions that process HTTP events from API Gateway, Application Load Balancer, and WebSocket APIs.
§Features
- Support for multiple AWS event types (API Gateway v2, v1, ALB, WebSocket)
- Path parameter extraction
- Type-safe route handlers
- Compile-time route registration
- Minimal runtime overhead
§Quick Start
// Define your application state
#[derive(Clone)]
struct AppState {
// your state fields here
}
// Set up the router
define_router!(event = ApiGatewayV2httpRequest, state = AppState);
// Define a route handler
#[route(path = "/hello/{name}")]
async fn handle_hello(ctx: RouteContext) -> Result<Value, Error> {
let name = ctx.params.get("name").map(|s| s.as_str()).unwrap_or("World");
Ok(json!({
"message": format!("Hello, {}!", name)
}))
}
§Examples
Basic usage with default module name:
#[derive(Clone)]
struct AppState {
// your state fields here
}
define_router!(event = ApiGatewayV2httpRequest, state = AppState);
// This creates a module with the following types:
// Router - Router<AppState, ApiGatewayV2httpRequest>
// RouterBuilder - RouterBuilder<AppState, ApiGatewayV2httpRequest>
// RouteContext - RouteContext<AppState, ApiGatewayV2httpRequest>
Custom module name for better readability or multiple routers:
#[derive(Clone)]
struct AppState {
// your state fields here
}
// Define an API Gateway router
define_router!(event = ApiGatewayV2httpRequest, module = api_router, state = AppState);
// Define an ALB router in the same application
define_router!(event = AlbTargetGroupRequest, module = alb_router, state = AppState);
// Now you can use specific types for each router:
// api_router::Router
// api_router::RouterBuilder
// api_router::RouteContext
//
// alb_router::Router
// alb_router::RouterBuilder
// alb_router::RouteContext
§Usage with Route Handlers
The module name defined here should match the module
parameter in your route handlers:
define_router!(event = ApiGatewayV2httpRequest, module = api_router, state = AppState);
#[route(path = "/hello", module = "api_router")]
async fn handle_hello(ctx: api_router::RouteContext) -> Result<Value, Error> {
Ok(json!({ "message": "Hello, World!" }))
}
Modules§
- ctor
- Procedural macro for defining global constructor/destructor functions.
Macros§
- define_
router - Defines a router module with the necessary type aliases for your Lambda application.
Structs§
- Route
Context - Context passed to route handlers containing request information and application state.
- Router
- The main router type that handles HTTP requests and dispatches them to handlers.
- Router
Builder - A builder for constructing routers with a fluent API.
Traits§
- Routable
Http Event - A trait for HTTP events that can be routed by the router.
Functions§
Attribute Macros§
- route
- Defines a route handler for Lambda HTTP events.