Skip to content

Commit

Permalink
feat(localhost): add custom host binding to allow external access
Browse files Browse the repository at this point in the history
  • Loading branch information
arihav committed Oct 29, 2024
1 parent 4341d7f commit 7ddc9f4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/localhost-custom-host-binding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'localhost': 'minor'
---

Add custom host binding to allow external access
10 changes: 9 additions & 1 deletion plugins/localhost/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,24 @@ type OnRequest = Option<Box<dyn Fn(&Request, &mut Response) + Send + Sync>>;

pub struct Builder {
port: u16,
host: Option<String>,
on_request: OnRequest,
}

impl Builder {
pub fn new(port: u16) -> Self {
Self {
port,
host: None,
on_request: None,
}
}

pub fn host<H: AsRef<str>>(mut self, host: H) -> Self {
self.host = Some(host.as_ref().to_string());
self
}

pub fn on_request<F: Fn(&Request, &mut Response) + Send + Sync + 'static>(
mut self,
f: F,
Expand All @@ -67,14 +74,15 @@ impl Builder {

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

PluginBuilder::new("localhost")
.setup(move |app, _api| {
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()
Expand Down

0 comments on commit 7ddc9f4

Please sign in to comment.