예제 #1
0
def start_webserver(host: int, port: int) -> None:
    """Start the Quart webserver."""
    config = Config()
    config.bind = [f"{host}:{port}"]

    app = create_app()
    asyncio.run(serve(app, config))  # type: ignore
예제 #2
0
파일: launch.py 프로젝트: sarzz2/API
def runserver(host: str, port: str, debug: bool, initdb: bool, verbose: bool):
    """
    Run the Quart app.

    :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
    """

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

    if not run_async(
            prepare_postgres(
                retries=6, interval=10.0, db_uri=ENV["DB_URI"], loop=loop)):
        exit(1)  # Connecting to our postgres server failed.

    if initdb:
        run_async(safe_create_tables(verbose=verbose))

    app.debug = debug

    config = Config()
    config.bind = [host + ":" + port]
    run_async(serve(app, config))
예제 #3
0
def test_create_sockets_multiple(monkeypatch: MonkeyPatch) -> None:
    mock_socket = Mock()
    monkeypatch.setattr(socket, "socket", mock_socket)
    config = Config()
    config.bind = ["127.0.0.1", "unix:/tmp/hypercorn.sock"]
    sockets = config.create_sockets()
    assert len(sockets.insecure_sockets) == 2
예제 #4
0
    async def start_http(self, nursery, bind_host="0.0.0.0", port=8000):
        from hypercorn.config import Config
        from hypercorn.trio import serve

        config = Config()
        config.bind = [f"{bind_host}:{port}"]
        await nursery.start(serve, self.app, config)
예제 #5
0
    async def __start_webserver(self):
        class SinglePageApplication(StaticFiles):
            async def get_response(self, path, scope):
                response = await super().get_response(path, scope)
                if response.status_code == 404:
                    response = await super().get_response('.', scope)
                return response

        app = FastAPI()
        server_config = Config()
        server_config.accesslog = '-'
        server_config.behind_proxy = config['behind_proxy']
        server_config.keyfile = config['keyfile']
        server_config.certfile = config['certfile']
        if config['production']:
            server_config.bind = ['0.0.0.0:8000', '[::]:8000']
        api = FastAPI()
        api.include_router(router)
        app.mount('/api', api)
        if not config['production'] and config['cors_allow_origin']:
            app.add_middleware(
                CORSMiddleware,
                allow_origins=config['cors_allow_origin'],
                allow_credentials=True,
                allow_methods=["*"],
                allow_headers=["*"],
            )
        if config['frontend_path'] is not None:
            app.mount(
                '/',
                app=SinglePageApplication(directory=config['frontend_path'],
                                          html=True),
            )
        await serve(app, server_config)
예제 #6
0
async def run_server() -> None:
    async with trio_asyncio.open_loop():
        asyncio._set_running_loop(asyncio.get_event_loop())  # noqa
        config = HyperConfig()
        config.bind = [f"127.0.0.1:5000"]
        config.use_reloader = True
        await serve(app, config)
예제 #7
0
def run(options, connection_uri):
    from app.main import app
    click.secho("Checking configuration ...", fg="yellow")
    try:
        urlparse(connection_uri)._replace(
            path="/{{cookiecutter.project_name}}").geturl()
        assert (hasattr(options, 'production'))
    except Exception as e:
        click.echo(e)
        click.secho("Failed to validate database URI and password", fg="red")

    click.secho("Starting server ...", fg="yellow")
    config = Config()
    config.bind = ["0.0.0.0:8080"]
    config.errorlog = logger
    config.accesslog = logger
    if options.production:
        config.loglevel = "DEBUG"
    else:
        config.loglevel = "INFO"

    if 'uvloop' in sys.modules:
        asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        loop.run_until_complete(serve(app, config))
    else:
        asyncio.run(serve(app, config))
