11use std:: collections:: HashMap ;
2- use std:: fs;
3- use std:: path:: { Path , PathBuf } ;
2+ use include_dir:: { include_dir, Dir } ;
43use std:: sync:: { Arc , Mutex } ;
54use std:: thread;
65
@@ -12,7 +11,7 @@ use tokio::sync::mpsc;
1211use tokio_tungstenite:: tungstenite:: Message ;
1312use uuid:: Uuid ;
1413
15- use crate :: commands :: refresh_obs_browser_source ;
14+ static DIST_DIR : Dir < ' _ > = include_dir ! ( "$CARGO_MANIFEST_DIR/../dist" ) ;
1615
1716pub struct Client {
1817 pub id : String ,
@@ -36,31 +35,22 @@ impl OverlayServer {
3635 pub fn start_http_server ( & self ) {
3736 let addr = "127.0.0.1:4000" ;
3837 let server = TinyHttpServer :: http ( addr) . expect ( "Failed to start HTTP server" ) ;
39- let dist_dir = Path :: new ( env ! ( "CARGO_MANIFEST_DIR" ) )
40- . join ( "../dist" )
41- . canonicalize ( )
42- . expect ( "Failed to canonicalize dist path" ) ;
4338
4439 println ! ( "HTTP overlay server running at http://{}" , addr) ;
4540
4641 thread:: spawn ( move || {
4742 for request in server. incoming_requests ( ) {
4843 let url_path = request. url ( ) . trim_start_matches ( '/' ) ;
4944
50- let file_path : PathBuf = if url_path == "overlay" || url_path. is_empty ( ) {
51- dist_dir . join ( "src/overlay.html" )
45+ let file = if url_path == "overlay" || url_path. is_empty ( ) {
46+ DIST_DIR . get_file ( "src/overlay.html" )
5247 } else {
53- let candidate = dist_dir. join ( url_path) ;
54- if candidate. exists ( ) && candidate. is_file ( ) {
55- candidate
56- } else {
57- PathBuf :: new ( )
58- }
48+ DIST_DIR . get_file ( url_path)
5949 } ;
6050
61- let response = if file_path . exists ( ) && file_path . is_file ( ) {
62- let content = fs :: read ( & file_path ) . unwrap_or_default ( ) ;
63- let mime = match file_path . extension ( ) . and_then ( |s| s. to_str ( ) ) {
51+ let response = if let Some ( file ) = file {
52+ let content = file . contents ( ) ;
53+ let mime = match file . path ( ) . extension ( ) . and_then ( |s| s. to_str ( ) ) {
6454 Some ( "css" ) => "text/css" ,
6555 Some ( "js" ) => "application/javascript" ,
6656 Some ( "html" ) => "text/html" ,
@@ -80,7 +70,7 @@ impl OverlayServer {
8070 }
8171
8272 pub async fn start_ws_server (
83- & self ,
73+ & self ,
8474 ws_addr : & str ,
8575 ) -> Result < ( ) , Box < dyn std:: error:: Error + Send + Sync > > {
8676 println ! ( "WebSocket server running on ws://{}" , ws_addr) ;
0 commit comments