Skip to content

Worker CORS

gutenberg_kg.serve.cors

CORS for the local worker, so the Knowledge Press Forest can call it.

RunPod's local API server (--rp_serve_api) sends no CORS headers, so a browser page cannot read its answers. :class:CORSMiddleware is plain ASGI: it answers preflight requests from the allowed origins and adds Access-Control-Allow-Origin to their responses. A request from any other origin passes through untouched, without CORS headers, so the browser refuses it as before.

The handler installs it only when GUTENKG_CORS_ORIGINS is set (see :func:cors_origins).

CORSMiddleware(app, origins)

Allow POST requests from a fixed set of origins.

Parameters:

Name Type Description Default
app

The ASGI app to wrap.

required
origins frozenset[str]

The allowed origins, as :func:cors_origins returns them.

required
Source code in src/gutenberg_kg/serve/cors.py
47
48
49
def __init__(self, app, origins: frozenset[str]) -> None:
    self.app = app
    self.origins = origins

cors_origins(value)

The allowed origins in a GUTENKG_CORS_ORIGINS value.

Parameters:

Name Type Description Default
value str

Comma-separated origins, e.g. "http://localhost:5173,https://flux-frontiers.github.io".

required

Returns:

Type Description
frozenset[str]

Each origin, trimmed, without a trailing slash; empty for "".

Source code in src/gutenberg_kg/serve/cors.py
23
24
25
26
27
28
29
30
def cors_origins(value: str) -> frozenset[str]:
    """The allowed origins in a ``GUTENKG_CORS_ORIGINS`` value.

    :param value: Comma-separated origins, e.g.
        ``"http://localhost:5173,https://flux-frontiers.github.io"``.
    :returns: Each origin, trimmed, without a trailing slash; empty for ``""``.
    """
    return frozenset(o.strip().rstrip("/") for o in value.split(",") if o.strip())