Exemplo n.º 1
0
def main(host: Optional[str], port: Optional[int], path: Optional[str],
         start_streams: bool, config_files: List[str],
         api_file: Optional[str]):
    loop = asyncio.get_event_loop()
    scheduler = loop.run_until_complete(aiojobs.create_scheduler())

    logger.info("Loading engine config file=%s...",
                config_files[0])  # type: ignore
    server_config = _load_engine_config(config_files[0])
    loop.run_until_complete(start_server(server_config))
    if server_config.auth.domain:
        auth_info_default['domain'] = server_config.auth.domain
    if api_file is not None:
        api.load_api_file(api_file)
        api.register_server_config(server_config)

    apps_config = []
    for config_file in config_files[1:]:
        logger.info(__name__, f"Loading app config file={config_file}...")
        config = _load_app_config(config_file)
        config.server = server_config
        apps_config.append(config)

    api.register_apps(apps_config)
    api.enable_swagger(server_config, web_server)
    for config in apps_config:
        loop.run_until_complete(start_app(config, scheduler, start_streams))

    logger.debug(__name__, "Performing forced garbage collection...")
    gc.collect()
    web.run_app(web_server, path=path, port=port, host=host)
Exemplo n.º 2
0
def _init_api(
        mock_api_app_config,
        mock_api_spec,
        plugin=None,  # noqa: F811
        init_server=False,
        init_apps=False,
        init_swagger=False,
        generate_mode=False):
    api.clear()
    if generate_mode:
        api.setup(generate_mode=True)
    api.init_empty_spec(api_version=mock_api_spec['info']['version'],
                        title=mock_api_spec['info']['title'],
                        description=mock_api_spec['info']['description'])
    server_config = mock_api_app_config.server
    if init_server:
        api.register_server_config(server_config)
    if init_apps:
        apps = [mock_api_app_config]
        if plugin:
            apps.append(plugin)
        api.register_apps(apps)
    if init_swagger:
        api.static_spec = deepcopy(api.spec)
        api.enable_swagger(mock_api_app_config.server, web.Application())
Exemplo n.º 3
0
async def test_enable_swagger_incompatible_api(
        mock_api_app_config, mock_api_spec, mock_plugin_config):  # noqa: F811
    _init_api(mock_api_app_config,
              mock_api_spec,
              plugin=mock_plugin_config,
              init_server=True,
              init_apps=True,
              init_swagger=False)

    with pytest.raises(APIError):
        api.static_spec['info']['title'] = 'Simulating API Change'
        api.enable_swagger(server_config=mock_api_app_config.server,
                           app=web.Application())
Exemplo n.º 4
0
def test_api_disabled(mock_api_app_config):  # noqa: F811
    api.clear()
    api.register_server_config(mock_api_app_config.server)
    assert api.spec is None
    apps = [mock_api_app_config]
    app = web.Application()
    api.register_apps(apps)
    api.enable_swagger(mock_api_app_config.server, app)
    assert api.spec is None
    route = app_route_name(mock_api_app_config.app,
                           event_name='mock-app-api-get')
    handler = api.add_route('get', route, _test_handler_get)
    assert handler is _test_handler_get
    route = app_route_name(mock_api_app_config.app,
                           event_name='mock-app-noapi')
    handler = api.add_route('get', route, _test_handler_get)
    assert handler is _test_handler_get
Exemplo n.º 5
0
def prepare_engine(*, config_files: List[str], api_file: Optional[str],
                   enabled_groups: List[str], start_streams: bool):
    """
    Load configuration files and add hooks to setup engine server and apps,
    start streams and services.
    """
    logger.info("Loading engine config file=%s...",
                config_files[0])  # type: ignore
    server_config: ServerConfig = _load_engine_config(config_files[0])

    # Add startup hook to start engine
    web_server.on_startup.append(partial(server_startup_hook, server_config))
    if server_config.auth.domain:
        auth_info_default['domain'] = server_config.auth.domain
    if api_file is not None:
        api.load_api_file(api_file)
        api.register_server_config(server_config)

    apps_config = []
    for config_file in config_files[1:]:
        logger.info(__name__, f"Loading app config file={config_file}...")
        config = _load_app_config(config_file)
        config.server = server_config
        apps_config.append(config)

    # Register and add startup hooks to start configured apps
    api.register_apps(apps_config)
    api.enable_swagger(server_config, web_server)
    for config in apps_config:
        web_server.on_startup.append(
            partial(app_startup_hook, config, enabled_groups))

    # Add hooks to start streams and service
    if start_streams:
        for config in apps_config:
            web_server.on_startup.append(partial(stream_startup_hook, config))

    logger.debug(__name__, "Performing forced garbage collection...")
    gc.collect()