Help for web.proxy.setup_app

Project

web.proxy

Function

setup_app

Sample CLI

gway web.proxy setup-app

References

['unwrap']

Full Code

def setup_app(*, endpoint: str, app=None, websockets: bool = False, path: str = "/"):
    """
    Create an HTTP (and optional WebSocket) proxy to the given endpoint.
    Accepts positional apps, the `app=` kwarg, or both. Flattens any iterables
    and selects apps by type using gw.filter_apps.

    Returns a single app if one is provided, otherwise a tuple of apps.
    """
    # selectors for app types
    from bottle import Bottle

    def is_bottle_app(candidate) -> bool:
        return isinstance(candidate, Bottle)

    def is_fastapi_app(candidate) -> bool:
        return hasattr(candidate, "websocket")

    # collect apps by type
    bottle_app = gw.unwrap(app, Bottle)
    fastapi_app = gw.unwrap(app, FastAPI)

    prepared = []

    # if no matching apps, default to a new Bottle
    if not bottle_app and not fastapi_app:
        default = Bottle()
        prepared.append(_wire_proxy(default, endpoint, websockets, path))
    elif bottle_app:
        prepared.append(_wire_proxy(bottle_app, endpoint, websockets, path))
    elif fastapi_app:
        prepared.append(_wire_proxy(fastapi_app, endpoint, websockets, path))

    # TODO: Test if this is properly compatible with web.server.start_app
    return prepared[0] if len(prepared) == 1 else tuple(prepared)