Skip to content

How to share axum routes between server and lambda? #685

@tmpfs

Description

@tmpfs

Hi,

I am trying to share routes between a lambda with a normal axum server so that I can run a local server for development and integration testing and deploy as a lambda (which is proxied via API Gateway). This means the router needs to work with both axum::body::Body and lambda_http::Body.

So I defined a generic function that declares the routes with this signature:

    pub fn service<B>() -> Router<(), B> where
        B: HttpBody + Send + 'static,
        <B as HttpBody>::Data: Send,
        <B as HttpBody>::Error: Send + Sync + std::error::Error + 'static
    {
    Router::new() // all the service routes
    }

Which works fine for a normal axum server using axum::body::Body (note I cannot remove the std::error::Error trait bound as it is required for axum routing).

However when I invoke the service() function from the code that calls lambda_http::run to set up the routes I get this compiler error:

   |
66 |     let v1 = server::Server::service();
   |              ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `(dyn std::error::Error + Send + Sync + 'static)`
   = help: the following other types implement trait `std::error::Error`:
             Box<(dyn sqlx_core::error::DatabaseError + 'static)>
             Box<T>
   = note: required for `Box<(dyn std::error::Error + Send + Sync + 'static)>` to implement `std::error::Error`
note: required by a bound in `sos_platform::server::Server::service`
  --> /Users/muji/git/sos/platform/src/server.rs:76:47
   |
76 |         <B as HttpBody>::Error: Send + Sync + std::error::Error + 'static
   |                                               ^^^^^^^^^^^^^^^^^ required by this bound in `Server::service`

For more information about this error, try `rustc --explain E0277`.

Which appears to be because the HttpBody implementation in lambda_events uses an associated type Error which is defined as:

pub type Error = Box<dyn std::error::Error + Send + Sync>;

I think to fix this HttpBody::Error would need to be a concrete type rather then a type alias so it implements Sized. Is that correct?

At the moment I can workaround this by duplicating my routes in the lambda but that's very far from ideal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions