Esempio n. 1
0
async def test_access_logging(use_colors, caplog):
    config = Config(app=app, use_colors=use_colors)
    with caplog_for_logger(caplog, "uvicorn.access"):
        async with run_server(config):
            async with httpx.AsyncClient() as client:
                response = await client.get("http://127.0.0.1:8000")

        assert response.status_code == 204
        messages = [
            record.message
            for record in caplog.records
            if record.name == "uvicorn.access"
        ]
        assert '"GET / HTTP/1.1" 204' in messages.pop()
Esempio n. 2
0
async def test_trace_logging_on_http_protocol(http_protocol, caplog):
    config = Config(app=app, log_level="trace", http=http_protocol)
    with caplog_for_logger(caplog, "uvicorn.error"):
        async with run_server(config):
            async with httpx.AsyncClient() as client:
                response = await client.get("http://127.0.0.1:8000")
        assert response.status_code == 204
        messages = [
            record.message
            for record in caplog.records
            if record.name == "uvicorn.error"
        ]
        assert any(" - HTTP connection made" in message for message in messages)
        assert any(" - HTTP connection lost" in message for message in messages)
Esempio n. 3
0
    async def async_setup(self):
        """
        The async method setup the runtime.

        Setup the uvicorn server.
        """
        with ImportExtensions(required=True):
            from uvicorn import Config, Server

        class UviServer(Server):
            """The uvicorn server."""

            async def setup(self, sockets=None):
                """
                Setup uvicorn server.

                :param sockets: sockets of server.
                """
                config = self.config
                if not config.loaded:
                    config.load()
                self.lifespan = config.lifespan_class(config)
                self.install_signal_handlers()
                await self.startup(sockets=sockets)
                if self.should_exit:
                    return

            async def serve(self, **kwargs):
                """
                Start the server.

                :param kwargs: keyword arguments
                """
                await self.main_loop()

        from .....helper import extend_rest_interface

        uvicorn_kwargs = self.args.uvicorn_kwargs or {}
        self._server = UviServer(
            config=Config(
                app=extend_rest_interface(get_fastapi_app(self.args, self.logger)),
                host=__default_host__,
                port=self.args.port_expose,
                ws_max_size=1024 * 1024 * 1024,
                log_level=os.getenv('JINA_LOG_LEVEL', 'error').lower(),
                **uvicorn_kwargs
            )
        )
        await self._server.setup()
Esempio n. 4
0
async def test_access_logging(capsys):
    config = Config(
        app=app,
        loop="asyncio",
        limit_max_requests=1,
        log_config=test_logging_config,
    )
    async with run_server(config):
        async with httpx.AsyncClient() as client:
            response = await client.get("http://127.0.0.1:8000")

    assert response.status_code == 204
    captured = capsys.readouterr()
    assert '"GET / HTTP/1.1" 204' in captured.out
    assert "uvicorn.access" in captured.out
Esempio n. 5
0
async def test_trace_logging(capsys):
    config = Config(
        app=app,
        loop="asyncio",
        limit_max_requests=1,
        log_config=test_logging_config,
        log_level="trace",
    )
    async with run_server(config):
        async with httpx.AsyncClient() as client:
            response = await client.get("http://127.0.0.1:8000")
    assert response.status_code == 204
    captured = capsys.readouterr()
    assert '"GET / HTTP/1.1" 204' in captured.out
    assert "[TEST_ACCESS] TRACE" not in captured.out
