diff --git a/.changes/localhost-custom-host-binding.md b/.changes/localhost-custom-host-binding.md new file mode 100644 index 000000000..b5bd3b53d --- /dev/null +++ b/.changes/localhost-custom-host-binding.md @@ -0,0 +1,5 @@ +--- +'localhost': 'minor' +--- + +Add custom host binding to allow external access \ No newline at end of file diff --git a/plugins/localhost/src/lib.rs b/plugins/localhost/src/lib.rs index a0c4c794f..1bfa3b299 100644 --- a/plugins/localhost/src/lib.rs +++ b/plugins/localhost/src/lib.rs @@ -46,6 +46,7 @@ type OnRequest = Option>; pub struct Builder { port: u16, + host: Option, on_request: OnRequest, } @@ -53,10 +54,16 @@ impl Builder { pub fn new(port: u16) -> Self { Self { port, + host: None, on_request: None, } } + pub fn host>(mut self, host: H) -> Self { + self.host = Some(host.as_ref().to_string()); + self + } + pub fn on_request( mut self, f: F, @@ -67,6 +74,7 @@ impl Builder { pub fn build(mut self) -> TauriPlugin { let port = self.port; + let host = self.host.unwrap_or("localhost".to_string()); let on_request = self.on_request.take(); PluginBuilder::new("localhost") @@ -74,7 +82,7 @@ impl Builder { let asset_resolver = app.asset_resolver(); std::thread::spawn(move || { let server = - Server::http(format!("localhost:{port}")).expect("Unable to spawn server"); + Server::http(format!("{host}:{port}")).expect("Unable to spawn server"); for req in server.incoming_requests() { let path = req .url()