GitHub

static_server

API reference for static_server.

Serving files from a directory (Effect's HttpStaticServer).

make(root=...) is an app serving the files below root: a directory serves its index (redirecting to the path with a trailing / first), byte ranges get 206 (416 when unsatisfiable), If-None-Match / If-Modified-Since get 304, and with spa an extensionless miss that accepts HTML serves the index. layer(...) adds it to the HttpRouter as GET /*.

Types

TypeDefinitionDescription
StaticApptype StaticApp = Effect[HttpServerResponse, HttpServerError, HttpServerRequest]

Classes

Functions

make

make(**options)

The app serving options["root"] (see the module docs).

ParameterTypeDescription
optionsUnpack[Options] = {}
@fn_untraced
def make(**options: Unpack[Options]):
    """The app serving ``options["root"]`` (see the module docs)."""
    file_system = yield from FileSystem()
    path = yield from Path()
    platform = yield from HttpPlatform()
    server = _Server(
        file_system,
        path,
        platform,
        path.resolve(options["root"]),
        options.get("index", "index.html"),
        options.get("spa", False),
        options.get("cache_control"),
        {**DEFAULT_MIME_TYPES, **options.get("mime_types", {})},
    )
    app: StaticApp = HttpServerRequest().flat_map(server.handle)
    return app

layer

layer(**options) -> Layer[Never, PlatformError, HttpRouter | FileSystem | Path | HttpPlatform]

make(...) added to the HttpRouter as GET /* (under prefix when given), its errors answered as their responses.

ParameterTypeDescription
optionsUnpack[LayerOptions] = {}

Returns Layer[Never, PlatformError, HttpRouter | FileSystem | Path | HttpPlatform]

def layer(
    **options: Unpack[LayerOptions],
) -> Layer[Never, PlatformError, HttpRouter | FileSystem | Path | HttpPlatform]:
    """``make(...)`` added to the ``HttpRouter`` as ``GET /*`` (under
    ``prefix`` when given), its errors answered as their responses."""
    prefix = options.pop("prefix", None)

    @fn_untraced
    def build():
        router = yield from HttpRouter()
        app: StaticApp = yield from make(**options)
        handler = app.catch_all(to_response)
        target = router if prefix is None else router.prefixed(prefix)
        yield from target.add("GET", "/*", handler)

    return effect_discard(build())