From 3449dd5a8f6d12fee8d6389c034fe47e19d72bcd Mon Sep 17 00:00:00 2001 From: Ari <67370329+arihav@users.noreply.github.com> Date: Wed, 6 Nov 2024 12:52:49 +0200 Subject: [PATCH] feat(localhost): add custom host binding to allow external access (#1982) Co-authored-by: Fabian-Lars --- .changes/localhost-custom-host-binding.md | 5 +++++ plugins/localhost/src/lib.rs | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 .changes/localhost-custom-host-binding.md 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..ae77cd806 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,17 @@ impl Builder { pub fn new(port: u16) -> Self { Self { port, + host: None, on_request: None, } } + // Change the host the plugin binds to. Defaults to `localhost`. + pub fn host>(mut self, host: H) -> Self { + self.host = Some(host.into()); + self + } + pub fn on_request( mut self, f: F, @@ -67,6 +75,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 +83,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()