Function lambda_web::run_hyper_on_lambda
source · [−]pub async fn run_hyper_on_lambda<S, B>(svc: S) -> Result<(), LambdaError> where
S: Service<Request<Body>, Response = Response<B>, Error = Infallible> + 'static,
B: HttpBody,
<B as HttpBody>::Error: Error + Send + Sync + 'static,
Expand description
Run hyper based web framework on AWS Lambda
axum 0.3 example:
use axum::{routing::get, Router};
use lambda_web::{is_running_on_lambda, run_hyper_on_lambda, LambdaError};
use std::net::SocketAddr;
// basic handler that responds with a static string
async fn root() -> &'static str {
"Hello, World!"
}
#[tokio::main]
async fn main() -> Result<(), LambdaError> {
// build our application with a route
let app = Router::new()
// `GET /` goes to `root`
.route("/", get(root));
if is_running_on_lambda() {
// Run app on AWS Lambda
run_hyper_on_lambda(app).await?;
} else {
// Run app on local server
let addr = SocketAddr::from(([127, 0, 0, 1], 8080));
axum::Server::bind(&addr).serve(app.into_make_service()).await?;
}
Ok(())
}
warp 0.3 example:
use warp::Filter;
use lambda_web::{is_running_on_lambda, run_warp_on_lambda, LambdaError};
#[tokio::main]
async fn main() -> Result<(),LambdaError> {
// GET /hello/warp => 200 OK with body "Hello, warp!"
let hello = warp::path!("hello" / String)
.map(|name| format!("Hello, {}!", name));
if is_running_on_lambda() {
// Run app on AWS Lambda
run_warp_on_lambda(warp::service(hello)).await?;
} else {
// Run app on local server
warp::serve(hello).run(([127, 0, 0, 1], 8080)).await;
}
Ok(())
}