-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathmain.rs
39 lines (33 loc) · 1.23 KB
/
main.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
//! This example demonstrates how [axum middleware](https://docs.rs/axum/latest/axum/middleware/index.html)
//! can be implemented.
//!
//! To test this:
//! ```sh
//! # start the local server
//! cargo lambda watch
//! # Then send through an example request
//! cargo lambda invoke --data-example apigw-request
//! ```
use axum::{response::Json, routing::post, Router};
use lambda_http::request::RequestContext::ApiGatewayV1;
use lambda_http::{run, tracing, Error};
use aws_lambda_json_impl::{json, Value};
// Sample middleware that logs the request id
async fn mw_sample(req: axum::extract::Request, next: axum::middleware::Next) -> impl axum::response::IntoResponse {
let context = req.extensions().get::<lambda_http::request::RequestContext>();
if let Some(ApiGatewayV1(ctx)) = context {
tracing::info!("RequestId = {:?}", ctx.request_id);
}
next.run(req).await
}
async fn handler_sample(body: Json<Value>) -> Json<Value> {
Json(json!({ "echo": *body }))
}
#[tokio::main]
async fn main() -> Result<(), Error> {
tracing::init_default_subscriber();
let app = Router::new()
.route("/testStage/hello/world", post(handler_sample))
.route_layer(axum::middleware::from_fn(mw_sample));
run(app).await
}