lambda_lw_http_router/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//! 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
//!
//! ```rust
//! # use lambda_lw_http_router::{define_router, route};
//! # use aws_lambda_events::apigw::ApiGatewayV2httpRequest;
//! # use serde_json::{json, Value};
//! # use lambda_runtime::Error;
//! #
//! // 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)
//!     }))
//! }
//!
//! # fn main() {}
//! ```
//!
//! # Examples
//!
//! Basic usage with default module name:
//!
//! ```rust
//! # use lambda_lw_http_router::define_router;
//! # use aws_lambda_events::apigw::ApiGatewayV2httpRequest;
//! # use serde_json::{json, Value};
//! # use lambda_runtime::Error;
//! #
//! #[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>
//! # fn main() {}
//! ```
//!
//! Custom module name for better readability or multiple routers:
//!
//! ```rust
//! # use lambda_lw_http_router::define_router;
//! # use aws_lambda_events::apigw::ApiGatewayV2httpRequest;
//! # use aws_lambda_events::alb::AlbTargetGroupRequest;
//! # use serde_json::{json, Value};
//! # use lambda_runtime::Error;
//! #
//! #[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
//! # fn main() {}
//! ```
//!
//! # Usage with Route Handlers
//!
//! The module name defined here should match the `module` parameter in your route handlers:
//!
//! ```rust
//! # use lambda_lw_http_router::{define_router, route};
//! # use aws_lambda_events::apigw::ApiGatewayV2httpRequest;
//! # use serde_json::{json, Value};
//! # use lambda_runtime::Error;
//! # #[derive(Clone)]
//! # struct AppState { }
//! #
//! 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!" }))
//! }
//! # fn main() {}
//! ```

pub use lambda_lw_http_router_core::*;
pub use lambda_lw_http_router_macro::route;

/// Defines a router module with the necessary type aliases for your Lambda application.
///
/// This macro creates a module containing type aliases for the router components,
/// making them easily accessible throughout your application. It's typically used
/// at the beginning of your Lambda function to set up the routing infrastructure.
///
/// # Type Aliases
///
/// The macro creates the following type aliases:
/// * `Event` - The Lambda HTTP event type (e.g., ApiGatewayV2httpRequest)
/// * `Router` - The router instance type for your application
/// * `RouterBuilder` - The builder type for constructing routers
/// * `RouteContext` - The context type passed to route handlers
///
/// # Arguments
///
/// * `event` - The Lambda HTTP event type (required). Supported types:
///   * `ApiGatewayV2httpRequest` - API Gateway HTTP API v2
///   * `ApiGatewayProxyRequest` - API Gateway REST API v1
///   * `AlbTargetGroupRequest` - Application Load Balancer
///   * `ApiGatewayWebsocketProxyRequest` - API Gateway WebSocket
/// * `module` - The module name (optional, defaults to an internal name)
/// * `state` - The state type for the router
///
/// # Examples
///
/// Basic usage with default module name:
/// ```rust,no_run
/// use lambda_lw_http_router::define_router;
/// use aws_lambda_events::apigw::ApiGatewayV2httpRequest;
/// use serde_json::{json, Value};
/// use lambda_runtime::Error;
///
/// // Define your application state
/// #[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>
/// # fn main() {}
/// ```
///
/// Custom module name for better readability or multiple routers:
/// ```rust,no_run
/// use lambda_lw_http_router::define_router;
/// use aws_lambda_events::apigw::ApiGatewayV2httpRequest;
/// use aws_lambda_events::alb::AlbTargetGroupRequest;
/// use serde_json::{json, Value};
/// use lambda_runtime::Error;
///
/// // Define your application state
/// #[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
/// # fn main() {}
/// ```
///
/// # Usage with Route Handlers
///
/// The module name defined here should match the `module` parameter in your route handlers:
///
/// ```rust, no_run
/// use lambda_lw_http_router::{define_router, route};
/// use aws_lambda_events::apigw::ApiGatewayV2httpRequest;
/// use serde_json::{json, Value};
/// use lambda_runtime::Error;
///
/// // Define your application state
/// #[derive(Clone)]
/// struct AppState {
///     // your state fields here
/// }
///
/// 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> {
///     let name = ctx.params.get("name").map(|s| s.as_str()).unwrap_or("World");
///     Ok(json!({ "message": format!("Hello, {}!", name) }))
/// }
/// # fn main() {}
/// ```

#[macro_export]
macro_rules! define_router {
    (event = $event_type:ty, module = $module:ident, state = $state_type:ty) => {
        pub mod $module {
            use super::*;

            pub type Event = $event_type;
            pub type State = $state_type;
            pub type Router = ::lambda_lw_http_router::Router<State, Event>;
            pub type RouterBuilder = ::lambda_lw_http_router::RouterBuilder<State, Event>;
            pub type RouteContext = ::lambda_lw_http_router::RouteContext<State, Event>;
        }
        pub use $module::*;
    };

    (event = $event_type:ty, state = $state_type:ty) => {
        mod __lambda_lw_http_router_core_default_router {
            use super::*;

            pub type Event = $event_type;
            pub type State = $state_type;
            pub type Router = ::lambda_lw_http_router::Router<State, Event>;
            pub type RouterBuilder = ::lambda_lw_http_router::RouterBuilder<State, Event>;
            pub type RouteContext = ::lambda_lw_http_router::RouteContext<State, Event>;
        }
        pub use __lambda_lw_http_router_core_default_router::*;
    };
}

#[cfg(doctest)]
extern crate doc_comment;

#[cfg(doctest)]
use doc_comment::doctest;

#[cfg(doctest)]
doctest!("../README.md", readme);