예제 #8
0
async def run(config_file: Optional[TextIO], host: str, port: int,
              dev: bool) -> None:
    """Run the application.

    Create default folders for config and data and the default config file if
    no one is given, create the fastapi app and start the server.

    TODO:
        * setup loging
        * use a FastAPI's "start" event to setup the app, and switch back to uvicorn?
    """
    if not path.exists(DEFAULT_CONFIG_FOLDER):
        mkdir(DEFAULT_CONFIG_FOLDER)

    if not path.exists(DEFAULT_DATA_FOLDER):
        mkdir(DEFAULT_DATA_FOLDER)

    ######################
    # read configuration #
    ######################

    if config_file is None:
        config_file = open(
            path.join(XDG_CONFIG_HOME, "escarpolette", "escarpolette.conf"),
            "w+")

    config = Config(config_file)
    config_file.close()

    ###############
    # set logging #
    ###############

    level = logging.INFO
    if dev:
        level = logging.DEBUG

    logging.basicConfig(
        level=level,
        format=
        "%(asctime)s - %(levelname)s - %(thread)d - %(name)s:%(lineno)s - %(message)s",
    )

    ###################
    # create ASGI app #
    ###################
    app = await create_app(config)

    ####################
    # start web server #
    ####################

    host = host or config.HOST
    port = port or config.PORT

    server_config = HConfig()
    server_config.bind = [f"{host}:{port}"]
    loop = asyncio.get_event_loop()
    future = loop.create_task(serve(app, server_config))
    await future
예제 #9
0
파일: app.py 프로젝트: threathive/dhp
    def quart_run(self):
        # looked at the hypercorn and quart Python project to figure out
        # how to start the application separately, without going through
        # the Quart.app.run APIs
        config = HyperConfig()
        config.debug = self.debug
        config.access_log_format = "%(h)s %(r)s %(s)s %(b)s %(D)s"
        config.accesslog = self.LOGGER
        config.bind = [
            "{host}:{port}".format(**{
                'host': self._App_host,
                'port': self._App_port
            })
        ]
        config.certfile = self._App_server_crt
        config.keyfile = self._App_server_key

        config.errorlog = config.accesslog
        config.use_reloader = True
        scheme = "https" if config.ssl_enabled else "http"

        self.LOGGER.info("Running on {}://{} (CTRL + C to quit)".format(
            scheme, config.bind[0]))
        loop = None  #asyncio.get_event_loop()
        if loop is not None:
            loop.set_debug(debug or False)
            loop.run_until_complete(serve(self, config))
        else:
            asyncio.run(serve(self, config), debug=config.debug)
예제 #10
0
    def handle(self, *args, **options):
        port = options["port"]
        set_perms = options["set_perms"]
        verbosity = "-v 1" if DEBUG else "-v 0"

        # Run any preconfiguration tasks
        if not options["disable_preconfig"]:
            preconfig_args = [
                "preconfig_conreq",
                options["uid"],
                options["gid"],
            ]
            if not set_perms:
                preconfig_args.append("--no-perms")
            call_command(*preconfig_args)

        # Execute tests to ensure Conreq is healthy before starting
        if not options["skip_checks"]:
            call_command("test", "--noinput", "--parallel", "--failfast")

        # Perform any debug related clean-up
        if DEBUG:
            print("Conreq is in DEBUG mode.")
            print("Clearing cache...")
            cache.clear()
            print("Removing stale background tasks...")
            self.reset_huey_db()

        # Migrate the database
        call_command("migrate", "--noinput", verbosity)

        if not DEBUG:
            # Collect static files
            call_command("collectstatic", "--link", "--clear", "--noinput",
                         verbosity)
            call_command("compress", "--force", verbosity)

        # Run background task management
        proc = Process(target=self.start_huey, daemon=True)
        proc.start()

        # Run the production webserver
        if not DEBUG:
            config = HypercornConfig()
            config.bind = f"0.0.0.0:{port}"
            config.websocket_ping_interval = 20
            config.workers = 3
            config.application_path = "conreq.asgi:application"
            config.accesslog = ACCESS_LOG_FILE

            # Additonal webserver configuration
            if os.path.exists(HYPERCORN_TOML):
                config.from_toml(HYPERCORN_TOML)

            # Run the webserver
            run_hypercorn(config)

        # Run the development webserver
        if DEBUG:
            call_command("runserver", f"0.0.0.0:{port}")
예제 #11
0
파일: __main__.py 프로젝트: ante4711/jskom
def run(host, port):
    # use 127.0.0.1 instead of localhost to avoid delays related to ipv6.
    # http://werkzeug.pocoo.org/docs/serving/#troubleshooting
    init_app()
    config = Config()
    config.bind = ["{}:{}".format(host, port)]
    asyncio.run(serve(app, config), debug=True)
