예제 #1
0
def setup_rest(app: web.Application):
    settings: RestSettings = get_plugin_settings(app)
    is_diagnostics_enabled: bool = (app[APP_SETTINGS_KEY].WEBSERVER_DIAGNOSTICS
                                    is not None)

    spec_path = get_openapi_specs_path(api_version_dir=API_VTAG)

    # validated openapi specs
    app[APP_OPENAPI_SPECS_KEY] = specs = load_openapi_specs(spec_path)

    # version check
    base_path = openapi.get_base_path(specs)
    major, *_ = specs.info.version

    if f"/v{major}" != base_path:
        raise ValueError(
            f"REST API basepath {base_path} does not fit openapi.yml version {specs.info.version}"
        )

    if api_version_prefix != f"v{major}":
        raise ValueError(
            f"__version__.api_version_prefix {api_version_prefix} does not fit openapi.yml version {specs.info.version}"
        )

    # basic routes
    app.add_routes(rest_handlers.routes)
    if not is_diagnostics_enabled:
        # NOTE: the healthcheck route is normally in diagnostics, but
        # if disabled, this plugin adds a simple version of it
        app.add_routes([
            web.get(
                path=f"/{api_version_prefix}/health",
                handler=rest_handlers.check_running,
                name="check_health",
            )
        ])

    # middlewares
    # NOTE: using safe get here since some tests use incomplete configs
    app.middlewares.extend([
        error_middleware_factory(
            api_version_prefix,
            log_exceptions=not is_diagnostics_enabled,
        ),
        envelope_middleware_factory(api_version_prefix),
    ])

    # Adds swagger doc UI
    #  - API doc at /dev/doc (optional, e.g. for testing since it can be heavy)
    #  - NOTE: avoid /api/* since traeffik uses for it's own API
    #
    log.debug("OAS loaded from %s ", spec_path)
    if settings.REST_SWAGGER_API_DOC_ENABLED:
        setup_swagger(
            app,
            swagger_url="/dev/doc",
            swagger_from_file=str(spec_path),
            ui_version=3,
        )
예제 #2
0
def setup(app: web.Application, *, swagger_doc_enabled: bool = True):
    # ----------------------------------------------
    # TODO: temporary, just to check compatibility between
    # trafaret and pydantic schemas
    cfg = assert_valid_config(app)
    # ---------------------------------------------

    api_version_dir = cfg["version"]
    spec_path = get_openapi_specs_path(api_version_dir)

    # validated openapi specs
    app[APP_OPENAPI_SPECS_KEY] = specs = load_openapi_specs(spec_path)

    # version check
    base_path = openapi.get_base_path(specs)
    major, *_ = specs.info.version

    if f"/v{major}" != base_path:
        raise ValueError(
            f"REST API basepath {base_path} does not fit openapi.yml version {specs.info.version}"
        )

    if api_version_prefix != f"v{major}":
        raise ValueError(
            f"__version__.api_version_prefix {api_version_prefix} does not fit openapi.yml version {specs.info.version}"
        )

    # diagnostics routes
    routes = rest_routes.create(specs)
    app.router.add_routes(routes)

    # middlewares
    # NOTE: using safe get here since some tests use incomplete configs
    is_diagnostics_enabled = get_diagnostics_config(app).get("enabled", False)
    app.middlewares.extend([
        error_middleware_factory(
            api_version_prefix,
            log_exceptions=not is_diagnostics_enabled,
        ),
        envelope_middleware_factory(api_version_prefix),
    ])

    # Adds swagger doc UI
    #  - API doc at /dev/doc (optional, e.g. for testing since it can be heavy)
    #  - NOTE: avoid /api/* since traeffik uses for it's own API
    #
    log.debug("OAS loaded from %s ", spec_path)
    if swagger_doc_enabled:
        setup_swagger(
            app,
            swagger_url="/dev/doc",
            swagger_from_file=str(spec_path),
            ui_version=3,
        )
def client(event_loop, aiohttp_client, specs):
    app = web.Application()

    # routes
    handlers = Handlers()
    routes = create_routes_from_namespace(specs, handlers, strict=False)
    app.router.add_routes(routes)

    # validators
    app[APP_OPENAPI_SPECS_KEY] = specs

    # middlewares
    base = openapi.get_base_path(specs)
    app.middlewares.append(error_middleware_factory(base))
    app.middlewares.append(envelope_middleware_factory(base))

    return event_loop.run_until_complete(aiohttp_client(app))