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
+ use axum:: extract:: Query ;
9
10
use axum:: http:: StatusCode ;
10
11
use axum:: {
11
12
extract:: Path ,
@@ -14,9 +15,16 @@ use axum::{
14
15
Router ,
15
16
} ;
16
17
use lambda_http:: { run, Error } ;
18
+ use serde:: { Deserialize , Serialize } ;
17
19
use serde_json:: { json, Value } ;
18
20
use std:: env:: set_var;
19
21
22
+ #[ derive( Deserialize , Serialize ) ]
23
+ struct Params {
24
+ first : Option < String > ,
25
+ second : Option < String > ,
26
+ }
27
+
20
28
async fn root ( ) -> Json < Value > {
21
29
Json ( json ! ( { "msg" : "I am GET /" } ) )
22
30
}
@@ -33,22 +41,26 @@ async fn post_foo_name(Path(name): Path<String>) -> Json<Value> {
33
41
Json ( json ! ( { "msg" : format!( "I am POST /foo/:name, name={name}" ) } ) )
34
42
}
35
43
44
+ async fn get_parameters ( Query ( params) : Query < Params > ) -> Json < Value > {
45
+ Json ( json ! ( { "request parameters" : params } ) )
46
+ }
47
+
36
48
/// Example on how to return status codes and data from an Axum function
37
49
async fn health_check ( ) -> ( StatusCode , String ) {
38
50
let health = true ;
39
51
match health {
40
52
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 ( ) ) ,
45
54
}
46
55
}
47
56
48
57
#[ tokio:: main]
49
58
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.
52
64
// i.e with: `GET /test-stage/todo/id/123` without: `GET /todo/id/123`
53
65
set_var ( "AWS_LAMBDA_HTTP_IGNORE_STAGE_IN_PATH" , "true" ) ;
54
66
@@ -65,6 +77,7 @@ async fn main() -> Result<(), Error> {
65
77
. route ( "/" , get ( root) )
66
78
. route ( "/foo" , get ( get_foo) . post ( post_foo) )
67
79
. route ( "/foo/:name" , post ( post_foo_name) )
80
+ . route ( "/parameters" , get ( get_parameters) )
68
81
. route ( "/health/" , get ( health_check) ) ;
69
82
70
83
run ( app) . await
0 commit comments