예제 #12
0
async def run(  # type: ignore
    self,  # app
    host: str = "127.0.0.1",
    port: int = 5000,
    debug: Optional[bool] = None,
    ca_certs: Optional[str] = None,
    certfile: Optional[str] = None,
    keyfile: Optional[str] = None,
    **kwargs: Any,
) -> None:
    """Run this application.
    """
    config = HyperConfig()
    config.access_log_format = "%(h)s %(r)s %(s)s %(b)s %(D)s"
    config.access_logger = create_serving_logger()  # type: ignore
    config.bind = [f"{host}:{port}"]
    config.ca_certs = ca_certs
    config.certfile = certfile
    #   if debug is not None:
    #       config.debug = debug
    config.error_logger = config.access_logger  # type: ignore
    config.keyfile = keyfile
    config.use_reloader = False

    scheme = "http" if config.ssl_enabled is None else "https"

    await hyper_serve(self, config)
예제 #13
0
    async def run(self):
        # Serve static files from ./static
        static_folder = os.path.join(os.path.dirname(__file__), 'static')
        app = QuartTrio(__name__, static_folder=static_folder)
        app.add_url_rule('/',
                         'static',
                         app.send_static_file,
                         defaults={'filename': 'index.html'})
        app.add_url_rule('/<path:filename>', 'static', app.send_static_file)

        app.add_url_rule('/tasks.json', 'task_tree', self.dispatch_task_tree,
                         ['GET'])
        app.add_url_rule('/task/<int:task_id>/stacktrace.json',
                         'task_stacktrace', self.dispatch_task_stacktrace,
                         ['GET'])
        app.add_url_rule('/nursery/<int:nursery_id>/cancel', 'nursery_cancel',
                         self.dispatch_nursery_cancel, ['GET'])
        app.add_url_rule('/stats.json', 'stats', self.dispatch_stats, ['GET'])

        config = HyperConfig()
        #config.access_log_format = '%(h)s %(r)s %(s)s %(b)s %(D)s'
        #config.access_logger = create_serving_logger()  # type: ignore
        config.bind = [f'{self._host}:{self._port}']
        config.error_logger = config.access_logger  # type: ignore

        #trio.hazmat.add_instrument(self)
        await serve(cors(app), config)
예제 #14
0
 def run_app_sync(*args, loop=None, shutdown_event=None):
     kwargs = {}
     config = Config()
     cert_file_name, key_file_name = ssl_creds or (None, None)
     if cert_file_name:
         kwargs['certfile'] = cert_file_name
         config.certfile = cert_file_name
     if key_file_name:
         kwargs['keyfile'] = key_file_name
         config.keyfile = key_file_name
     setup_quart_logging()
     config.bind = ['0.0.0.0:%s' % port]
     loop = loop or ensure_event_loop()
     run_kwargs = {}
     if shutdown_event:
         run_kwargs['shutdown_trigger'] = shutdown_event.wait
     try:
         try:
             return loop.run_until_complete(serve(app, config, **run_kwargs))
         except Exception as e:
             if 'SSLError' in str(e):
                 c_exists = os.path.exists(cert_file_name)
                 k_exists = os.path.exists(key_file_name)
                 c_size = len(load_file(cert_file_name)) if c_exists else 0
                 k_size = len(load_file(key_file_name)) if k_exists else 0
                 LOG.warning('Unable to create SSL context. Cert files exist: %s %s (%sB), %s %s (%sB)' %
                     (cert_file_name, c_exists, c_size, key_file_name, k_exists, k_size))
             raise
     finally:
         try:
             _cancel_all_tasks(loop)
             loop.run_until_complete(loop.shutdown_asyncgens())
         finally:
             asyncio.set_event_loop(None)
             loop.close()
예제 #15
0
 def run_app_sync(*args, loop=None, shutdown_event=None):
     kwargs = {}
     config = Config()
     cert_file_name, key_file_name = ssl_creds or (None, None)
     if cert_file_name:
         kwargs['certfile'] = cert_file_name
         config.certfile = cert_file_name
     if key_file_name:
         kwargs['keyfile'] = key_file_name
         config.keyfile = key_file_name
     setup_quart_logging()
     config.bind = ['0.0.0.0:%s' % port]
     loop = loop or ensure_event_loop()
     run_kwargs = {}
     if shutdown_event:
         run_kwargs['shutdown_trigger'] = shutdown_event.wait
     try:
         return loop.run_until_complete(serve(app, config, **run_kwargs))
     finally:
         try:
             _cancel_all_tasks(loop)
             loop.run_until_complete(loop.shutdown_asyncgens())
         finally:
             asyncio.set_event_loop(None)
             loop.close()
