Skip to content

conduit-axum/examples: Use ConduitAxumHandler to implement route handlers #5800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions conduit-axum/examples/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#![deny(clippy::all)]

use axum::routing::get;
use conduit::{Body, Handler, RequestExt, ResponseResult};
use conduit_axum::Server;
use conduit_axum::{ConduitAxumHandler, Server};
use conduit_router::RouteBuilder;
use http::{header, Response};

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

let router = axum::Router::new()
.route("/axum/", get(wrap(endpoint)))
.route("/axum/panic", get(wrap(panic)))
.route("/axum/error", get(wrap(error)));

let app = build_conduit_handler();
let addr = ([127, 0, 0, 1], 12345).into();

Server::serve(&addr, app).await;
Server::serve(&addr, router, app).await;
}

pub fn wrap<H>(handler: H) -> ConduitAxumHandler<H> {
ConduitAxumHandler::wrap(handler)
}

fn build_conduit_handler() -> impl Handler {
Expand Down
8 changes: 7 additions & 1 deletion conduit-axum/src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ pub trait ConduitFallback {

impl ConduitFallback for axum::Router {
fn conduit_fallback(self, handler: impl Handler) -> Self {
self.fallback(ConduitAxumHandler(Arc::new(handler)))
self.fallback(ConduitAxumHandler::wrap(handler))
}
}

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

impl<H> ConduitAxumHandler<H> {
pub fn wrap(handler: H) -> Self {
Self(Arc::new(handler))
}
}

impl<H> Clone for ConduitAxumHandler<H> {
fn clone(&self) -> Self {
Self(self.0.clone())
Expand Down
8 changes: 6 additions & 2 deletions conduit-axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
//!
//! #[tokio::main]
//! async fn main() {
//! let router = axum::Router::new();
//!
//! let app = build_conduit_handler();
//! let addr = ([127, 0, 0, 1], 12345).into();
//! let server = Server::serve(&addr, app);
//! let server = Server::serve(&addr, router, app);
//!
//! server.await;
//! }
Expand Down Expand Up @@ -52,7 +54,9 @@ mod tests;
mod tokio_utils;

pub use error::ServiceError;
pub use fallback::{conduit_into_axum, CauseField, ConduitFallback, ErrorField};
pub use fallback::{
conduit_into_axum, CauseField, ConduitAxumHandler, ConduitFallback, ErrorField,
};
pub use server::Server;
pub use tokio_utils::spawn_blocking;

Expand Down
8 changes: 6 additions & 2 deletions conduit-axum/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ impl Server {
/// `tokio::Runtime` it is not possible to furter configure the `hyper::Server`. If more
/// control, such as configuring a graceful shutdown is necessary, then call
/// `Service::from_blocking` instead.
pub fn serve<H: conduit::Handler>(addr: &SocketAddr, handler: H) -> impl Future {
let router = axum::Router::new().conduit_fallback(handler);
pub fn serve<H: conduit::Handler>(
addr: &SocketAddr,
router: axum::Router,
handler: H,
) -> impl Future {
let router = router.conduit_fallback(handler);
let make_service = router.into_make_service_with_connect_info::<SocketAddr>();

hyper::Server::bind(addr).serve(make_service)
Expand Down