Beispiel #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)
Beispiel #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)
Beispiel #3
0
def development(*args, **kwargs):
    logging.config.dictConfig({
        "version": 1,
        "disable_existing_loggers": False,
        "formatters": {
            "standard": {
                "format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s"
            }
        },
        "handlers": {
            "default": {
                "level": "DEBUG",
                "formatter": "standard",
                "class": "logging.StreamHandler"
            }
        },
        "loggers": {
            "goobox_nodes": {
                "handlers": ["default"],
                "level": "DEBUG",
                "propagate": False
            },
            "nodes": {
                "handlers": ["default"],
                "level": "DEBUG",
                "propagate": False
            },
        },
    })

    subprocess.run(shlex.split("alembic upgrade head"))

    StatReload(get_logger("debug")).run(
        uvicorn.run,
        {
            "app": os.environ["STARLETTE_APP"],
            "host": os.environ["APP_HOST"],
            "port": int(os.environ["APP_PORT"]),
            "debug": True,
        },
    )
Beispiel #4
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')
Beispiel #5
0
     HOST, PORT, app, use_debugger=DEBUG, use_reloader=RELOAD),
 'quart':
 lambda app: app.run(HOST,
                     PORT,
                     debug=DEBUG,
                     use_reloader=RELOAD,
                     loop=asyncio.get_event_loop()),
 'responder':
 lambda app: app.run(address=HOST, port=PORT, debug=DEBUG),
 'sanic':
 lambda app: app.run(HOST, PORT, debug=DEBUG, auto_reload=RELOAD),
 'starlette':
 lambda app: StatReload(logging.getLogger(__name__)).run(
     uvicorn.run, {
         'app': app,
         'host': HOST,
         'port': PORT,
         'debug': DEBUG
     }) if RELOAD else uvicorn.run(app, HOST, PORT, debug=DEBUG),
 'tornado':
 lambda app: HTTPServer(app).listen(PORT, HOST),
 'turbogears':
 lambda app: run_simple(
     HOST, PORT, app, use_debugger=DEBUG, use_reloader=RELOAD),
 'twisted':
 lambda app: endpoints.serverFromString(
     reactor, f'tcp:{PORT}:interface={HOST}').listen(app),
 'uvicorn':
 lambda app: StatReload(logging.getLogger(__name__)).run(
     uvicorn.run, {
         'app': app,
Beispiel #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)