Esempio n. 6
0
def setup_app_and_run(directory, config_path, runner, tool_override):
    uvicorn_config = Config(app, host="127.0.0.1", port=8889, log_level="info")
    uvicorn_config.setup_event_loop()
    server = Server(uvicorn_config)

    app.loop = asyncio.get_event_loop()
    # EM
    app.em = EventEmitter(app.loop)

    config = read_toml_config(config_path, tool_override)

    # TODO: Re-add support for multiple test suites
    app.suite = TestSuite(config["name"], runner, app.em, config)
    app.suites: Dict[str, TestSuite] = {config["name"]: app.suite}

    # Tests
    app.tests = Tests(app.suites)

    app.ws_client = WebsocketClients()
    app.directory = directory

    async def forward_notifications(message):
        LOGGER.debug("Forwarding to %d clients: %r",
                     len(app.ws_client.clients), message)
        data = {
            "jsonrpc": "2.0",
            "id": None,
            "method": "test",
            "params": message
        }
        await app.ws_client.broadcast(json.dumps(data))

    app.em.register(forward_notifications)
    app.em.register(process_notification)

    app.loop.run_until_complete(server.serve())
Esempio n. 7
0
def create(loop, config, *, mqtt=None, debug=False):
  global mqtt_client
  mqtt_client = mqtt

  if debug:
    log_level = logging.DEBUG
  else:
    log_level = logging.INFO

  servers = []
  for listener in config['listeners']:
    webapp_config = Config(app=app.app, loop=loop, log_config=None, log_level=log_level, debug=debug, **config['listeners'][listener])
    server = SignalableServer(webapp_config)
    servers.append(server)
  return servers
Esempio n. 8
0
def run(app, **kwargs):
    # Config.configure_logging = functools.partial(override_configure_logging, Config) #overwrite uvicorn method
    # kwargs["log_config"] = LOGGING_CONFIG #! overwrite
    config = Config(app, **kwargs)
    config.backlog = 2048  #! for some reason we need to specify this
    # config.log_config = LOGGING_CONFIG  #! overwrite write our own
    server = Server(config=config)

    if (config.reload or config.workers > 1) and not isinstance(app, str):
        logger.warn(
            "You must pass the application as an import string to enable 'reload' or 'workers'."
        )
        sys.exit(1)

    if config.should_reload:
        sock = config.bind_socket()
        supervisor = StatReload(config, target=server.run, sockets=[sock])
        supervisor.run()
    elif config.workers > 1:
        sock = config.bind_socket()
        supervisor = Multiprocess(config, target=server.run, sockets=[sock])
        supervisor.run()
    else:
        server.run()
Esempio n. 9
0
async def test_trace_logging(caplog):
    config = Config(app=app, log_level="trace")
    with caplog_for_logger(caplog, "uvicorn.asgi"):
        async with run_server(config):
            async with httpx.AsyncClient() as client:
                response = await client.get("http://127.0.0.1:8000")
        assert response.status_code == 204
        messages = [
            record.message for record in caplog.records if record.name == "uvicorn.asgi"
        ]
        assert "ASGI [1] Started scope=" in messages.pop(0)
        assert "ASGI [1] Raised exception" in messages.pop(0)
        assert "ASGI [2] Started scope=" in messages.pop(0)
        assert "ASGI [2] Send " in messages.pop(0)
        assert "ASGI [2] Send " in messages.pop(0)
        assert "ASGI [2] Completed" in messages.pop(0)
Esempio n. 10
0
    async def run_app(self):
        """
        Run app with the given parameters to init.
        Not supposed to be used manually by user.

        :return: None
        """

        config = Config(app=self.app,
                        host=self._host,
                        port=self._port,
                        debug=self._debug,
                        ssl_certfile=self._ssl_context[0],
                        ssl_keyfile=self._ssl_context[1])
        server = Server(config)
        await server.serve()
        await self._bot.close_session()
Esempio n. 11
0
async def test_default_logging(use_colors, caplog):
    config = Config(app=app, use_colors=use_colors)
    with caplog_for_logger(caplog, "uvicorn.access"):
        async with run_server(config):
            async with httpx.AsyncClient() as client:
                response = await client.get("http://127.0.0.1:8000")
        assert response.status_code == 204
        messages = [
            record.message for record in caplog.records if "uvicorn" in record.name
        ]
        assert "Started server process" in messages.pop(0)
        assert "Waiting for application startup" in messages.pop(0)
        assert "ASGI 'lifespan' protocol appears unsupported" in messages.pop(0)
        assert "Application startup complete" in messages.pop(0)
        assert "Uvicorn running on http://127.0.0.1:8000" in messages.pop(0)
        assert '"GET / HTTP/1.1" 204' in messages.pop(0)
        assert "Shutting down" in messages.pop(0)
