示例#1
0
文件: api.py 项目: rsneh/bocadillo
    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)
示例#2
0
文件: main.py 项目: matthiask/uvicorn
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)
示例#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')
示例#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')
示例#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')
示例#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)