예제 #16
0
    async def start_http(self, nursery, port=0):
        from hypercorn.config import Config
        from hypercorn.trio import serve

        config = Config()
        config.bind = [f"localhost:{port}"]
        [base_url] = await nursery.start(serve, self._app, config)
        self.base_url = base_url
        return base_url
예제 #17
0
파일: service.py 프로젝트: pnxtech/HydraPy
async def startQuart(si):
    print(f"{si['serviceName']}({si['instanceID']})(v{si['serviceVersion']}) running at {si['serviceIP']}:{si['servicePort']}")
    config = Config()
    config.bind = [f"{si['serviceIP']}:{si['servicePort']}"]
    # config.bind = [f"0.0.0.0:{si['servicePort']}"]
    config.access_log_format = '%(h)s %(r)s %(s)s %(b)s %(D)s'
    config.accesslog = create_serving_logger()
    config.errorlog = config.accesslog
    loop = asyncio.get_event_loop()
    await loop.create_task(serve(app, config))
예제 #18
0
def api():
    import asyncio
    from hypercorn.config import Config as HypConfig
    from hypercorn.asyncio import serve

    hyp_config = HypConfig()
    hyp_config.bind = [config.API_BIND_ADDR]
    hyp_config.certfile = config.API_TLS_CERT_PATH or None
    hyp_config.keyfile = config.API_TLS_KEY_PATH or None

    asyncio.run(serve(app, hyp_config))
예제 #19
0
def test_create_sockets_fd(monkeypatch: MonkeyPatch) -> None:
    mock_fromfd = Mock()
    monkeypatch.setattr(socket, "fromfd", mock_fromfd)
    config = Config()
    config.bind = ["fd://2"]
    sockets = config.create_sockets()
    sock = sockets.insecure_sockets[0]
    mock_fromfd.assert_called_with(2, socket.AF_UNIX, socket.SOCK_STREAM)
    sock.setsockopt.assert_called_with(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)  # type: ignore
    sock.setblocking.assert_called_with(False)  # type: ignore
    sock.set_inheritable.assert_called_with(True)  # type: ignore
예제 #20
0
def start_hypercorn_server(app: Application, config: EasyDict) -> None:
    """Start the hypercorn ASGI server"""

    web_config = Config()
    web_config.bind = [f'{config.host}:{config.port}']

    if config.ssl.enabled:
        web_config.keyfile = expand_path(config.ssl.keyfile)
        web_config.certfile = expand_path(config.ssl.certfile)

    asyncio.run(serve(app, web_config))
예제 #21
0
async def run_server() -> None:
    async with trio_asyncio.open_loop():
        # trio_asyncio has difficulties with aioredis, workaround here:
        # https://github.com/python-trio/trio-asyncio/issues/63 (answer from @parity3)
        asyncio._set_running_loop(asyncio.get_event_loop())

        config = HyperConfig()
        config.bind = [f"0.0.0.0:5000"]
        config.use_reloader = True
        app.static_folder = "frontend"
        await serve(app, config)
예제 #22
0
def start_http_server(app: Application, config: Dict[str, Any]) -> None:
    """Start the HTTP server given the ASGI application and some config.

    :param app: The ASGI application.
    :type app: Application
    :param config: A dictionary of configuration.
    :type config: Dict[str, Any]
    """
    http_config = Config()
    http_config.bind = [f"{config['host']}:{config['port']}"]
    asyncio.run(serve(app, http_config))
예제 #23
0
def run_server_loop(app, port):
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    config = Config()
    config.bind = [f"localhost:{port}"]

    # As of Hypercorn 0.11.0, need to explicitly set signal handlers to a no-op
    # (otherwise it will try to set signal handlers assuming it is on the main thread which throws an error)
    loop.run_until_complete(
        serve(app, config, shutdown_trigger=lambda: asyncio.Future()))
    loop.close()
예제 #24
0
def test_create_sockets_unix(monkeypatch: MonkeyPatch) -> None:
    mock_socket = Mock()
    monkeypatch.setattr(socket, "socket", mock_socket)
    config = Config()
    config.bind = ["unix:/tmp/hypercorn.sock"]
    sockets = config.create_sockets()
    sock = sockets.insecure_sockets[0]
    mock_socket.assert_called_with(socket.AF_UNIX, socket.SOCK_STREAM)
    sock.setsockopt.assert_called_with(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)  # type: ignore
    sock.bind.assert_called_with("/tmp/hypercorn.sock")  # type: ignore
    sock.setblocking.assert_called_with(False)  # type: ignore
    sock.set_inheritable.assert_called_with(True)  # type: ignore