Esempio n. 12
0
def test_override_server_header_multiple_times():
    config = Config(
        app=app,
        loop="asyncio",
        limit_max_requests=1,
        headers=[("Server", "over-ridden"), ("Server", "another-value")],
    )
    server = Server(config=config)
    thread = threading.Thread(target=server.run)
    thread.start()
    while not server.started:
        time.sleep(0.01)
    response = requests.get("http://127.0.0.1:8000")

    assert (response.headers["server"] == "over-ridden, another-value"
            and response.headers["date"])

    thread.join()
Esempio n. 13
0
def test_trace_logging(capsys):
    config = Config(
        app=app,
        loop="asyncio",
        limit_max_requests=1,
        log_config=test_logging_config,
        log_level="trace",
    )
    server = Server(config=config)
    thread = threading.Thread(target=server.run)
    thread.start()
    while not server.started:
        time.sleep(0.01)
    response = requests.get("http://127.0.0.1:8000")
    assert response.status_code == 204
    thread.join()
    captured = capsys.readouterr()
    assert '"GET / HTTP/1.1" 204' in captured.out
    assert "[TEST_ACCESS] TRACE" not in captured.out
Esempio n. 14
0
def test_access_logging(capsys, http_protocol):
    config = Config(
        app=app,
        loop="asyncio",
        http=http_protocol,
        limit_max_requests=1,
        log_config=test_logging_config,
    )
    server = Server(config=config)
    thread = threading.Thread(target=server.run)
    thread.start()
    while not server.started:
        time.sleep(0.01)
    response = requests.get("http://127.0.0.1:8000")
    assert response.status_code == 204
    thread.join()
    captured = capsys.readouterr()
    assert '"GET / HTTP/1.1" 204' in captured.out
    assert "uvicorn.access" in captured.out
Esempio n. 15
0
def test_add_additional_header():
    config = Config(
        app=app,
        loop="asyncio",
        limit_max_requests=1,
        headers=[("X-Additional", "new-value")],
    )
    server = Server(config=config)
    thread = threading.Thread(target=server.run)
    thread.start()
    while not server.started:
        time.sleep(0.01)
    response = requests.get("http://127.0.0.1:8000")

    assert (response.headers["x-additional"] == "new-value"
            and response.headers["server"] == "uvicorn"
            and response.headers["date"])

    thread.join()
Esempio n. 16
0
async def test_unknown_status_code(caplog):
    async def app(scope, receive, send):
        assert scope["type"] == "http"
        await send({"type": "http.response.start", "status": 599, "headers": []})
        await send({"type": "http.response.body", "body": b"", "more_body": False})

    config = Config(app=app)
    with caplog_for_logger(caplog, "uvicorn.access"):
        async with run_server(config):
            async with httpx.AsyncClient() as client:
                response = await client.get("http://127.0.0.1:8000")

        assert response.status_code == 599
        messages = [
            record.message
            for record in caplog.records
            if record.name == "uvicorn.access"
        ]
        assert '"GET / HTTP/1.1" 599' in messages.pop()
Esempio n. 17
0
File: __init__.py Progetto: yk/jina
    async def serve_forever(self, is_ready_event: 'Event'):
        with ImportExtensions(required=True):
            from uvicorn import Config, Server

        class UvicornCustomServer(Server):
            def run(self, sockets=None):
                # uvicorn only supports predefined event loops
                # hence we implement a way to serve from a custom (already running) loop
                asyncio.create_task(self.serve(sockets=sockets))

        # change log_level for REST server debugging
        config = Config(app=self.get_fastapi_app(),
                        host=self.args.host,
                        port=self.args.port_expose,
                        log_level='critical')
        self._server = UvicornCustomServer(config=config)
        self.logger.success(f'{self.__class__.__name__} is listening at: {self.args.host}:{self.args.port_expose}')
        self._server.run()
        is_ready_event.set()
