Skip to content

crate

weft_server

A tiny, dependency-free static file server for Weft apps.

Built as a static binary it lets a container ship as FROM scratch with only the server and the site — a few megabytes in total. It serves .wasm with the right MIME type, prefers precompressed .gz files, falls back to index.html for client-side routes, exposes /healthz, and refuses path traversal.

Structs

  • Config

    How to serve: the port, the directory, and whether unknown paths get index.html.

  • Request

    An HTTP request, as the server reads it: its method, path (with the query) and headers.

  • Response

    An HTTP response: its status, headers and body.

Functions

bind

pub fn bind(config: &Config) -> Result<TcpListener>

Listen on every interface, on config.port.

cache_control

pub fn cache_control(_path: &Path) -> &'static str

Asset names aren't content-hashed, so clients always revalidate (a cheap 304 thanks to etag) and new deployments are picked up immediately.

etag

pub fn etag(len: u64, mtime_secs: u64) -> String

A validator built from file size and modification time.

fits

pub fn fits(pattern: &str, path: &str) -> bool

Whether path fits a route pattern.

handle

pub fn handle(root: &Path, spa: bool, req: &Request) -> Response

Produce the response for a request.

health_check

pub fn health_check(port: u16) -> bool

GET /healthz on localhost; used as the container health check.

mime_type

pub fn mime_type(path: &Path) -> &'static str

The Content-Type for a file, by its extension (application/octet-stream when unknown).

parse_request

pub fn parse_request(head: &str) -> Result<Request, String>

Parse an HTTP/1.x request head.

percent_decode

pub fn percent_decode(s: &str) -> Option<String>

Decode %XX escapes; None for malformed escapes or invalid UTF-8.

read_head

pub fn read_head(r: &mut impl Read) -> Result<String>

Read a request head (through the blank line) without consuming the body.

redirect_location

pub fn redirect_location(root: &Path, url_path: &str) -> Option<String>

/blog → /blog/ (keeping the query) when blog/ is a directory with an index.html, so the page's relative asset paths resolve.

resolve_path

pub fn resolve_path(root: &Path, url_path: &str) -> Option<PathBuf>

Map a URL path to an existing file under root, refusing traversal.

rewrites

pub fn rewrites(json: &str) -> Vec<(String, String)>

The rewrites in an app's routes.json (written by Weft's prerender): (pattern, destination), where a pattern is a route's (/blog/:slug/, /docs/*path/).

run

pub fn run(args: Vec<String>) -> i32

CLI entry point; returns the process exit code.

serve

pub fn serve(listener: TcpListener, config: Config)

Accept connections forever, one thread per connection.

status_reason

pub fn status_reason(status: u16) -> &'static str

The reason phrase for an HTTP status (404 → Not Found).

Constants

MAX_HEAD

pub const MAX_HEAD: usize = _;

Largest request head we accept.