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-devandVITE_URL_VARIABLEset, the server module runs from that Vite dev server throughViteDev, 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,DevReloadserves 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
impl Server
Sourcepub fn new(directory: impl Into<PathBuf>) -> Self
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.
Sourcepub fn from_artifact(artifact: Artifact) -> Self
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.
Sourcepub fn dev_directory(self, directory: impl Into<PathBuf>) -> Self
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.
Sourcepub fn backend(self, backend: Backend) -> Self
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.
Sourcepub fn host_services(
self,
services: impl Fn() -> Result<HostServices> + Send + Sync + 'static,
) -> Self
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.
Sourcepub fn routes(
self,
routes: impl Fn(&AppHandle) -> Router + Send + Sync + 'static,
) -> Self
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.
Sourcepub fn limits(self, limits: Limits) -> Self
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.
Sourcepub fn default_limits() -> Limits
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.
Sourcepub fn worker(self, worker: WorkerOptions) -> Self
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.
Sourcepub fn queue_capacity(self, capacity: NonZeroUsize) -> Self
pub fn queue_capacity(self, capacity: NonZeroUsize) -> Self
Commands queued behind the running call, keeping the rest of the worker options. 8 by default.
Sourcepub fn instances(self, instances: Instances) -> Self
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.
Sourcepub fn isolation(self, isolation: Isolation) -> Self
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.
Sourcepub fn public_origin(self, origin: impl Into<String>) -> Self
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.
Sourcepub fn rendering(
self,
configure: impl Fn(AxumService) -> AxumService + Send + Sync + 'static,
) -> Self
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.
Sourcepub async fn webview(
self,
scheme: WebviewScheme,
response_body_bytes: usize,
) -> Result<Webview, ServerError>
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.
Sourcepub async fn serve(self, listener: TcpListener) -> Result<(), ServerError>
pub async fn serve(self, listener: TcpListener) -> Result<(), ServerError>
Serve on listener until Ctrl-C, then shut the application down.
Sourcepub async fn serve_with_shutdown(
self,
listener: TcpListener,
signal: impl Future<Output = ()> + Send + 'static,
) -> Result<(), ServerError>
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.