Пример #1
0
async def start_app(config: AppConfig,
                    scheduler: Scheduler,
                    start_streams: bool = False):
    """
    Start Hopeit app specified by config

    :param config: AppConfig, configuration for the app to start
    :param start_streams: if True all stream events in app will start consuming
    """
    app_engine = await runtime.server.start_app(app_config=config)
    cors_origin = aiohttp_cors.setup(web_server,
                                     defaults={
                                         config.engine.cors_origin:
                                         aiohttp_cors.ResourceOptions(
                                             allow_credentials=True,
                                             expose_headers="*",
                                             allow_headers="*",
                                         )
                                     }) if config.engine.cors_origin else None

    _setup_app_event_routes(app_engine)
    for plugin in config.plugins:
        plugin_engine = runtime.server.app_engine(app_key=plugin.app_key())
        _setup_app_event_routes(app_engine, plugin_engine)
    if cors_origin:
        app = app_engine.app_config.app
        _enable_cors(route_name('api', app.name, app.version), cors_origin)
    if start_streams:
        await _start_streams(app_engine, scheduler)
Пример #2
0
def app_route_name(app: AppDescriptor, *, event_name: str,
                   plugin: Optional[AppDescriptor] = None,
                   prefix: str = 'api', override_route_name: Optional[str] = None) -> str:
    """
    Returns the full route name for a given app event

    :param app: AppDescriptor, as defined in AppConfig
    :param event_name: event name as defined in AppConfig
    :param plugin: optional plugin if the event comes from a plugin and EventPlugMode=='OnApp'
    :param prefix: route prefix, defaults to 'api'
    :param override_route_name: Optional[str], provided route to be used instead app and event name,
        if starts with '/', prefix will be ignored, otherwised appended to prefix
    :return: str, full route name. i.e.:
        /api/app-name/1x0/event-name or /api/app-name/1x0/plugin-name/1x0/event-name
    """
    components = [
        prefix, app.name, app.version,
        *([plugin.name, plugin.version] if plugin else []),
        *event_name.split('.')
    ] if override_route_name is None else [
        override_route_name[1:]
    ] if override_route_name[0] == '/' else [
        prefix, override_route_name
    ]
    return route_name(*components)
Пример #3
0
async def app_startup_hook(config: AppConfig, enabled_groups: List[str], *args,
                           **kwargs):
    """
    Start Hopeit app specified by config

    :param config: AppConfig, configuration for the app to start
    :param enabled_groups: list of event groups names to enable. If empty,
        all events will be enabled.
    """
    app_engine = await runtime.server.start_app(app_config=config,
                                                enabled_groups=enabled_groups)
    cors_origin = aiohttp_cors.setup(web_server,
                                     defaults={
                                         config.engine.cors_origin:
                                         aiohttp_cors.ResourceOptions(
                                             allow_credentials=True,
                                             expose_headers="*",
                                             allow_headers="*",
                                         )
                                     }) if config.engine.cors_origin else None

    _setup_app_event_routes(app_engine)
    for plugin in config.plugins:
        plugin_engine = runtime.server.app_engine(app_key=plugin.app_key())
        _setup_app_event_routes(app_engine, plugin_engine)
    if cors_origin:
        app = app_engine.app_config.app
        _enable_cors(route_name('api', app.name, app.version), cors_origin)
Пример #4
0
def app_base_route_name(app: AppDescriptor, *, plugin: Optional[AppDescriptor] = None, prefix='api') -> str:
    """
    Returns base route name for paths in a given app
    """
    components = [
        prefix, app.name, app.version,
        *([plugin.name, plugin.version] if plugin else [])
    ]
    return route_name(*components)
Пример #5
0
async def __postprocess__(result: RuntimeAppsConfig, context: EventContext,
                          response: PostprocessHook) -> str:
    """
    Renders html from template, using cytospace data json
    """
    response.set_content_type("text/html")

    app_prefix = result.options.app_prefix

    view_link = f"apps-visualizer?app_prefix={result.options.app_prefix}"
    view_link += f"&host_filter={result.options.host_filter}"
    view_link += f"&expanded_view={str(not result.options.expanded_view).lower()}"
    view_link += f"&live={str(result.options.live).lower()}"

    live_link = f"apps-visualizer?app_prefix={result.options.app_prefix}"
    live_link += f"&host_filter={result.options.host_filter}"
    live_link += f"&expanded_view={str(result.options.expanded_view).lower()}"
    live_link += f"&live={str(not result.options.live).lower()}"

    app_prefix = f"{result.options.app_prefix}*" if result.options.app_prefix else 'All running apps'
    host_filter = f"*{result.options.host_filter}*" if result.options.host_filter else 'All servers'
    view_type = "Effective Events" if result.options.expanded_view else "Configured Events"
    live_type = "Live!" if result.options.live else "Static"

    refresh_endpoint_comps = (['event-stats', 'live'] if result.options.live
                              else ['apps', 'events-graph'])
    refresh_endpoint = route_name("api", context.app.name, context.app.version,
                                  *refresh_endpoint_comps)
    refresh_endpoint += f"?app_prefix={result.options.app_prefix}"
    refresh_endpoint += f"&host_filter={result.options.host_filter}"
    refresh_endpoint += f"&expanded_view={str(result.options.expanded_view).lower()}"
    refresh_endpoint += f"&live={str(result.options.live).lower()}"

    with open(_dir_path / 'events_graph_template.html') as f:
        template = f.read()
        template = template.replace("{{ app_prefix }}", app_prefix)
        template = template.replace("{{ host_filter }}", host_filter)
        template = template.replace("{{ view_link }}", view_link)
        template = template.replace("{{ live_link }}", live_link)
        template = template.replace("{{ refresh_endpoint }}", refresh_endpoint)
        template = template.replace("{{ view_type }}", view_type)
        template = template.replace("{{ live_type }}", live_type)
        return template
Пример #6
0
def test_route_name():
    assert route_name('api', 'app-name', '1.0') == '/api/app-name/1x0'
    assert route_name('api', 'app_name', '1.0') == '/api/app-name/1x0'
    assert route_name('api', 'app_name', '1x0') == '/api/app-name/1x0'
    assert route_name('api', 'app-name', '1.0', 'plugin-name', '2.0') == \
        '/api/app-name/1x0/plugin-name/2x0'