Esempio n. 1
0
async def app(event_loop, db, request):
    globalregistry.reset()
    settings = get_db_settings(request.node)
    app = make_app(settings=settings, loop=event_loop)

    server_settings = settings.get("test_server_settings", {})
    host = server_settings.get("host", "127.0.0.1")
    port = int(server_settings.get("port", 8000))

    from uvicorn import Config, Server

    config = Config(app, host=host, port=port, lifespan="on")
    server = Server(config=config)
    task = asyncio.ensure_future(server.serve(), loop=event_loop)

    while app.app is None and not task.done():
        # Wait for app initialization
        await asyncio.sleep(0.05)

    if task.done():
        task.result()

    await _clear_dbs(app.app.root)

    yield host, port

    server.should_exit = True
    await asyncio.sleep(1)  # There is no other way to wait for server shutdown
    clear_task_vars()
Esempio n. 2
0
    def server(self, loop):
        """
            prepare the server and initialize it
            so it be ready to run inside a thread

            this code is inspired by the following link:
                - https://github.com/encode/uvicorn/issues/706#issuecomment-652220153
        """

        app = FastAPI()
        app.x = self
        app.redis = redis.Redis(
            host=self.redis_config["host"],
            port=self.redis_config["port"],
            password=self.redis_config["password"],
            db=self.redis_config["db"]
        )

        app.include_router(routes.router)

        config = Config(
            app=app,
            loop=loop,
            workers=self.server_workers_count,
            port=self.server_listen_port,
            host=self.server_listen_host,
            access_log=self.enable_access_log,
            debug=False
        )

        server = Server(config)

        loop.run_until_complete(server.serve())
Esempio n. 3
0
 async def create_server(config):
     server = Server(config)
     task = asyncio.ensure_future(server.serve())
     try:
         yield task
     finally:
         await server.shutdown()
         task.cancel()
Esempio n. 4
0
def run_api(bot):
    from uvicorn import Server, Config
    web_cog = bot.get_cog("web")
    if web_cog is None:
        return
    server = Server(Config(web_cog.app, host="0.0.0.0", port=1234))
    server.config.setup_event_loop()
    return bot.loop.create_task(server.serve())
Esempio n. 5
0
async def do_tests():
    # Create dummy files
    os.makedirs("tests")
    os.makedirs("tests/client")
    os.makedirs("tests/server")
    os.makedirs("tests/server/empty_dir")
    os.makedirs("tests/server/sub1")
    os.makedirs("tests/server/sub1/sub2")
    for filename in [
            "tests/server/f1",
            "tests/server/f2",
            "tests/server/sub1/f3",
            "tests/server/sub1/sub2/f4",
    ]:
        async with aiofiles.open(filename, mode='wb') as f:
            await f.write(os.urandom(10240))

    # Launch server
    http_mv_server.files_path = os.path.join(os.getcwd(), "tests/server")
    http_mv_server.files_pattern = "**"
    server = Server(
        config=Config(http_mv_server.app, host='127.0.0.1', port=8000))
    loop = asyncio.get_event_loop()
    loop.create_task(server.serve(sockets=None))
    await asyncio.sleep(2)  # Create server

    # Launch client
    await http_mv_client("http://127.0.0.1:8000", "tests/client")

    # Check
    files_local = glob.glob("tests/**", recursive=True)
    files_supposed = [
        "tests/client/f1",
        "tests/client/f2",
        "tests/client/sub1/f3",
        "tests/client/sub1/sub2/f4",
        "tests\\client\\f1",
        "tests\\client\\f2",
        "tests\\client\\sub1\\f3",
        "tests\\client\\sub1\\sub2\\f4",
    ]
    for file in files_local:
        if file in files_supposed:
            os.remove(file)
    os.rmdir("tests/client/sub1/sub2")
    os.rmdir("tests/client/sub1")
    os.rmdir("tests/client/")

    # Exit server
    server.handle_exit(None, None)
    await asyncio.sleep(2)  # Gracefully

    # Clean
    os.rmdir("tests/server/sub1/sub2")
    os.rmdir("tests/server/sub1")
    os.rmdir("tests/server/empty_dir")
    os.rmdir("tests/server")
    os.rmdir("tests")
