Skip to main content

Builder

Struct Builder 

Source
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

Source

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(),
    )
}
Source

pub fn host<H: Into<String>>(self, host: H) -> Self

Sets the host the localhost server binds to. Defaults to localhost.

Source

pub fn on_request<F: Fn(&Request, &mut Response) + Send + Sync + 'static>( self, f: F, ) -> Self

Sets a hook that is called for every request that resolves to a known frontend asset, right before the response is sent. Use it to inspect the Request and add or override headers on the Response, for example to append custom CORS or caching headers.

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more