Ejemplo n.º 1
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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
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))
Ejemplo n.º 4
0
def run_http_app(app, host, port):
    from hypercorn.config import Config as HyperConfig
    from hypercorn.asyncio import serve
    from quart.logging import create_serving_logger

    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 = None
    config.certfile = None
    # config.debug = True
    config.error_logger = config.access_logger  # type: ignore
    config.keyfile = None
    config.use_reloader = False
    scheme = 'https' if config.ssl_enabled else 'http'
    print("Listening on {}://{}".format(scheme, config.bind[0]))
    return serve(app, config)
Ejemplo n.º 5
0
    def start_app(self, debug):
        ssl_context = None
        self.logger.debug("Preparing to start rest-service")
        if self.get_use_ssl() and self.get_key_pem() is not None and \
                self.get_cert_pem() is not None:
            self.logger.debug(
                "Preparing ssl_context with cert:{} and key:{}".format(
                    self.get_cert_pem(), self.get_key_pem()))
            ssl_context = (self.get_cert_pem(), self.get_key_pem())
        self.logger.info("Starting the application {}:{} using ssl? {}".format(
            self.get_listening_host(), self.get_listening_port(),
            ssl_context is None))

        # 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
        self.app.debug = debug
        config = HyperConfig()
        config.debug = debug
        config.access_log_format = "%(h)s %(r)s %(s)s %(b)s %(D)s"
        config.accesslog = self.logger.logger
        config.bind = [
            "{host}:{port}".format(
                **{
                    'host': self.get_listening_host(),
                    'port': self.get_listening_port()
                })
        ]
        config.certfile = self.get_cert_pem() if self.get_use_ssl() else None
        config.keyfile = self.get_key_pem() if self.get_use_ssl() else None

        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 = asyncio.get_event_loop()
        if loop is not None:
            loop.set_debug(debug or False)
            loop.run_until_complete(serve(self.app, config))
        else:
            asyncio.run(serve(self.app, config), debug=config.debug)
Ejemplo n.º 6
0
def test_access_logger_init(
    target: Union[logging.Logger, str, None],
    expected_name: Optional[str],
    expected_handler_type: Optional[Type[logging.Handler]],
) -> None:
    config = Config()
    config.accesslog = target
    config.access_log_format = "%h"
    logger = Logger(config)
    assert logger.access_log_format == "%h"
    assert logger.getEffectiveLevel() == logging.INFO
    if expected_name is None:
        assert logger.access_logger.handlers == []
    else:
        assert logger.access_logger.name == expected_name
        if expected_handler_type is None:
            assert logger.access_logger.handlers == []
        else:
            assert isinstance(logger.access_logger.handlers[0],
                              expected_handler_type)
Ejemplo n.º 7
0
    async def run (self) -> None:
        """
        Run this application.

        This is a simple Hypercorn runner.
        You should probably use something more elaborate in a production setting.
        """
        config = HyperConfig()
        cfg = self.cfg.server
        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"{cfg.host}:{cfg.port}"]
        config.ca_certs = cfg.ca_certs
        config.certfile = cfg.certfile
#   if debug is not None:
#       config.debug = debug
        config.error_logger = config.access_logger  # type: ignore
        config.keyfile = cfg.keyfile
        config.use_reloader = cfg.use_reloader

        scheme = "http" if config.ssl_enabled is None else "https"
        async with trio.open_nursery() as n:
            self.main = n
            await hyper_serve(self.app, config)
Ejemplo n.º 8
0
    def run(
        self,
        host: str = '127.0.0.1',
        port: int = 5000,
        debug: Optional[bool] = None,
        use_reloader: bool = True,
        loop: Optional[asyncio.AbstractEventLoop] = None,
        ca_certs: Optional[str] = None,
        certfile: Optional[str] = None,
        keyfile: Optional[str] = None,
        **kwargs: Any,
    ) -> None:
        """Run this application.

        This is best used for development only, see Hypercorn for
        production servers.

        Arguments:
            host: Hostname to listen on. By default this is loopback
                only, use 0.0.0.0 to have the server listen externally.
            port: Port number to listen on.
            debug: If set enable (or disable) debug mode and debug output.
            use_reloader: Automatically reload on code changes.
            loop: Asyncio loop to create the server in, if None, take default one.
                If specified it is the caller's responsibility to close and cleanup the
                loop.
            ca_certs: Path to the SSL CA certificate file.
            certfile: Path to the SSL certificate file.
            ciphers: Ciphers to use for the SSL setup.
            keyfile: Path to the SSL key file.

        """
        if kwargs:
            warnings.warn(
                f"Additional arguments, {','.join(kwargs.keys())}, are not supported.\n"
                "They may be supported by Hypercorn, which is the ASGI server Quart "
                "uses by default. This method is meant for development and debugging."
            )

        config = HyperConfig()
        config.access_log_format = "{h} | {m} | {s} | {U} | {D}"
        # config.access_logger = colored_logger()
        config.bind = [f"{host}:{port}"]
        config.ca_certs = ca_certs
        config.access_logger_class = AccessLogger
        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 = use_reloader

        scheme = 'https' if config.ssl_enabled else 'http'
        print("Running on {}://{} (CTRL + C to quit)".format(
            scheme, config.bind[0]))  # noqa: T001

        if loop is not None:
            loop.set_debug(config.debug)
            loop.run_until_complete(serve(self, config))  # type: ignore
        else:
            asyncio.run(serve(self, config),
                        debug=config.debug)  # type: ignore
Ejemplo n.º 9
0
from os import path
sys.path.append('../vogdb')
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from vogdb import main
import signal

# Configuration
config = Config()
config.bind = ["localhost:8000"]  # here we add our domain
config.use_reloader = True
config.graceful_timeout = 60.0
config.h11_max_incomplete_size = 16384
config.h2_max_concurrent_streams = 100
config.h2_max_header_list_size = 65536
config.h2_max_inbound_frame_size = 16384
config.access_log_format = '%(h)s %(l)s %(l)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'
config.accesslog = "-"  #path to log file
config.errorlog = "-"  #path to error log file
config.statsd_host = "localhost:8000"

#config.server_names = ["test"]
# uncomment when we have an actual domain and SSL certificates
# config.ca_certs = <path/to/cert.pem>
# config.keyfile = <path/to/key.pem>
# config.insecure_bind = ["domain:80"]

asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
shutdown_event = asyncio.Event()


def _signal_handler(*_: Any) -> None: