Skip to content

Commit 001451d

Browse files
committed
feat(localhost): add custom host binding to allow external access
1 parent 4341d7f commit 001451d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'localhost': 'minor'
3+
---
4+
5+
Add custom host binding to allow external access

plugins/localhost/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,24 @@ type OnRequest = Option<Box<dyn Fn(&Request, &mut Response) + Send + Sync>>;
4646

4747
pub struct Builder {
4848
port: u16,
49+
host: Option<String>,
4950
on_request: OnRequest,
5051
}
5152

5253
impl Builder {
5354
pub fn new(port: u16) -> Self {
5455
Self {
5556
port,
57+
host: None,
5658
on_request: None,
5759
}
5860
}
5961

62+
pub fn host<H: AsRef<str>>(mut self, host: H) -> Self {
63+
self.host = Some(host.as_ref().to_string());
64+
self
65+
}
66+
6067
pub fn on_request<F: Fn(&Request, &mut Response) + Send + Sync + 'static>(
6168
mut self,
6269
f: F,
@@ -67,14 +74,15 @@ impl Builder {
6774

6875
pub fn build<R: Runtime>(mut self) -> TauriPlugin<R> {
6976
let port = self.port;
77+
let host = self.host.unwrap_or("localhost".to_string());
7078
let on_request = self.on_request.take();
7179

7280
PluginBuilder::new("localhost")
7381
.setup(move |app, _api| {
7482
let asset_resolver = app.asset_resolver();
7583
std::thread::spawn(move || {
7684
let server =
77-
Server::http(format!("localhost:{port}")).expect("Unable to spawn server");
85+
Server::http(format!("{host}:{port}")).expect("Unable to spawn server");
7886
for req in server.incoming_requests() {
7987
let path = req
8088
.url()

0 commit comments

Comments
 (0)