mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-30 17:48:50 +02:00
feat(localhost): add custom host binding to allow external access (#1982)
Co-authored-by: Fabian-Lars <github@fabianlars.de>
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'localhost': 'minor'
|
||||||
|
---
|
||||||
|
|
||||||
|
Add custom host binding to allow external access
|
||||||
@@ -46,6 +46,7 @@ type OnRequest = Option<Box<dyn Fn(&Request, &mut Response) + Send + Sync>>;
|
|||||||
|
|
||||||
pub struct Builder {
|
pub struct Builder {
|
||||||
port: u16,
|
port: u16,
|
||||||
|
host: Option<String>,
|
||||||
on_request: OnRequest,
|
on_request: OnRequest,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,10 +54,17 @@ impl Builder {
|
|||||||
pub fn new(port: u16) -> Self {
|
pub fn new(port: u16) -> Self {
|
||||||
Self {
|
Self {
|
||||||
port,
|
port,
|
||||||
|
host: None,
|
||||||
on_request: None,
|
on_request: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Change the host the plugin binds to. Defaults to `localhost`.
|
||||||
|
pub fn host<H: Into<String>>(mut self, host: H) -> Self {
|
||||||
|
self.host = Some(host.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn on_request<F: Fn(&Request, &mut Response) + Send + Sync + 'static>(
|
pub fn on_request<F: Fn(&Request, &mut Response) + Send + Sync + 'static>(
|
||||||
mut self,
|
mut self,
|
||||||
f: F,
|
f: F,
|
||||||
@@ -67,6 +75,7 @@ impl Builder {
|
|||||||
|
|
||||||
pub fn build<R: Runtime>(mut self) -> TauriPlugin<R> {
|
pub fn build<R: Runtime>(mut self) -> TauriPlugin<R> {
|
||||||
let port = self.port;
|
let port = self.port;
|
||||||
|
let host = self.host.unwrap_or("localhost".to_string());
|
||||||
let on_request = self.on_request.take();
|
let on_request = self.on_request.take();
|
||||||
|
|
||||||
PluginBuilder::new("localhost")
|
PluginBuilder::new("localhost")
|
||||||
@@ -74,7 +83,7 @@ impl Builder {
|
|||||||
let asset_resolver = app.asset_resolver();
|
let asset_resolver = app.asset_resolver();
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
let server =
|
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() {
|
for req in server.incoming_requests() {
|
||||||
let path = req
|
let path = req
|
||||||
.url()
|
.url()
|
||||||
|
|||||||
Reference in New Issue
Block a user