Skip to content

Commit

Permalink
Add build_with constructor
Browse files Browse the repository at this point in the history
This allows you to modify the final router while ensuring that the
connect info is still available to the signaling server.
  • Loading branch information
RaoulHC committed Apr 9, 2024
1 parent 5cd7122 commit 2b783af
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
26 changes: 16 additions & 10 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 @@ -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 2b783af

Please sign in to comment.