Esempio n. 6
0
async def run_server(config: Config, sockets=None):
    server = Server(config=config)
    cancel_handle = asyncio.ensure_future(server.serve(sockets=sockets))
    await asyncio.sleep(0.1)
    try:
        yield server
    finally:
        await server.shutdown()
        cancel_handle.cancel()
Esempio n. 7
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. 8
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. 9
0
    </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)

    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.create_task(server.serve())
    loop.run_forever()
Esempio n. 10
0
def run_server(
    context: Context = None,
    client: dask.distributed.Client = None,
    host: str = "0.0.0.0",
    port: int = 8080,
    startup=False,
    log_level=None,
    blocking: bool = True,
    jdbc_metadata: bool = False,
):  # pragma: no cover
    """
    Run a HTTP server for answering SQL queries using ``dask-sql``.
    It uses the `Presto Wire Protocol <https://github.com/prestodb/presto/wiki/HTTP-Protocol>`_
    for communication.
    This means, it has a single POST endpoint `/v1/statement`, which answers
    SQL queries (as string in the body) with the output as a JSON
    (in the format described in the documentation above).
    Every SQL expression that ``dask-sql`` understands can be used here.

    See :ref:`server` for more information.

    Note:
        The presto protocol also includes some statistics on the query
        in the response.
        These statistics are currently only filled with placeholder variables.

    Args:
        context (:obj:`dask_sql.Context`): If set, use this context instead of an empty one.
        client (:obj:`dask.distributed.Client`): If set, use this dask client instead of a new one.
        host (:obj:`str`): The host interface to listen on (defaults to all interfaces)
        port (:obj:`int`): The port to listen on (defaults to 8080)
        startup (:obj:`bool`): Whether to wait until Apache Calcite was loaded
        log_level: (:obj:`str`): The log level of the server and dask-sql
        blocking: (:obj:`bool`): If running in an environment with an event loop (e.g. a jupyter notebook),
                do not block. The server can be stopped with `context.stop_server()` afterwards.
        jdbc_metadata: (:obj:`bool`): If enabled create JDBC metadata tables using schemas and tables in
                the current dask_sql context

    Example:
        It is possible to run an SQL server by using the CLI script ``dask-sql-server``
        or by calling this function directly in your user code:

        .. code-block:: python

            from dask_sql import run_server

            # Create your pre-filled context
            c = Context()
            ...

            run_server(context=c)

        After starting the server, it is possible to send queries to it, e.g. with the
        `presto CLI <https://prestosql.io/docs/current/installation/cli.html>`_
        or via sqlalchemy (e.g. using the `PyHive <https://github.com/dropbox/PyHive#sqlalchemy>`_ package):

        .. code-block:: python

            from sqlalchemy.engine import create_engine
            engine = create_engine('presto://localhost:8080/')

            import pandas as pd
            pd.read_sql_query("SELECT 1 + 1", con=engine)

        Of course, it is also possible to call the usual ``CREATE TABLE``
        commands.

        If in a jupyter notebook, you should run the following code

        .. code-block:: python

            from dask_sql import Context

            c = Context()
            c.run_server(blocking=False)

            ...

            c.stop_server()

        Note:
            When running in a jupyter notebook without blocking,
            it is not possible to access the SQL server from within the
            notebook, e.g. using sqlalchemy.
            Doing so will deadlock infinitely.

    """
    _init_app(app, context=context, client=client)
    if jdbc_metadata:
        create_meta_data(context)

    if startup:
        app.c.sql("SELECT 1 + 1").compute()

    config = Config(app, host=host, port=port, log_level=log_level)
    server = Server(config=config)

    loop = asyncio.get_event_loop()
    if blocking:
        if loop and loop.is_running():
            apply(loop=loop)

        server.run()
    else:
        if not loop or not loop.is_running():
            raise AttributeError(
                "blocking=True needs a running event loop (e.g. in a jupyter notebook)"
            )
        loop.create_task(server.serve())
        context.sql_server = server
 def start(self):
     loop = asyncio.get_event_loop()
     config = Config(app=self._web, host="0.0.0.0", port=8083)
     server = Server(config)
     loop.run_until_complete(server.serve())