GitHub

API reference for server.

HTTP servers (Effect's HttpServer): the HttpServer service a backend provides (live_server.layer), serving an app as a layer, and the test client.

::

app = router.to_http_effect(...)
main = server.serve(app).pipe(layer.provide(live_server.layer(port=8080)))

Values

AttributeTypeDescription
log_addressEffect[None, Never, HttpServer]
make_test_clientEffect[HttpClientService[Any], Never, HttpServer | HttpClient]

The HttpClient with the server's origin prepended to each URL (the loopback address for a server on an unspecified one).

layer_test_clientLayer[HttpClient, Never, HttpServer | HttpClient]

An HttpClient sending to the HttpServer (make_test_client), built on the HttpClient it is provided with.

layer_servicesLayer[Path | HttpPlatform | FileSystem | Generator]

What servers need: HttpPlatform, Path, weak ETags, and a no-op FileSystem.

Types

TypeDefinitionDescription
HttpApptype HttpApp = Effect[HttpServerResponse, Any, Any]
Middlewaretype Middleware = Callable[[HttpApp], HttpApp]

Classes

Functions

make

make(*, serve, address) -> HttpServerService
ParameterTypeDescription
serveCallable[[HttpApp, Middleware | None], Effect[None, Never, Scope]]
addressSocketAddress

Returns HttpServerService

def make(
    *,
    serve: Callable[[HttpApp, Middleware | None], Effect[None, Never, Scope]],
    address: SocketAddress,
) -> HttpServerService:
    return HttpServerService(serve, address)

serve_effect

serve_effect[E](app: _AppIn[E, Never], /) -> Effect[None, Never, Scope | HttpServer]
serve_effect[E, R](app: _AppIn[E, R], /) -> Effect[None, Never, Scope | HttpServer | R]
serve_effect[E, EM](app: _AppIn[E, Never], middleware: Callable[[_AppIn[E, Never]], _AppIn[EM, Never]], /) -> Effect[None, Never, Scope | HttpServer]
serve_effect[E, EM, RM](app: _AppIn[E, Never], middleware: Callable[[_AppIn[E, Never]], _AppIn[EM, RM]], /) -> Effect[None, Never, Scope | HttpServer | RM]
serve_effect[E, R, EM, RM](app: _AppIn[E, R], middleware: Callable[[_AppIn[E, R]], _AppIn[EM, RM]], /) -> Effect[None, Never, Scope | HttpServer | RM]

Serve app (through middleware) until the scope closes.

ParameterTypeDescription
app
middleware= None
@overload
def serve_effect[E](app: _AppIn[E, Never], /) -> Effect[None, Never, Scope | HttpServer]: ...
@overload
def serve_effect[E, R](app: _AppIn[E, R], /) -> Effect[None, Never, Scope | HttpServer | R]: ...
@overload
def serve_effect[E, EM](
    app: _AppIn[E, Never], middleware: Callable[[_AppIn[E, Never]], _AppIn[EM, Never]], /
) -> Effect[None, Never, Scope | HttpServer]: ...
@overload
def serve_effect[E, EM, RM](
    app: _AppIn[E, Never], middleware: Callable[[_AppIn[E, Never]], _AppIn[EM, RM]], /
) -> Effect[None, Never, Scope | HttpServer | RM]: ...
@overload
def serve_effect[E, R, EM, RM](
    app: _AppIn[E, R], middleware: Callable[[_AppIn[E, R]], _AppIn[EM, RM]], /
) -> Effect[None, Never, Scope | HttpServer | RM]: ...
def serve_effect(
    app: Effect[HttpServerResponse, Any, Any], middleware: Middleware | None = None, /
) -> Effect[None, Never, Any]:
    """Serve ``app`` (through ``middleware``) until the scope closes."""
    require_effect(app, "app")
    return HttpServer().flat_map(lambda server: server.serve(app, middleware))

serve

serve[E](app: _AppIn[E, Never], /) -> Layer[Never, Never, HttpServer]
serve[E, R](app: _AppIn[E, R], /) -> Layer[Never, Never, HttpServer | R]
serve[E, EM](app: _AppIn[E, Never], middleware: Callable[[_AppIn[E, Never]], _AppIn[EM, Never]], /) -> Layer[Never, Never, HttpServer]
serve[E, EM, RM](app: _AppIn[E, Never], middleware: Callable[[_AppIn[E, Never]], _AppIn[EM, RM]], /) -> Layer[Never, Never, HttpServer | RM]
serve[E, R, EM, RM](app: _AppIn[E, R], middleware: Callable[[_AppIn[E, R]], _AppIn[EM, RM]], /) -> Layer[Never, Never, HttpServer | RM]

A layer serving app (through middleware) while it is in use.

ParameterTypeDescription
app
middleware= None
@overload
def serve[E](app: _AppIn[E, Never], /) -> Layer[Never, Never, HttpServer]: ...
@overload
def serve[E, R](app: _AppIn[E, R], /) -> Layer[Never, Never, HttpServer | R]: ...
@overload
def serve[E, EM](
    app: _AppIn[E, Never], middleware: Callable[[_AppIn[E, Never]], _AppIn[EM, Never]], /
) -> Layer[Never, Never, HttpServer]: ...
@overload
def serve[E, EM, RM](
    app: _AppIn[E, Never], middleware: Callable[[_AppIn[E, Never]], _AppIn[EM, RM]], /
) -> Layer[Never, Never, HttpServer | RM]: ...
@overload
def serve[E, R, EM, RM](
    app: _AppIn[E, R], middleware: Callable[[_AppIn[E, R]], _AppIn[EM, RM]], /
) -> Layer[Never, Never, HttpServer | RM]: ...
def serve(
    app: Effect[HttpServerResponse, Any, Any], middleware: Middleware | None = None, /
) -> Layer[Never, Never, Any]:
    """A layer serving ``app`` (through ``middleware``) while it is in
    use."""
    require_effect(app, "app")
    return effect_discard(serve_effect(app, middleware) if middleware else serve_effect(app))

format_address

format_address(address) -> str

http://host:port, or unix://path.

ParameterTypeDescription
addressSocketAddress

Returns str

def format_address(address: SocketAddress) -> str:
    """``http://host:port``, or ``unix://path``."""
    return format_url_unsafe(address)

address_formatted_with

address_formatted_with[A, E, R](f) -> Effect[A, E, HttpServer | R]
ParameterTypeDescription
fCallable[[str], Effect[A, E, R]]

Returns Effect[A, E, HttpServer | R]

def address_formatted_with[A, E, R](
    f: Callable[[str], Effect[A, E, R]],
) -> Effect[A, E, HttpServer | R]:
    return HttpServer().flat_map(lambda server: f(format_address(server.address)))

with_log_address

with_log_address[A, E, R](layer) -> Layer[A, E, R | HttpServer]

layer, logging the server's address once built.

ParameterTypeDescription
layerLayer[A, E, R]

Returns Layer[A, E, R | HttpServer]

def with_log_address[A, E, R](layer: Layer[A, E, R]) -> Layer[A, E, R | HttpServer]:
    """``layer``, logging the server's address once built."""
    return effect_discard(log_address).pipe(provide_merge(layer))