pub struct Builder { /* private fields */ }Expand description
Builds the localhost plugin.
Note: This plugin brings considerable security risks and you should only use it if you know what you are doing. Because the server has no authentication, any local process can connect to it and read the assets it serves. If in doubt, use the default custom protocol implementation.
Implementations§
Source§impl Builder
impl Builder
Sourcepub fn new(port: u16) -> Self
pub fn new(port: u16) -> Self
Creates a new Builder that will serve the app’s assets on the given port, bound to
localhost unless Self::host is called.
§Examples
fn setup<R: tauri::Runtime>(builder: tauri::Builder<R>) -> tauri::Builder<R> {
builder.plugin(
tauri_plugin_localhost::Builder::new(9527)
.host("127.0.0.1")
.on_request(|request, response| {
println!("{}", request.url());
})
.build(),
)
}Sourcepub fn host<H: Into<String>>(self, host: H) -> Self
pub fn host<H: Into<String>>(self, host: H) -> Self
Sets the host the localhost server binds to. Defaults to localhost.
Sourcepub fn on_request<F: Fn(&Request, &mut Response) + Send + Sync + 'static>(
self,
f: F,
) -> Self
pub fn on_request<F: Fn(&Request, &mut Response) + Send + Sync + 'static>( self, f: F, ) -> Self
Sourcepub fn build<R: Runtime>(self) -> TauriPlugin<R>
pub fn build<R: Runtime>(self) -> TauriPlugin<R>
Builds the plugin, ready to be registered with tauri::Builder::plugin.
On setup it spawns a background thread that starts a tiny_http server bound to
host:port. For every incoming request whose path resolves to a known frontend asset, the
server responds with that asset’s bytes, automatically setting the Content-Type, the
Content-Security-Policy (when the asset has one) and a Cache-Control: no-cache header,
then invoking the Self::on_request hook (if any) before writing the response.
§Panics
Panics on the background thread if the server fails to bind to host:port, or if it fails
to send a response for a request.