Skip to content

feat: convenience methods for axum ws #28

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 2 commits into from
Mar 30, 2025
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "Simple, modern, ergonomic JSON-RPC 2.0 router built with tower an
keywords = ["json-rpc", "jsonrpc", "json"]
categories = ["web-programming::http-server", "web-programming::websocket"]

version = "0.3.2"
version = "0.3.3"
edition = "2021"
rust-version = "1.81"
authors = ["init4", "James Prestwich"]
Expand Down
44 changes: 43 additions & 1 deletion src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ where
/// use. This runtime is accessible to all handlers invoked by the router.
///
/// Tasks spawned by the router will be spawned on the provided runtime,
/// and automatically cancelled when the returned `axum::Router` is dropped.
/// and automatically cancelled when the returned [`axum::Router`] is
/// dropped.
#[cfg(feature = "axum")]
pub fn into_axum_with_handle(
self,
Expand All @@ -375,6 +376,47 @@ impl Router<()> {
connect.serve(self).await
}

/// Create an [`AxumWsCfg`] from this router. This is a convenience method
/// for `AxumWsCfg::new(self.clone())`.
///
/// [`AxumWsCfg`]: crate::pubsub::AxumWsCfg
#[cfg(all(feature = "axum", feature = "pubsub"))]
pub fn to_axum_cfg(&self) -> crate::pubsub::AxumWsCfg {
crate::pubsub::AxumWsCfg::new(self.clone())
}

/// Create an [`axum::Router`] from this router, serving this router via
/// HTTP `POST` requests at `post_route` and via WebSocket at `ws_route`.
#[cfg(all(feature = "axum", feature = "pubsub"))]
pub fn into_axum_with_ws(self, post_route: &str, ws_route: &str) -> axum::Router<()> {
let cfg = self.to_axum_cfg();

self.into_axum(post_route)
.with_state(())
.route(ws_route, axum::routing::any(crate::pubsub::ajj_websocket))
.with_state(cfg)
}

/// Create an [`axum::Router`] from this router, serving this router via
/// HTTP `POST` requests at `post_route` and via WebSocket at `ws_route`.
/// This convenience method allows users to specify a runtime handle for the
/// router to use. See [`Router::into_axum_with_handle`] for more
/// information.
#[cfg(all(feature = "axum", feature = "pubsub"))]
pub fn into_axum_with_ws_and_handle(
self,
post_route: &str,
ws_route: &str,
handle: tokio::runtime::Handle,
) -> axum::Router<()> {
let cfg = self.to_axum_cfg();

self.into_axum_with_handle(post_route, handle)
.with_state(())
.route(ws_route, axum::routing::any(crate::pubsub::ajj_websocket))
.with_state(cfg)
}

/// Call a method on the router.
pub fn handle_request(&self, args: HandlerArgs) -> RouteFuture {
self.call_with_state(args, ())
Expand Down