I am building a REST API using AWS Lambda and Rust. I want the Rust code to handle multiple resources from within the same library as this seems to make more sense than creating a separate binary for every resource.
What I would like to do is use actix_web's configuration and middleware options to configure my REST endpoints. Because my code is running in an AWS Lambda environment I don't need to use the HttpServer
parts of actix_web
. The code just needs to respond to the request from within the same thread using the configuration of App
from actix_web
.
I've studied actix_lambda but it actually starts a separate thread for HttpServer
which seems unnecessary resource usage.
The code that I would like to write looks similar to this:
fn main() -> Result<(), Box<dyn Error>> {
lambda!(handle); // this macro is from lambda_http crate
Ok(())
}
fn handle(request: Request, _ctx: Context) -> Result<impl IntoResponse, HandlerError> {
let app = App::new()
.service(web::resource("/resource1").to(|| HttpResponse::Ok())))
.service(web::resource("/resource2").to(|| HttpResponse::Ok())))
.service(web::resource("/resource3").to(|| HttpResponse::Ok())));
// the method below does not exist, but what I would like to do is
// figure out where the incoming request is pointing to and let the
// App configuration take it from there.
app.handle(request)
}
Questions:
- Is it possible to use the
App
part ofactix_web
without theHttpServer
in a way similar to what the code above shows? - Does it make sense not to use
HttpServer
to avoid spawning a thread? - Why has no one done this before?
- Is it better to have separate binaries for every REST resource?
- Should I not use
actix_web
and just create my own routing and middleware solution? - Is there a more rustacean way to approach this problem? (I'm new to Rust)