Ejemplo n.º 1
0
    def run(
        self,
        host: str = None,
        port: int = None,
        debug: bool = False,
        log_level: str = "info",
        _run: Callable = run,
    ):
        """Serve the application using [uvicorn](https://www.uvicorn.org).

        For further details, refer to
        [uvicorn settings](https://www.uvicorn.org/settings/).

        # Parameters

        host (str):
            The host to bind to.
            Defaults to `"127.0.0.1"` (localhost).
            If not given and `$PORT` is set, `"0.0.0.0"` will be used to
            serve to all known hosts.
        port (int):
            The port to bind to.
            Defaults to `8000` or (if set) the value of the `$PORT` environment
            variable.
        debug (bool):
            Whether to serve the application in debug mode. Defaults to `False`.
        log_level (str):
            A logging level for the debug logger. Must be a logging level
            from the `logging` module. Defaults to `"info"`.
        """
        if "PORT" in os.environ:
            port = int(os.environ["PORT"])
            if host is None:
                host = "0.0.0.0"

        if host is None:
            host = "127.0.0.1"

        if port is None:
            port = 8000

        if debug:
            reloader = StatReload(get_logger(log_level))
            reloader.run(
                run,
                {
                    "app": self,
                    "host": host,
                    "port": port,
                    "log_level": log_level,
                    "debug": debug,
                },
            )
        else:
            _run(self, host=host, port=port)
Ejemplo n.º 2
0
def main(
    app,
    host: str,
    port: int,
    uds: str,
    fd: int,
    loop: str,
    http: str,
    ws: str,
    wsgi: bool,
    debug: bool,
    log_level: str,
    no_access_log: bool,
    proxy_headers: bool,
    root_path: str,
    limit_concurrency: int,
    limit_max_requests: int,
    timeout_keep_alive: int,
    disable_lifespan: bool,
):
    sys.path.insert(0, ".")

    kwargs = {
        "app": app,
        "host": host,
        "port": port,
        "uds": uds,
        "fd": fd,
        "loop": loop,
        "http": http,
        "ws": ws,
        "log_level": log_level,
        "access_log": not no_access_log,
        "wsgi": wsgi,
        "debug": debug,
        "proxy_headers": proxy_headers,
        "root_path": root_path,
        "limit_concurrency": limit_concurrency,
        "limit_max_requests": limit_max_requests,
        "timeout_keep_alive": timeout_keep_alive,
        "disable_lifespan": disable_lifespan,
    }

    if debug:
        logger = get_logger(log_level)
        reloader = StatReload(logger)
        reloader.run(run, kwargs)
    else:
        run(**kwargs)
Ejemplo n.º 3
0
    try:
        day = request.query_params['day']  # eg. 20181209
    except KeyError:
        day = eightDigits()

    # some backend rendering
    props = PropsFactory(chart, day).props
    props['day'] = day

    template = app.get_template('index.html')
    content = template.render(
        request=request,
        props=props,
    )
    return HTMLResponse(content)


if __name__ == '__main__':
    reloader = StatReload(get_logger('debug'))
    reloader.run(
        run, {
            'app': app,
            'host': '0.0.0.0',
            'port': FRONTEND_PORT,
            'log_level': 'debug',
            'debug': 'true'
        })

    uvicorn.run(app, host='0.0.0.0', port=FRONTEND_PORT, debug='true')
Ejemplo n.º 4
0
    try:
        day = request.query_params['day']
    except KeyError:
        day = eightDigits()

    try:
        chart = request.query_params['chart']
    except KeyError:
        chart = 'rise'

    m = MongoDBPipeline()
    query = f'{day}.{chart}'
    result = m.ls(query)
    for item in result:
        item.pop('_id')

    return JSONResponse(result)


if __name__ == '__main__':
    reloader = StatReload(get_logger('debug'))
    reloader.run(
        run, {
            'app': app,
            'host': '127.0.0.1',
            'port': BACKEND_PORT,
            'log_level': 'debug',
            'debug': 'true'
        })
    uvicorn.run(app=app, host='127.0.0.1', port=BACKEND_PORT, debug='true')
Ejemplo n.º 5
0
                    if result:
                        message = f'successfully run {_sh}'
                    else:
                        message = f'failed run {_sh}'
            except Exception as e:
                message = e
        else:
            message = await self.echo(payload)
        resp = {'done': result, 'message': message}
        return JSONResponse(resp)

    async def echo(self, payload):
        return f"got `{self.event}` from {payload['repository']['full_name']}"


if __name__ == '__main__':
    reloader = StatReload(get_logger('debug'))
    reloader.run(
        run, {
            'app': app,
            'host': '0.0.0.0',
            'port': 50001,
            'log_level': 'debug',
            'debug': 'true'
        })
    uvicorn.run(app=app,
                host='0.0.0.0',
                port=50001,
                log_level='debug',
                debug='true')
Ejemplo n.º 6
0
    def run(
        self,
        host: str = None,
        port: int = None,
        debug: bool = False,
        log_level: str = "info",
        _run: Callable = None,
        **kwargs,
    ):
        """Serve the application using [uvicorn](https://www.uvicorn.org).

        # Parameters

        host (str):
            The host to bind to.
            Defaults to `"127.0.0.1"` (localhost).
            If not given and `$PORT` is set, `"0.0.0.0"` will be used to
            serve to all known hosts.
        port (int):
            The port to bind to.
            Defaults to `8000` or (if set) the value of the `$PORT` environment
            variable.
        debug (bool):
            Whether to serve the application in debug mode. Defaults to `False`.
        log_level (str):
            A logging level for the debug logger. Must be a logging level
            from the `logging` module. Defaults to `"info"`.
        kwargs (dict):
            Extra keyword arguments that will be passed to the Uvicorn runner.

        # See Also
        - [Configuring host and port](../guides/api.md#configuring-host-and-port)
        - [Debug mode](../guides/api.md#debug-mode)
        - [Uvicorn settings](https://www.uvicorn.org/settings/) for all
        available keyword arguments.
        """
        if _run is None:  # pragma: no cover
            _run = run

        if "PORT" in os.environ:
            port = int(os.environ["PORT"])
            if host is None:
                host = "0.0.0.0"

        if host is None:
            host = "127.0.0.1"

        if port is None:
            port = 8000

        if debug:
            self.debug = True
            reloader = StatReload(get_logger(log_level))
            reloader.run(
                run,
                {
                    "app": self,
                    "host": host,
                    "port": port,
                    "log_level": log_level,
                    "debug": self.debug,
                    **kwargs,
                },
            )
        else:
            _run(self, host=host, port=port, **kwargs)