Skip to content

Commit

Permalink
Merge pull request #447 from RaoulHC/raoul/builder-additions
Browse files Browse the repository at this point in the history
Add construct_router function
  • Loading branch information
johanhelsing authored Dec 15, 2024
2 parents 0179f4d + 18357c7 commit eba5c4d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
28 changes: 17 additions & 11 deletions matchbox_signaling/src/signaling_server/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
};
use axum::{response::Response, routing::get, Extension, Router};
use matchbox_protocol::PeerId;
use std::net::SocketAddr;
use std::{convert::identity, net::SocketAddr};
use tower_http::{
cors::{Any, CorsLayer},
trace::{DefaultOnResponse, TraceLayer},
Expand Down Expand Up @@ -66,7 +66,7 @@ where

/// Modify the router with a mutable closure. This is where one may apply middleware or other
/// layers to the Router.
pub fn mutate_router(mut self, mut alter: impl FnMut(Router) -> Router) -> Self {
pub fn mutate_router(mut self, alter: impl FnOnce(Router) -> Router) -> Self {
self.router = alter(self.router);
self
}
Expand Down Expand Up @@ -116,18 +116,24 @@ where

/// Create a [`SignalingServer`].
pub fn build(self) -> SignalingServer {
self.build_with(identity)
}

/// Create a [`SignalingServer`] with a closure that modifies the signaling router
pub fn build_with(self, alter: impl FnOnce(Router) -> Router) -> SignalingServer {
// Insert topology
let state_machine: SignalingStateMachine<Cb, S> =
SignalingStateMachine::from_topology(self.topology);
let info = self
.router
.route("/", get(ws_handler::<Cb, S>))
.route("/:path", get(ws_handler::<Cb, S>))
.layer(Extension(state_machine))
.layer(Extension(self.shared_callbacks))
.layer(Extension(self.callbacks))
.layer(Extension(self.state))
.into_make_service_with_connect_info::<SocketAddr>();
let info = alter(
self.router
.route("/", get(ws_handler::<Cb, S>))
.route("/:path", get(ws_handler::<Cb, S>))
.layer(Extension(state_machine))
.layer(Extension(self.shared_callbacks))
.layer(Extension(self.callbacks))
.layer(Extension(self.state)),
)
.into_make_service_with_connect_info::<SocketAddr>();
SignalingServer {
requested_addr: self.socket_addr,
info,
Expand Down
18 changes: 18 additions & 0 deletions matchbox_signaling/tests/full_mesh.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(test)]
mod tests {
use axum::Router;
use futures::{SinkExt, StreamExt};
use matchbox_protocol::{JsonPeerEvent, PeerId};
use matchbox_signaling::SignalingServer;
Expand Down Expand Up @@ -54,6 +55,23 @@ mod tests {
assert!(matches!(id_assigned_event, JsonPeerEvent::IdAssigned(..)));
}

#[tokio::test]
async fn nested_server() {
let mut server = SignalingServer::full_mesh_builder((Ipv4Addr::LOCALHOST, 0))
.build_with(|router| Router::new().nest("/nested/", router));
let addr = server.bind().unwrap();
tokio::spawn(server.serve());

let (mut client, _response) =
tokio_tungstenite::connect_async(format!("ws://{addr}/nested/room_a"))
.await
.unwrap();

let id_assigned_event = recv_peer_event(&mut client).await;

assert!(matches!(id_assigned_event, JsonPeerEvent::IdAssigned(..)));
}

#[tokio::test]
async fn new_peer() {
let mut server = SignalingServer::full_mesh_builder((Ipv4Addr::LOCALHOST, 0)).build();
Expand Down

0 comments on commit eba5c4d

Please sign in to comment.