Skip to content

Commit 3449dd5

Browse files
arihavFabianLars
andauthored
feat(localhost): add custom host binding to allow external access (#1982)
Co-authored-by: Fabian-Lars <[email protected]>
1 parent 57f69c6 commit 3449dd5

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,25 @@ 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+
// Change the host the plugin binds to. Defaults to `localhost`.
63+
pub fn host<H: Into<String>>(mut self, host: H) -> Self {
64+
self.host = Some(host.into());
65+
self
66+
}
67+
6068
pub fn on_request<F: Fn(&Request, &mut Response) + Send + Sync + 'static>(
6169
mut self,
6270
f: F,
@@ -67,14 +75,15 @@ impl Builder {
6775

6876
pub fn build<R: Runtime>(mut self) -> TauriPlugin<R> {
6977
let port = self.port;
78+
let host = self.host.unwrap_or("localhost".to_string());
7079
let on_request = self.on_request.take();
7180

7281
PluginBuilder::new("localhost")
7382
.setup(move |app, _api| {
7483
let asset_resolver = app.asset_resolver();
7584
std::thread::spawn(move || {
7685
let server =
77-
Server::http(format!("localhost:{port}")).expect("Unable to spawn server");
86+
Server::http(format!("{host}:{port}")).expect("Unable to spawn server");
7887
for req in server.incoming_requests() {
7988
let path = req
8089
.url()

0 commit comments

Comments
 (0)