Esempio n. 18
0
def main():
    import blockexp

    directory = os.path.dirname(os.path.dirname(__file__))

    reloader = StatReload(Config(
        app=None,
        debug=True,
        reload_dirs=[directory],
    ))

    logger: Logger = reloader.config.logger_instance

    while True:
        uvicorn.run(
            f"{__name__}:app",
            host='0.0.0.0',
            port=8000,
            debug=True and RELOAD,
            reload=RELOAD,
            reload_dirs=[directory],
        )

        if not RELOAD:
            break

        logger.error("FAILURE RELOAD")

        while True:
            time.sleep(0.3)
            if reloader.should_restart():
                reloader.clear()

                # noinspection PyBroadException
                try:
                    reload(blockexp)
                    init_app(debug=True)
                except Exception:
                    traceback.print_exc()
                    logger.error("FAILURE RELOAD")
                else:
                    break
Esempio n. 19
0
File: rest.py Progetto: lsgrep/jina
    async def start(self):
        with ImportExtensions(required=True):
            from uvicorn import Config, Server

        self.logger.warning('you are using a REST gateway, which is still in early beta version. '
                            'advanced features such as prefetch and streaming are disabled.')

        class UvicornCustomServer(Server):
            # uvicorn only supports predefined event loops
            # hence we implement a way to serve from a custom (already running) loop
            def run(self, sockets=None):
                return asyncio.get_event_loop().create_task(self.serve(sockets=sockets))

        # change log_level for REST server debugging
        self._config = Config(app=self.app, host=self.host, port=self.port_expose, log_level='critical')
        self._server = UvicornCustomServer(config=self._config)
        self.logger.success(f'gateway (REST) is listening at: {self.host}:{self.port_expose}')
        self._server.run()
        await self.is_gateway_ready.wait()
        return self
Esempio n. 20
0
def run_receiver(
    app: FastAPI,
    event_loop: asyncio.AbstractEventLoop,
    handlers: List[Callable[[signal.Signals, Optional[FrameType]], None]],
) -> Server:
    """
    NOTE: Uvicorn override signal handler for eventloop in the case of "ctrl-c" and "kill pid".
    So, tasks in eventloop other than "Uvicorn" cannot exit if "ctrl-c" and "kill pid".
    To solve it, inject signal handler for other tasks.
    """
    class SubServer(Server):  # type: ignore
        def handle_exit(self, sig: signal.Signals,
                        frame: Optional[FrameType]) -> None:
            super().handle_exit(sig, frame)
            for handler in handlers:
                handler(sig, frame)

    config = Config(app=app, loop=event_loop, log_level=logging.ERROR)

    server = SubServer(config)
    return server
Esempio n. 21
0
def test_trace_logging(capsys):
    class App:
        def __init__(self, scope):
            if scope["type"] != "http":
                raise Exception()

        async def __call__(self, receive, send):
            await send({
                "type": "http.response.start",
                "status": 204,
                "headers": []
            })
            await send({
                "type": "http.response.body",
                "body": b"",
                "more_body": False
            })

    class CustomServer(Server):
        def install_signal_handlers(self):
            pass

    config = Config(
        app=App,
        loop="asyncio",
        limit_max_requests=1,
        log_config=test_logging_config,
        log_level="trace",
    )
    server = CustomServer(config=config)
    thread = threading.Thread(target=server.run)
    thread.start()
    while not server.started:
        time.sleep(0.01)
    response = requests.get("http://127.0.0.1:8000")
    assert response.status_code == 204
    thread.join()
    captured = capsys.readouterr()
    assert '"GET / HTTP/1.1" 204' in captured.out
    assert "[TEST_ACCESS] TRACE" not in captured.out
