@@ -14,9 +14,8 @@ use kuchiki::{traits::TendrilSink, NodeRef};
1414use notify:: RecursiveMode ;
1515use notify_debouncer_mini:: new_debouncer;
1616use std:: {
17- net:: SocketAddr ,
17+ net:: { Ipv4Addr , SocketAddr } ,
1818 path:: { Path , PathBuf } ,
19- str:: FromStr ,
2019 sync:: { mpsc:: sync_channel, Arc } ,
2120 thread,
2221 time:: Duration ,
@@ -25,15 +24,23 @@ use tauri_utils::mime_type::MimeType;
2524use tokio:: sync:: broadcast:: { channel, Sender } ;
2625
2726const AUTO_RELOAD_SCRIPT : & str = include_str ! ( "./auto-reload.js" ) ;
28- pub const SERVER_URL : & str = "http://127.0.0.1:1430" ;
2927
3028struct State {
3129 serve_dir : PathBuf ,
3230 tx : Sender < ( ) > ,
3331}
3432
35- pub fn start_dev_server < P : AsRef < Path > > ( path : P ) {
33+ pub fn start_dev_server < P : AsRef < Path > > ( path : P , port : Option < u16 > ) -> SocketAddr {
3634 let serve_dir = path. as_ref ( ) . to_path_buf ( ) ;
35+ let server_url = SocketAddr :: new (
36+ Ipv4Addr :: new ( 127 , 0 , 0 , 1 ) . into ( ) ,
37+ port. unwrap_or_else ( || {
38+ std:: env:: var ( "TAURI_DEV_SERVER_PORT" )
39+ . unwrap_or_else ( |_| "1430" . to_string ( ) )
40+ . parse ( )
41+ . unwrap ( )
42+ } ) ,
43+ ) ;
3744
3845 std:: thread:: spawn ( move || {
3946 tokio:: runtime:: Builder :: new_current_thread ( )
@@ -84,12 +91,14 @@ pub fn start_dev_server<P: AsRef<Path>>(path: P) {
8491 ws. on_upgrade ( |socket| async move { ws_handler ( socket, state) . await } )
8592 } ) ,
8693 ) ;
87- Server :: bind ( & SocketAddr :: from_str ( SERVER_URL . split ( '/' ) . nth ( 2 ) . unwrap ( ) ) . unwrap ( ) )
94+ Server :: bind ( & server_url )
8895 . serve ( router. into_make_service ( ) )
8996 . await
9097 . unwrap ( ) ;
9198 } )
9299 } ) ;
100+
101+ server_url
93102}
94103
95104async fn handler < T > ( req : Request < T > , state : Arc < State > ) -> impl IntoResponse {
0 commit comments