pub struct RouteContext<State, E>where
State: Clone,{
pub path: String,
pub method: String,
pub params: HashMap<String, String>,
pub state: Arc<State>,
pub event: E,
pub lambda_context: Context,
pub route_pattern: String,
}
Expand description
Context passed to route handlers containing request information and application state.
This struct provides access to request details, path parameters, application state, and Lambda execution context. It is passed to every route handler and provides methods for accessing OpenTelemetry span attributes.
§Examples
use aws_lambda_events::apigw::ApiGatewayV2httpRequest;
use lambda_lw_http_router_core::{RouteContext, Router};
use serde_json::Value;
use lambda_runtime::Error;
use serde_json::json;
async fn handle_user(ctx: RouteContext<(), ApiGatewayV2httpRequest>) -> Result<Value, Error> {
// Get path parameters
let user_id = ctx.get_param("id").unwrap_or_else(||"default".to_string());
// Access request details
let method = ctx.method();
let path = ctx.path();
// Set span attributes
ctx.set_otel_attribute("user.id", user_id.clone());
Ok(json!({ "id": user_id }))
}
Fields§
§path: String
The full request path
method: String
The HTTP method (GET, POST, etc.)
params: HashMap<String, String>
Path parameters extracted from the URL (e.g., {id} -> “123”)
state: Arc<State>
Application state shared across all requests
event: E
The original Lambda event
lambda_context: Context
Lambda execution context
route_pattern: String
The route template pattern (e.g., “/quote/{id}”)
Implementations§
Source§impl<State, E> RouteContext<State, E>where
State: Clone,
impl<State, E> RouteContext<State, E>where
State: Clone,
Sourcepub fn path(&self) -> &str
pub fn path(&self) -> &str
Returns the full request path of the current request.
§Examples
async fn handler(ctx: RouteContext<AppState, ApiGatewayV2httpRequest>) {
let path = ctx.path();
assert_eq!(path, "/users/123/profile");
}
Sourcepub fn method(&self) -> &str
pub fn method(&self) -> &str
Returns the HTTP method of the current request (e.g., “GET”, “POST”).
§Examples
async fn handler(ctx: RouteContext<AppState, ApiGatewayV2httpRequest>) {
let method = ctx.method();
assert_eq!(method, "POST");
}
Sourcepub fn state(&self) -> &State
pub fn state(&self) -> &State
Returns a reference to the shared application state.
The state is shared across all request handlers and can be used to store application-wide data like database connections or configuration.
§Examples
#[derive(Clone)]
struct AppState {
api_key: String,
}
async fn handler(ctx: RouteContext<AppState, ApiGatewayV2httpRequest>) {
let api_key = &ctx.state().api_key;
// Use the API key for authentication
}
Sourcepub fn event(&self) -> &E
pub fn event(&self) -> &E
Returns a reference to the original Lambda event.
This provides access to the raw event data from AWS Lambda, which can be useful for accessing event-specific fields not exposed through the router interface.
§Examples
async fn handler(ctx: RouteContext<AppState, ApiGatewayV2httpRequest>) {
let raw_event = ctx.event();
if let Some(body) = &raw_event.body {
// Process the raw request body
}
}
Sourcepub fn lambda_context(&self) -> &Context
pub fn lambda_context(&self) -> &Context
Returns a reference to the Lambda execution context.
The Lambda context contains metadata about the current execution environment, such as the request ID, function name, and remaining execution time.
§Examples
async fn handler(ctx: RouteContext<AppState, ApiGatewayV2httpRequest>) {
let request_id = &ctx.lambda_context().request_id;
let deadline = ctx.lambda_context().deadline;
}
Sourcepub fn route_pattern(&self) -> &str
pub fn route_pattern(&self) -> &str
Returns the route pattern that matched this request.
The route pattern is the original path template with parameter placeholders, such as “/users/{id}/profile”.
§Examples
async fn handler(ctx: RouteContext<AppState, ApiGatewayV2httpRequest>) {
let pattern = ctx.route_pattern();
assert_eq!(pattern, "/users/{id}/profile");
}
Sourcepub fn get_param(&self, name: &str) -> Option<String>
pub fn get_param(&self, name: &str) -> Option<String>
Returns a path parameter by name, if it exists.
Path parameters are extracted from the URL based on the route pattern.
For example, if the route pattern is “/users/{id}” and the URL is “/users/123”,
then get_param("id")
will return Some("123")
.
§Arguments
name
- The name of the path parameter to retrieve
§Examples
async fn get_user(ctx: RouteContext<AppState, ApiGatewayV2httpRequest>) -> serde_json::Value {
let user_id = ctx.get_param("id").unwrap_or_default();
json!({ "id": user_id })
}
Sourcepub fn get_param_or(&self, name: &str, default: &str) -> String
pub fn get_param_or(&self, name: &str, default: &str) -> String
Returns a path parameter by name, or a default value if it doesn’t exist.
§Examples
async fn get_user(ctx: RouteContext<AppState, ApiGatewayV2httpRequest>) -> serde_json::Value {
let user_id = ctx.get_param_or("id", "default");
json!({ "id": user_id })
}
Sourcepub fn get_param_or_empty(&self, name: &str) -> String
pub fn get_param_or_empty(&self, name: &str) -> String
Returns a path parameter by name, or an empty string if it doesn’t exist.
§Examples
async fn get_user(ctx: RouteContext<AppState, ApiGatewayV2httpRequest>) -> serde_json::Value {
let user_id = ctx.get_param_or_empty("id");
json!({ "id": user_id })
}
Sourcepub fn params(&self) -> &HashMap<String, String>
pub fn params(&self) -> &HashMap<String, String>
Returns a reference to all path parameters.
This method returns a HashMap containing all path parameters extracted from the URL based on the route pattern.
§Examples
async fn handler(ctx: RouteContext<AppState, ApiGatewayV2httpRequest>) -> serde_json::Value {
let params = ctx.params();
json!({
"user_id": params.get("user_id"),
"post_id": params.get("post_id")
})
}
Sourcepub fn set_otel_attribute(
&self,
key: impl Into<Key>,
value: impl Into<Value>,
) -> &RouteContext<State, E>
pub fn set_otel_attribute( &self, key: impl Into<Key>, value: impl Into<Value>, ) -> &RouteContext<State, E>
Sets a single attribute on the current OpenTelemetry span.
This method allows you to add custom attributes to the current span for better observability and tracing.
§Arguments
key
- The attribute keyvalue
- The attribute value (supports strings, numbers, and booleans)
§Returns
Returns a reference to self for method chaining
§Examples
async fn handler(ctx: RouteContext<AppState, ApiGatewayV2httpRequest>) {
ctx.set_otel_attribute("user.id", "123")
.set_otel_attribute("request.size", 1024)
.set_otel_attribute("cache.hit", true);
}
Sourcepub fn set_otel_span_kind(&self, kind: &str) -> &RouteContext<State, E>
pub fn set_otel_span_kind(&self, kind: &str) -> &RouteContext<State, E>
Sets the OpenTelemetry span kind for the current span.
The span kind describes the relationship between the span and its parent. Common values include:
- “SERVER” for server-side request handling
- “CLIENT” for outbound requests
- “PRODUCER” for message publishing
- “CONSUMER” for message processing
- “INTERNAL” for internal operations
§Arguments
kind
- The span kind to set
§Returns
Returns a reference to self for method chaining
§Examples
async fn handler(ctx: RouteContext<AppState, ApiGatewayV2httpRequest>) {
ctx.set_otel_span_kind("SERVER")
.set_otel_attribute("request.id", "abc-123");
}
Trait Implementations§
Source§impl<State, E> Clone for RouteContext<State, E>
impl<State, E> Clone for RouteContext<State, E>
Source§fn clone(&self) -> RouteContext<State, E>
fn clone(&self) -> RouteContext<State, E>
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read more