-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathmain.rs
78 lines (67 loc) · 2.77 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! This is an example function that leverages the Lambda Rust runtime HTTP support
//! and the [axum](https://docs.rs/axum/latest/axum/index.html) web framework. The
//! runtime HTTP support is backed by the [tower::Service](https://docs.rs/tower-service/0.3.2/tower_service/trait.Service.html)
//! trait. Axum's applications are also backed by the `tower::Service` trait. That means
//! that it is fairly easy to build an Axum application and pass the resulting `Service`
//! implementation to the Lambda runtime to run as a Lambda function. By using Axum instead
//! of a basic `tower::Service` you get web framework niceties like routing, request component
//! extraction, validation, etc.
use axum::extract::Query;
use axum::http::StatusCode;
use axum::{
extract::Path,
response::Json,
routing::{get, post},
Router,
};
use lambda_http::{run, tracing, Error};
use serde::{Deserialize, Serialize};
use aws_lambda_json_impl::{json, Value};
use std::env::set_var;
#[derive(Deserialize, Serialize)]
struct Params {
first: Option<String>,
second: Option<String>,
}
async fn root() -> Json<Value> {
Json(json!({ "msg": "I am GET /" }))
}
async fn get_foo() -> Json<Value> {
Json(json!({ "msg": "I am GET /foo" }))
}
async fn post_foo() -> Json<Value> {
Json(json!({ "msg": "I am POST /foo" }))
}
async fn post_foo_name(Path(name): Path<String>) -> Json<Value> {
Json(json!({ "msg": format!("I am POST /foo/:name, name={name}") }))
}
async fn get_parameters(Query(params): Query<Params>) -> Json<Value> {
Json(json!({ "request parameters": params }))
}
/// Example on how to return status codes and data from an Axum function
async fn health_check() -> (StatusCode, String) {
let health = true;
match health {
true => (StatusCode::OK, "Healthy!".to_string()),
false => (StatusCode::INTERNAL_SERVER_ERROR, "Not healthy!".to_string()),
}
}
#[tokio::main]
async fn main() -> Result<(), Error> {
// If you use API Gateway stages, the Rust Runtime will include the stage name
// as part of the path that your application receives.
// Setting the following environment variable, you can remove the stage from the path.
// This variable only applies to API Gateway stages,
// you can remove it if you don't use them.
// i.e with: `GET /test-stage/todo/id/123` without: `GET /todo/id/123`
set_var("AWS_LAMBDA_HTTP_IGNORE_STAGE_IN_PATH", "true");
// required to enable CloudWatch error logging by the runtime
tracing::init_default_subscriber();
let app = Router::new()
.route("/", get(root))
.route("/foo", get(get_foo).post(post_foo))
.route("/foo/:name", post(post_foo_name))
.route("/parameters", get(get_parameters))
.route("/health/", get(health_check));
run(app).await
}