Skip to main content

Server

Struct Server 

Source
pub struct Server { /* private fields */ }
Expand description

An axum server for one fetchlet/build artifact: Rust routes first, then the artifact’s client assets, then the application.

ⓘ
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await?;
Server::new("build")
    .host_services(|| HostServices::new().env("MESSAGE", "Hello"))
    .routes(|_app| Router::new().route("/api/rust", get(rust_endpoint)))
    .serve(listener)
    .await?;

The engine is the one the artifact was built for. How the build is served follows the crate’s features:

  • With vite-dev and VITE_URL_VARIABLE set, the server module runs from that Vite dev server through ViteDev, which also serves the browser modules, and console output goes to standard error. A request the client cancels runs to its end (CancelledCalls::Finish), since a new instance would load every module from Vite again.
  • Otherwise, with dev, DevReload serves each build written to the directory and reloads open pages.
  • Otherwise the build is opened once.

Keep dev and vite-dev out of release builds.

Implementations§

Source§

impl Server

Source

pub fn new(directory: impl Into<PathBuf>) -> Self

A server for the artifact in directory, the output directory of fetchlet/build. A relative path is resolved against the working directory.

Source

pub fn from_artifact(artifact: Artifact) -> Self

A server for an already-opened or embedded Artifact, such as one created with include_artifact!.

Combine with dev_directory to reload from a build directory when dev-reload is enabled while serving the embedded artifact in release builds without #[cfg] branches.

Source

pub fn dev_directory(self, directory: impl Into<PathBuf>) -> Self

When built with dev-reload and started from an embedded artifact via from_artifact, watch and reload builds from directory instead of serving the embedded copy. Ignored when dev-reload is disabled.

Source

pub fn backend(self, backend: Backend) -> Self

The engine and its options. By default, the engine the artifact was built for, through Backend::for_target; on QuickJS or V8 the limits then get a 64 MiB Limits::heap_bytes unless they set one. While the worker keeps its default stack, QuickJS also gets a 4 MiB stack limit and Boa a value stack of 131,072 slots and a recursion limit of 10,000 calls. A backend given here is used as it is, with the limits as they are. V8Options::reliable_termination stays off unless a backend given here sets it.

Source

pub fn host_services( self, services: impl Fn() -> Result<HostServices> + Send + Sync + 'static, ) -> Self

The host services, made once for each application the server starts: one, or one per build under dev.

Source

pub fn routes( self, routes: impl Fn(&AppHandle) -> Router + Send + Sync + 'static, ) -> Self

Rust routes, which answer before the artifact’s assets and the application. They are made for each application the server starts, whose handle they receive, so a route can call the module’s named exports with AppHandle::call_json.

Source

pub fn limits(self, limits: Limits) -> Self

The application’s caller budgets, used as they are. Server::default_limits by default; start from it to keep its timeouts, or from Limits::default to run without them.

Source

pub fn default_limits() -> Limits

The limits a server has unless limits replaces them: a 10 s busy_timeout and a 60 s startup_timeout, which retire an instance whose JavaScript stops returning to the event loop or whose module never finishes evaluating; no others.

Source

pub fn worker(self, worker: WorkerOptions) -> Self

The application’s worker options. By default, a queue of 8 commands, one instance and a 64 MiB native stack for each worker thread.

Source

pub fn queue_capacity(self, capacity: NonZeroUsize) -> Self

Commands queued behind the running call, keeping the rest of the worker options. 8 by default.

Source

pub fn instances(self, instances: Instances) -> Self

The instances that serve requests, keeping the rest of the worker options. One by default. Each runs one request at a time and has its own engine heap and module state; see Instances.

Source

pub fn isolation(self, isolation: Isolation) -> Self

Where each instance’s engine runs. In this process by default. Under Isolation::Process, each instance runs in a child process, which the server’s executable serves when main calls fetchlet::serve_if_child first. Unless the options set ProcessOptions::stack_bytes, the child’s engine thread gets the worker’s native stack, which the default engine stack limits need.

Source

pub fn public_origin(self, origin: impl Into<String>) -> Self

The origin the application sees in request URLs, such as the one a proxy serves it under. By default, http:// and the listener’s address.

Source

pub fn rendering( self, configure: impl Fn(AxumService) -> AxumService + Send + Sync + 'static, ) -> Self

Adjust the service that passes requests to the application, such as with AxumService::stream_request_bodies or AxumService::trusted_context. Errors are written to standard error before this runs.

Source

pub async fn webview( self, scheme: WebviewScheme, response_body_bytes: usize, ) -> Result<Webview, ServerError>

Start the application for a webview’s custom URI scheme, such as Tauri’s, instead of a listener. The application sees the scheme’s origin unless public_origin names another. response_body_bytes bounds each response the webview receives whole (WebviewResponder).

With dev, each build written to the directory serves the next request, and Webview::reloads tells the host when to reload its window; pages get no reload script. vite-dev does not apply.

Source

pub async fn serve(self, listener: TcpListener) -> Result<(), ServerError>

Serve on listener until Ctrl-C, then shut the application down.

Source

pub async fn serve_with_shutdown( self, listener: TcpListener, signal: impl Future<Output = ()> + Send + 'static, ) -> Result<(), ServerError>

Serve on listener until signal completes, finish the requests in progress, then shut the application down.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Server

§

impl !UnwindSafe for Server

§

impl Forget for Server

§

impl Freeze for Server

§

impl Move for Server

§

impl Send for Server

§

impl Sync for Server

§

impl Unpin for Server

§

impl UnsafeUnpin for Server

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

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> ParallelSend for T

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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