예제 #25
0
파일: server.py 프로젝트: tkf/refresher
async def start_server(root: str, debug: bool, port: int) -> None:
    app.config["DEBUG"] = debug

    cfg = Config()
    cfg.bind = [f"localhost:{port}"]

    async with open_watcher(root) as watcher:
        app.config["REFRESHER_WATCHER"] = watcher
        logger.info("Serving and watching: %s", watcher.root.resolve())

        # https://pgjones.gitlab.io/hypercorn/how_to_guides/api_usage.html
        await serve(app, cfg)
예제 #26
0
def serve(title: str, description: str, mapping: Dict[str, Definition],
          port: int):
    app = FastAPI(title=title, description=description)
    router = GeneratedRouter(mapping)
    app.include_router(router)

    conf = Config()
    conf.bind = [f"0.0.0.0:{port}"]

    loop = asyncio.get_event_loop()
    loop.add_signal_handler(signal.SIGTERM, _signal_handler)
    loop.run_until_complete(
        hyper_serve(app, conf, shutdown_trigger=shutdown_event.wait))
예제 #27
0
파일: main.py 프로젝트: amadeuszkryze/bday
def main():
    config = Config()
    config.bind = "0.0.0.0:8080"
    config.use_reloader = bool(os.getenv("AUTO_RELOAD", False))

    loop = asyncio.get_event_loop()
    loop.add_signal_handler(signal.SIGTERM, _signal_handler)
    loop.run_until_complete(
        serve(
            app,
            config,
            shutdown_trigger=shutdown_event.wait,
        )
    )
예제 #28
0
def main(host="localhost", port=8000, server="uvicorn"):
    if server == "uvicorn":
        uvicorn.run("minij_proxy_asgi:application",
                    host=host,
                    port=port,
                    log_level="info")

    elif server == "hypercorn":
        from hypercorn.asyncio import serve
        from hypercorn.config import Config

        config = Config()
        config.bind = [f"{host}:{port}"]
        asyncio.run(serve(application, config))
예제 #29
0
def run_production():
    import asyncio
    import uvloop
    from hypercorn.asyncio import serve
    from hypercorn.config import Config

    config = Config()
    config.bind = ['%s:%s' % (api_config.HOST, api_config.PORT)]

    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(serve(app, config))
    pass
예제 #30
0
 def run_app_sync(*args, loop=None, shutdown_event=None):
     kwargs = {}
     config = Config()
     cert_file_name, key_file_name = ssl_creds or (None, None)
     if cert_file_name:
         kwargs["certfile"] = cert_file_name
         config.certfile = cert_file_name
     if key_file_name:
         kwargs["keyfile"] = key_file_name
         config.keyfile = key_file_name
     setup_quart_logging()
     config.bind = [f"{bind_address}:{port}" for bind_address in bind_addresses]
     config.workers = len(bind_addresses)
     loop = loop or ensure_event_loop()
     run_kwargs = {}
     if shutdown_event:
         run_kwargs["shutdown_trigger"] = shutdown_event.wait
     try:
         try:
             return loop.run_until_complete(serve(app, config, **run_kwargs))
         except Exception as e:
             LOG.info(
                 "Error running server event loop on port %s: %s %s",
                 port,
                 e,
                 traceback.format_exc(),
             )
             if "SSL" in str(e):
                 c_exists = os.path.exists(cert_file_name)
                 k_exists = os.path.exists(key_file_name)
                 c_size = len(load_file(cert_file_name)) if c_exists else 0
                 k_size = len(load_file(key_file_name)) if k_exists else 0
                 LOG.warning(
                     "Unable to create SSL context. Cert files exist: %s %s (%sB), %s %s (%sB)",
                     cert_file_name,
                     c_exists,
                     c_size,
                     key_file_name,
                     k_exists,
                     k_size,
                 )
             raise
     finally:
         try:
             _cancel_all_tasks(loop)
             loop.run_until_complete(loop.shutdown_asyncgens())
         finally:
             asyncio.set_event_loop(None)
             loop.close()