2

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 of actix_web without the HttpServer 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)
rinde
  • 1,181
  • 1
  • 8
  • 20
  • Have you been able to answer some of these questions? – Thomas Ingalls Jan 07 '20 at 05:39
  • 1
    @ThomasIngalls so far I have opted for not using ‘actix_web’. I have only a couple routes so far (still early in the dev cycle), so it hasn’t been a problem so far – rinde Jan 07 '20 at 16:31
  • Hi, @ThomasIngalls I found [this library](https://docs.rs/lambda-web/0.1.5/lambda_web/fn.run_actix_on_lambda.html), it should enable you to just add a little bit of code before starting and then it should work normally on lambda – TDiblik Apr 23 '22 at 09:56

0 Answers0