Skip to content

Commit 2913a8e

Browse files
authored
feat: convenience methods for axum ws (#28)
1 parent f26985a commit 2913a8e

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description = "Simple, modern, ergonomic JSON-RPC 2.0 router built with tower an
55
keywords = ["json-rpc", "jsonrpc", "json"]
66
categories = ["web-programming::http-server", "web-programming::websocket"]
77

8-
version = "0.3.2"
8+
version = "0.3.3"
99
edition = "2021"
1010
rust-version = "1.81"
1111
authors = ["init4", "James Prestwich"]

src/router.rs

+43-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ where
348348
/// use. This runtime is accessible to all handlers invoked by the router.
349349
///
350350
/// Tasks spawned by the router will be spawned on the provided runtime,
351-
/// and automatically cancelled when the returned `axum::Router` is dropped.
351+
/// and automatically cancelled when the returned [`axum::Router`] is
352+
/// dropped.
352353
#[cfg(feature = "axum")]
353354
pub fn into_axum_with_handle(
354355
self,
@@ -375,6 +376,47 @@ impl Router<()> {
375376
connect.serve(self).await
376377
}
377378

379+
/// Create an [`AxumWsCfg`] from this router. This is a convenience method
380+
/// for `AxumWsCfg::new(self.clone())`.
381+
///
382+
/// [`AxumWsCfg`]: crate::pubsub::AxumWsCfg
383+
#[cfg(all(feature = "axum", feature = "pubsub"))]
384+
pub fn to_axum_cfg(&self) -> crate::pubsub::AxumWsCfg {
385+
crate::pubsub::AxumWsCfg::new(self.clone())
386+
}
387+
388+
/// Create an [`axum::Router`] from this router, serving this router via
389+
/// HTTP `POST` requests at `post_route` and via WebSocket at `ws_route`.
390+
#[cfg(all(feature = "axum", feature = "pubsub"))]
391+
pub fn into_axum_with_ws(self, post_route: &str, ws_route: &str) -> axum::Router<()> {
392+
let cfg = self.to_axum_cfg();
393+
394+
self.into_axum(post_route)
395+
.with_state(())
396+
.route(ws_route, axum::routing::any(crate::pubsub::ajj_websocket))
397+
.with_state(cfg)
398+
}
399+
400+
/// Create an [`axum::Router`] from this router, serving this router via
401+
/// HTTP `POST` requests at `post_route` and via WebSocket at `ws_route`.
402+
/// This convenience method allows users to specify a runtime handle for the
403+
/// router to use. See [`Router::into_axum_with_handle`] for more
404+
/// information.
405+
#[cfg(all(feature = "axum", feature = "pubsub"))]
406+
pub fn into_axum_with_ws_and_handle(
407+
self,
408+
post_route: &str,
409+
ws_route: &str,
410+
handle: tokio::runtime::Handle,
411+
) -> axum::Router<()> {
412+
let cfg = self.to_axum_cfg();
413+
414+
self.into_axum_with_handle(post_route, handle)
415+
.with_state(())
416+
.route(ws_route, axum::routing::any(crate::pubsub::ajj_websocket))
417+
.with_state(cfg)
418+
}
419+
378420
/// Call a method on the router.
379421
pub fn handle_request(&self, args: HandlerArgs) -> RouteFuture {
380422
self.call_with_state(args, ())

0 commit comments

Comments
 (0)