Skip to content

Commit

Permalink
feat(shuttle-axum): support axum 0.7 through feature flag (#1440)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaro00 authored Nov 29, 2023
1 parent 0c03da0 commit 2128794
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
8 changes: 7 additions & 1 deletion services/shuttle-axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ keywords = ["shuttle-service", "axum"]
[workspace]

[dependencies]
axum = { version = "0.6.10" }
axum = { version = "0.6.13", optional = true }
axum-0-7 = { package = "axum", version = "0.7.1", optional = true }
shuttle-runtime = { path = "../../runtime", version = "0.34.1", default-features = false }

[dev-dependencies]
tokio = { version = "1.26.0", features = ["macros", "rt-multi-thread"] }

[features]
default = ["axum"]

axum-0-7 = ["dep:axum-0-7"]
21 changes: 18 additions & 3 deletions services/shuttle-axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,40 @@
use shuttle_runtime::{CustomError, Error};
use std::net::SocketAddr;

#[cfg(feature = "axum")]
use axum::Router;
#[cfg(feature = "axum-0-7")]
use axum_0_7::Router;

/// A wrapper type for [axum::Router] so we can implement [shuttle_runtime::Service] for it.
pub struct AxumService(pub axum::Router);
pub struct AxumService(pub Router);

#[shuttle_runtime::async_trait]
impl shuttle_runtime::Service for AxumService {
/// Takes the router that is returned by the user in their [shuttle_runtime::main] function
/// and binds to an address passed in by shuttle.
async fn bind(mut self, addr: SocketAddr) -> Result<(), Error> {
#[cfg(feature = "axum")]
axum::Server::bind(&addr)
.serve(self.0.into_make_service())
.await
.map_err(CustomError::new)?;
#[cfg(feature = "axum-0-7")]
axum_0_7::serve(
shuttle_runtime::tokio::net::TcpListener::bind(addr)
.await
.map_err(CustomError::new)?,
self.0,
)
.await
.map_err(CustomError::new)?;

Ok(())
}
}

impl From<axum::Router> for AxumService {
fn from(router: axum::Router) -> Self {
impl From<Router> for AxumService {
fn from(router: Router) -> Self {
Self(router)
}
}
Expand Down

0 comments on commit 2128794

Please sign in to comment.