Skip to content

Commit 4824d24

Browse files
authored
Add example route for Axum query extractors. (#804)
This helps us verify the query integration with Axum. Signed-off-by: David Calavera <david.calavera@gmail.com>
1 parent e79183b commit 4824d24

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

examples/http-axum/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ edition = "2021"
1414
axum = "0.7"
1515
lambda_http = { path = "../../lambda-http" }
1616
lambda_runtime = { path = "../../lambda-runtime" }
17+
serde = "1.0.196"
1718
serde_json = "1.0"
1819
tokio = { version = "1", features = ["macros"] }
1920
tracing = { version = "0.1", features = ["log"] }

examples/http-axum/src/main.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//! implementation to the Lambda runtime to run as a Lambda function. By using Axum instead
77
//! of a basic `tower::Service` you get web framework niceties like routing, request component
88
//! extraction, validation, etc.
9+
use axum::extract::Query;
910
use axum::http::StatusCode;
1011
use axum::{
1112
extract::Path,
@@ -14,9 +15,16 @@ use axum::{
1415
Router,
1516
};
1617
use lambda_http::{run, Error};
18+
use serde::{Deserialize, Serialize};
1719
use serde_json::{json, Value};
1820
use std::env::set_var;
1921

22+
#[derive(Deserialize, Serialize)]
23+
struct Params {
24+
first: Option<String>,
25+
second: Option<String>,
26+
}
27+
2028
async fn root() -> Json<Value> {
2129
Json(json!({ "msg": "I am GET /" }))
2230
}
@@ -33,22 +41,26 @@ async fn post_foo_name(Path(name): Path<String>) -> Json<Value> {
3341
Json(json!({ "msg": format!("I am POST /foo/:name, name={name}") }))
3442
}
3543

44+
async fn get_parameters(Query(params): Query<Params>) -> Json<Value> {
45+
Json(json!({ "request parameters": params }))
46+
}
47+
3648
/// Example on how to return status codes and data from an Axum function
3749
async fn health_check() -> (StatusCode, String) {
3850
let health = true;
3951
match health {
4052
true => (StatusCode::OK, "Healthy!".to_string()),
41-
false => (
42-
StatusCode::INTERNAL_SERVER_ERROR,
43-
"Not healthy!".to_string(),
44-
),
53+
false => (StatusCode::INTERNAL_SERVER_ERROR, "Not healthy!".to_string()),
4554
}
4655
}
4756

4857
#[tokio::main]
4958
async fn main() -> Result<(), Error> {
50-
// AWS Runtime can ignore Stage Name passed from json event
51-
// Remove if you want the first section of the url to be the stage name of the API Gateway
59+
// If you use API Gateway stages, the Rust Runtime will include the stage name
60+
// as part of the path that your application receives.
61+
// Setting the following environment variable, you can remove the stage from the path.
62+
// This variable only applies to API Gateway stages,
63+
// you can remove it if you don't use them.
5264
// i.e with: `GET /test-stage/todo/id/123` without: `GET /todo/id/123`
5365
set_var("AWS_LAMBDA_HTTP_IGNORE_STAGE_IN_PATH", "true");
5466

@@ -65,6 +77,7 @@ async fn main() -> Result<(), Error> {
6577
.route("/", get(root))
6678
.route("/foo", get(get_foo).post(post_foo))
6779
.route("/foo/:name", post(post_foo_name))
80+
.route("/parameters", get(get_parameters))
6881
.route("/health/", get(health_check));
6982

7083
run(app).await

0 commit comments

Comments
 (0)