Skip to content

Web App

1 min read

Import: import system.webapi;

See the Web APIs guide for runnable examples, lifecycle, and error behavior.

WebApp

An isolated native HTTP application with explicitly mounted routers.

public class WebApp

state

Application-wide values shared by every request; each request sees this map as RequestContext.app_state.

public state: Map<string, string>

constructor

Creates a WebApp with default settings.

public constructor()

constructor

Creates a WebApp with options.

public constructor(options: WebAppOptions)

use

Appends an outer-to-inner middleware layer before startup.

public fun use(middleware: Middleware): Result<Unit, IoError>

include_router

Mounts a router with an optional additional path prefix.

public fun include_router(router: WebRouter, prefix: string = ""): Result<Unit, IoError>

override_dependencies

Installs generated dependency replacements before startup.

public fun override_dependencies(overrides: DependencyOverrides): Result<Unit, IoError>

lifespan

Pairs one startup acquisition with its shutdown release; completed pairs unwind in reverse order.

public fun lifespan(
    start: fun(WebApp): Future<Result<Unit, IoError>>,
    stop: fun(WebApp): Future<void>
): Result<Unit, IoError>

shutdown

Stops accepting traffic and cancels active transport operations on this app.

public fun shutdown(): void

wait

Drains this application's requests and releases its listener.

public fun wait(token: Option<CancellationToken> = Option.None): Future<void>

listen

Binds this app with the default ServerConfig and returns the bound port; port zero selects an ephemeral port.

public fun listen(
    host: string,
    port: int,
    token: Option<CancellationToken> = Option.None
): Future<Result<int, IoError>>

listen

Binds this app with the transport settings in config and returns the bound port; port zero selects an ephemeral port. Negative settings fail with EINVAL.

public fun listen(
    host: string,
    port: int,
    config: ServerConfig,
    token: Option<CancellationToken> = Option.None
): Future<Result<int, IoError>>

run

Listens with the default ServerConfig and waits until shutdown or cancellation.

public fun run(
    host: string,
    port: int,
    token: Option<CancellationToken> = Option.None
): Future<Result<Unit, IoError>>

run

Listens with config and waits until shutdown or cancellation, like uvicorn.run.

public fun run(
    host: string,
    port: int,
    config: ServerConfig,
    token: Option<CancellationToken> = Option.None
): Future<Result<Unit, IoError>>

openapi

Produces documentation for this application's mounted routes only.

public fun openapi(): string