Skip to content

Commit 9f8f250

Browse files
authored
Update axum documentation to include env var of stage name (#796)
This would have been very helpful as I want making a lambda function that recieved paths from api gateway!
1 parent 0384c75 commit 9f8f250

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

examples/http-axum/src/main.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
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-
9+
use std::env::set_var;
10+
use axum::http::StatusCode;
1011
use lambda_http::{
1112
run,
1213
Error,
@@ -35,8 +36,22 @@ async fn post_foo_name(Path(name): Path<String>) -> Json<Value> {
3536
Json(json!({ "msg": format!("I am POST /foo/:name, name={name}") }))
3637
}
3738

39+
/// Example on how to return status codes and data from a Axum function
40+
async fn health_check() -> (StatusCode, &str) {
41+
let healthy = false;
42+
match health {
43+
true => {(StatusCode::OK, "Healthy!")},
44+
false => {(StatusCode::INTERNAL_SERVER_ERROR, "Not healthy!")}
45+
}
46+
}
47+
3848
#[tokio::main]
3949
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
52+
// i.e with: `GET /test-stage/todo/id/123` without: `GET /todo/id/123`
53+
set_var("AWS_LAMBDA_HTTP_IGNORE_STAGE_IN_PATH", "true");
54+
4055
// required to enable CloudWatch error logging by the runtime
4156
tracing_subscriber::fmt()
4257
.with_max_level(tracing::Level::INFO)
@@ -49,7 +64,8 @@ async fn main() -> Result<(), Error> {
4964
let app = Router::new()
5065
.route("/", get(root))
5166
.route("/foo", get(get_foo).post(post_foo))
52-
.route("/foo/:name", post(post_foo_name));
67+
.route("/foo/:name", post(post_foo_name))
68+
.route("/health/", get(health_check));
5369

5470
run(app).await
5571
}

0 commit comments

Comments
 (0)