6
6
//! implementation to the Lambda runtime to run as a Lambda function. By using Axum instead
7
7
//! of a basic `tower::Service` you get web framework niceties like routing, request component
8
8
//! extraction, validation, etc.
9
-
9
+ use std:: env:: set_var;
10
+ use axum:: http:: StatusCode ;
10
11
use lambda_http:: {
11
12
run,
12
13
Error ,
@@ -35,8 +36,22 @@ async fn post_foo_name(Path(name): Path<String>) -> Json<Value> {
35
36
Json ( json ! ( { "msg" : format!( "I am POST /foo/:name, name={name}" ) } ) )
36
37
}
37
38
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
+
38
48
#[ tokio:: main]
39
49
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
+
40
55
// required to enable CloudWatch error logging by the runtime
41
56
tracing_subscriber:: fmt ( )
42
57
. with_max_level ( tracing:: Level :: INFO )
@@ -49,7 +64,8 @@ async fn main() -> Result<(), Error> {
49
64
let app = Router :: new ( )
50
65
. route ( "/" , get ( root) )
51
66
. 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) ) ;
53
69
54
70
run ( app) . await
55
71
}
0 commit comments