Esempio n. 22
0
def runserver(host: str, port: str, debug: bool, initdb: bool, resetdb: bool,
              verbose: bool):
    """
    Run the FastAPI Server.

    :param host:        Host to run it on.
    :param port:        Port to run it on.
    :param debug:       Run server in debug mode?
    :param initdb:      Create models before running API?
    :param verbose:     Set logging to DEBUG instead of INFO
    """
    config.set_debug(debug)

    if verbose:
        logging.basicConfig(level=logging.DEBUG)

    if not run_async(
            prepare_postgres(retries=6,
                             interval=10.0,
                             db_uri=config.postgres_uri(),
                             loop=loop)):
        exit(1)  # Connecting to our postgres server failed.

    server_config = Config("api.app:app", host=host, port=port, debug=debug)
    server = Server(config=server_config)

    async def worker():
        if initdb:
            await safe_create_tables(verbose=verbose)
        elif resetdb:
            await delete_tables(verbose=verbose)
            await safe_create_tables(verbose=verbose)

        await server.serve()

    run_async(worker())
Esempio n. 23
0
 def __init__(self, _mock: Mock):
     self._id = _mock.id
     self._mock = _mock
     _headers = [('server', 'swavan')]
     _config = Config(app=SwaVanHttp(_mock).app,
                      host="localhost",
                      headers=_headers,
                      port=int(_mock.port),
                      access_log=False)
     if _mock.enable_https:
         _key = full_path("data/__certs__/swavan.key")
         _crt = full_path("data/__certs__/swavan.crt")
         if _mock.use_default_cert:
             if os.path.isfile(_key) and os.path.isfile(_crt):
                 _config.ssl_keyfile = _key
                 _config.ssl_certfile = _crt
                 SwaVanLogRecorder.send_log(f"Using default cert")
         else:
             if os.path.isfile(_mock.ssl_key_file_url) and os.path.isfile(
                     _mock.ssl_cert_file_url):
                 _config.ssl_keyfile = _mock.ssl_key_file_url
                 _config.ssl_certfile = _mock.ssl_cert_file_url
                 SwaVanLogRecorder.send_log(f"Using custom cert")
     self._core_server = Server(config=_config)
Esempio n. 24
0
        return plan.as_dict()
    else:
        raise HTTPException(status_code=404, detail='no such plan')


@app.put("/plans/")
async def add_plan(routine: Routine,
                   region: int,
                   first_uik: int,
                   last_uik: int,
                   hour_start: int = 1,
                   hour_end: int = 2):
    """ 
    Добавить план. 
    """
    id = max(state.plans or [0]) + 1
    state.plans[id] = dict(id=id,
                           routine=str(routine),
                           finished=False,
                           region=region,
                           first_uik=first_uik,
                           last_uik=last_uik,
                           hour_start=hour_start,
                           hour_end=hour_end)
    queue.put_nowait(state.plans[id])
    return state.plans[id].as_dict()


server = Server(Config(app))
server.install_signal_handlers = lambda *a: None  # Do not catch signals
Esempio n. 25
0
def web_app(loop):
    config = Config(app=app, loop=loop, port=8001, host='0.0.0.0')
    server = Server(config)
    return server
Esempio n. 26
0

app = VersionedFastAPI(app, version="1.0.0", prefix_format="/v{major}.{minor}", enable_latest=True)


@app.get("/")
async def read_items() -> Any:
    html_content = """
    <html>
        <head>
            <title>NMEA Injector</title>
        </head>
    </html>
    """
    return HTMLResponse(content=html_content, status_code=200)


if __name__ == "__main__":
    loop = asyncio.new_event_loop()

    # # Running uvicorn with log disabled so loguru can handle it
    config = Config(app=app, loop=loop, host="0.0.0.0", port=2748, log_config=None)
    server = Server(config)

    loop.create_task(controller.load_socks_from_settings())
    if args.udp:
        loop.create_task(controller.add_sock(NMEASocket(kind=SocketKind.UDP, port=args.udp, component_id=220)))
    if args.tcp:
        loop.create_task(controller.add_sock(NMEASocket(kind=SocketKind.TCP, port=args.tcp, component_id=221)))
    loop.run_until_complete(server.serve())
