Skip to content

Commit da8a91c

Browse files
authored
Merge pull request #5800 from Turbo87/axum-route-handlers
conduit-axum/examples: Use `ConduitAxumHandler` to implement route handlers
2 parents 5438707 + 1b82db0 commit da8a91c

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

conduit-axum/examples/server.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#![deny(clippy::all)]
22

3+
use axum::routing::get;
34
use conduit::{Body, Handler, RequestExt, ResponseResult};
4-
use conduit_axum::Server;
5+
use conduit_axum::{ConduitAxumHandler, Server};
56
use conduit_router::RouteBuilder;
67
use http::{header, Response};
78

@@ -12,10 +13,19 @@ use std::thread::sleep;
1213
async fn main() {
1314
tracing_subscriber::fmt::init();
1415

16+
let router = axum::Router::new()
17+
.route("/axum/", get(wrap(endpoint)))
18+
.route("/axum/panic", get(wrap(panic)))
19+
.route("/axum/error", get(wrap(error)));
20+
1521
let app = build_conduit_handler();
1622
let addr = ([127, 0, 0, 1], 12345).into();
1723

18-
Server::serve(&addr, app).await;
24+
Server::serve(&addr, router, app).await;
25+
}
26+
27+
pub fn wrap<H>(handler: H) -> ConduitAxumHandler<H> {
28+
ConduitAxumHandler::wrap(handler)
1929
}
2030

2131
fn build_conduit_handler() -> impl Handler {

conduit-axum/src/fallback.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@ pub trait ConduitFallback {
3131

3232
impl ConduitFallback for axum::Router {
3333
fn conduit_fallback(self, handler: impl Handler) -> Self {
34-
self.fallback(ConduitAxumHandler(Arc::new(handler)))
34+
self.fallback(ConduitAxumHandler::wrap(handler))
3535
}
3636
}
3737

3838
#[derive(Debug)]
3939
pub struct ConduitAxumHandler<H>(pub Arc<H>);
4040

41+
impl<H> ConduitAxumHandler<H> {
42+
pub fn wrap(handler: H) -> Self {
43+
Self(Arc::new(handler))
44+
}
45+
}
46+
4147
impl<H> Clone for ConduitAxumHandler<H> {
4248
fn clone(&self) -> Self {
4349
Self(self.0.clone())

conduit-axum/src/lib.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
//!
2020
//! #[tokio::main]
2121
//! async fn main() {
22+
//! let router = axum::Router::new();
23+
//!
2224
//! let app = build_conduit_handler();
2325
//! let addr = ([127, 0, 0, 1], 12345).into();
24-
//! let server = Server::serve(&addr, app);
26+
//! let server = Server::serve(&addr, router, app);
2527
//!
2628
//! server.await;
2729
//! }
@@ -52,7 +54,9 @@ mod tests;
5254
mod tokio_utils;
5355

5456
pub use error::ServiceError;
55-
pub use fallback::{conduit_into_axum, CauseField, ConduitFallback, ErrorField};
57+
pub use fallback::{
58+
conduit_into_axum, CauseField, ConduitAxumHandler, ConduitFallback, ErrorField,
59+
};
5660
pub use server::Server;
5761
pub use tokio_utils::spawn_blocking;
5862

conduit-axum/src/server.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ impl Server {
1414
/// `tokio::Runtime` it is not possible to furter configure the `hyper::Server`. If more
1515
/// control, such as configuring a graceful shutdown is necessary, then call
1616
/// `Service::from_blocking` instead.
17-
pub fn serve<H: conduit::Handler>(addr: &SocketAddr, handler: H) -> impl Future {
18-
let router = axum::Router::new().conduit_fallback(handler);
17+
pub fn serve<H: conduit::Handler>(
18+
addr: &SocketAddr,
19+
router: axum::Router,
20+
handler: H,
21+
) -> impl Future {
22+
let router = router.conduit_fallback(handler);
1923
let make_service = router.into_make_service_with_connect_info::<SocketAddr>();
2024

2125
hyper::Server::bind(addr).serve(make_service)

0 commit comments

Comments
 (0)