Skip to content

Commit

Permalink
support for static serving from dev command
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb committed May 5, 2024
1 parent d3a2473 commit 63424b2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
64 changes: 64 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions stateroom-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ dashmap = "5.5.3"
futures-util = "0.3.30"
stateroom = {path="../stateroom", version="0.4.1"}
tokio = { version = "1.37.0", features = ["rt-multi-thread"] }
tower-http = { version="0.5.2", features=["fs"] }
tracing = "0.1.40"
11 changes: 10 additions & 1 deletion stateroom-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{
time::Duration,
};
use tokio::{net::TcpListener, select};
use tower_http::services::ServeDir;

mod server;

Expand Down Expand Up @@ -106,10 +107,18 @@ impl Server {
pub async fn serve_async(self, factory: impl StateroomServiceFactory) -> std::io::Result<()> {
let server_state = Arc::new(ServerState::new(factory));

let app = Router::new()
let mut app = Router::new()
.route("/ws", get(serve_websocket))
.with_state(server_state);

if let Some(static_path) = self.static_path {
app = app.nest_service("/", ServeDir::new(static_path));
}

if let Some(client_path) = self.client_path {
app = app.nest_service("/client", ServeDir::new(client_path));
}

let ip = self.ip.parse::<IpAddr>().unwrap();
let addr = SocketAddr::new(ip, self.port);
let listener = TcpListener::bind(&addr).await?;
Expand Down

0 comments on commit 63424b2

Please sign in to comment.