Esempio n. 27
0
    async def run(self):
        app = "bountydns.api.main:api"
        kwargs = self.get_kwargs()
        env = self.option("env")
        self.load_env(f"api.{env}")

        if self.should_import_check():
            logger.info("performing import check")
            from bountydns.api.main import api

        logger.critical("starting api server with options: {}".format(
            str(kwargs)))
        from bountydns.db.checks import is_db_up, is_db_setup

        if self.should_db_check():
            self.db_register()
            db_up = is_db_up()
            if not db_up:
                logger.critical("database not up error. please check logs")
                return self.exit(1)

        if self.option("db_setup"):
            logger.critical("running database migration")
            db_setup_options = self._args_to_dict(self.options)
            if self.option("db_seed"):
                db_setup_options["seed"] = True
            await DbSetup(db_setup_options).run()

        if self.should_db_check():
            db_setup = is_db_setup()
            if not db_setup:
                logger.critical("database not setup error. please check logs")
                return self.exit(1)

        from bountydns.broadcast import is_broadcast_up

        if self.should_bcast_check():
            bcast_up = await is_broadcast_up()
            if not bcast_up:
                logger.critical(
                    "broadcast (queue) not up error. please check logs")
                return self.exit(1)

        if self.option("db_seed_env", False):
            self.seed_from_env()

        # taken from uvicorn/main.py:run
        config = UvicornConfig(app, **kwargs)
        server = UvicornServer(config=config)

        if isinstance(app, str) and (config.debug or config.reload):
            sock = config.bind_socket()
            supervisor = StatReload(config)
            logger.warning(f"running bountydns api in dev mode...")
            return supervisor.run(server.run, sockets=[sock])
        elif config.workers > 1:
            sock = config.bind_socket()
            supervisor = Multiprocess(config)
            logger.warning(f"running bountydns api in worker mode...")
            return supervisor.run(server.run, sockets=[sock])
        else:
            sockets = None
            logger.warning(f"running bountydns api in standard mode...")
            return await server.serve(sockets=sockets)
Esempio n. 28
0
def version():
    """Route for retrieving server version."""
    return Response(status_code=200, content=MDAI_DEPLOY_API_VERSION)


@app.get("/has-testing-metrics")
def has_testing_metrics():
    """Route for metric evaluation check."""
    headers = {"Content-Type": "application/msgpack"}
    data = {"hasTestingMetrics": False}
    if hasattr(mdai_model, "evaluate_on_batch") and callable(mdai_model.evaluate_on_batch):
        data["hasTestingMetrics"] = True
    return Response(
        status_code=200, content=msgpack.packb(data, use_bin_type=True), headers=headers
    )


if __name__ == "__main__":

    from mdai_deploy import MDAIModel

    mdai_model = MDAIModel()
    mdai_model_ready = True

    loop = asyncio.new_event_loop()

    config = Config(app=app, host="0.0.0.0", port="6324", workers=1)
    server = Server(config)

    loop.run_until_complete(server.serve())
Esempio n. 29
0
def main():
    config = Config(app=app, host='0.0.0.0', port=int(PORT), debug=True)
    server = Server(config=config)
    server.run()
Esempio n. 30
0

def setup_logging():
    # intercept everything at the root logger
    logging.root.handlers = [InterceptHandler()]
    logging.root.setLevel(LOG_LEVEL)

    # remove every other logger's handlers
    # and propagate to root logger
    for name in logging.root.manager.loggerDict.keys():
        logging.getLogger(name).handlers = []
        logging.getLogger(name).propagate = True

    # configure loguru
    logger.configure(handlers=[{"sink": sys.stdout, "serialize": JSON_LOGS}])


if __name__ == '__main__':
    server = Server(
        Config(
            "tdb.camerasuite.app:app",
            host="0.0.0.0",
            log_level=LOG_LEVEL,
        ), )

    # setup logging last, to make sure no library overwrites it
    # (they shouldn't, but it happens)
    setup_logging()

    server.run()