Beispiel #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)
Beispiel #2
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))
Beispiel #3
0
 def start_app(self):
     self.loop.create_task(
         self.loop.create_server(
             lambda: Server(app,
                            self.bot.loop,
                            create_serving_logger(),
                            '%(h)s %(r)s %(s)s %(b)s %(D)s',
                            keep_alive_timeout=5),
             host='0.0.0.0',
             port=6142,
             ssl=None))
Beispiel #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)
Beispiel #5
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)
Beispiel #6
0
def main():
    import argparse
    import os.path
    import sys
    import asyncio
    from hypercorn import config
    from hypercorn.asyncio import serve
    from quart.logging import create_serving_logger

    parser = argparse.ArgumentParser(
        prog='datta.s3_server', description='start the s3 compatible server')
    parser.add_argument('-d',
                        default='fdb',
                        dest='dsn',
                        help='DSN for file manager')
    parser.add_argument('--debug',
                        default=False,
                        dest='debug',
                        action='store_true')
    parser.add_argument('-r',
                        dest='host',
                        default='localhost:8484',
                        help='Root domain')
    parser.add_argument('-c',
                        dest='cert_path',
                        help='Path to SSL certificates')
    parser.add_argument('-p', type=int, default=8484, help='port', dest='port')
    parser.add_argument('-a', default='127.0.0.1', help='addr', dest='addr')
    parser.add_argument('-w',
                        type=int,
                        help='# of workers',
                        dest='workers',
                        default=1)
    parser.add_argument('-b',
                        dest='block_server',
                        help='Also run block server at host:port')

    args = parser.parse_args()

    if args.debug:
        app.debug = True

    app.config['SERVER_NAME'] = args.host

    if args.cert_path:
        import ssl
        ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
        ssl_context.set_ciphers('ECDHE+AESGCM')
        ssl_context.load_cert_chain(os.path.join(args.cert_path, 'cert.pem'),
                                    keyfile=os.path.join(
                                        args.cert_path, 'key.pem'))
    else:
        ssl_context = None

    if 'PyPy' not in sys.version:
        try:
            import uvloop
            asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
        except ImportError:
            pass

    app.config['FS_DSN'] = args.dsn
    app.config['MAX_CONTENT_LENGTH'] = 85 * 1024 * 1024
    app.config['MAX_CONTENT_LENGTH'] = None

    hc = config.Config()
    hc.uvloop = True
    hc.ssl = ssl_context
    hc.port = args.port
    hc.debug = hc.use_reloader = args.debug
    hc.keep_alive_timeout = 600

    if hc.debug:
        hc.access_log_format = "%(h)s %(m)s %(U)s?%(q)s %(s)s %(b)s %(D)s"
        hc.access_logger = create_serving_logger()
        # hc.access_log_target = '-'
        hc.error_logger = hc.access_logger
    loop = asyncio.get_event_loop()

    if args.block_server:
        from datta.fs.cas.server import AsyncioBlockServer
        host, port = args.block_server.split(':')
        bs = AsyncioBlockServer(args.dsn,
                                host=host,
                                port=port,
                                debug=args.debug)
        block_server = bs.start(loop=loop, run_loop=False)
    else:
        block_server = None

    if args.workers > 1:
        import signal

        def _shutdown(num, frame):
            raise KeyboardInterrupt()

        signal.signal(signal.SIGTERM, _shutdown)
        if block_server:
            from concurrent.futures.thread import ThreadPoolExecutor
            loop.run_in_executor(ThreadPoolExecutor(), loop.run_forever)
        run.run_multiple(app, hc, workers=args.workers)
    else:
        try:
            loop.run_until_complete(serve(app, hc))
        except KeyboardInterrupt:
            if block_server:
                block_server.close()
                loop.run_until_complete(block_server.wait_closed())
            loop.run_until_complete(loop.shutdown_asyncgens())
            loop.close()
Beispiel #7
0
class WebServer:

    def __init__(self, cfg, factory=Server):
        self.app = Quart(cfg['name'],
                # no, we do not want any of those default folders and whatnot
                static_folder=None,template_folder=None,root_path="/nonexistent",
                )
        self.cfg = cfg = combine_dict(cfg, DEFAULTS)
        self._server = {}
        self.factory = factory
        

        @self.app.route("/test", methods=['GET'])
        async def _get_test():
            msg = json.dumps(dict(hello="This is a test."))
            return Response(msg, content_type="application/json")

        @self.app.route("/test", methods=['POST'])
        async def _echo_data():
            msg = await request.get_data()
            return Response(msg, content_type="application/x-fubar")


        # GET only sends. PUT only receives. POST does both.

        @self.app.route("/json", methods=['GET'])
        async def _get_data():
            s = await self.server
            msg = await s.make_reply()
            return Response(msg, content_type="application/json")

        @self.app.route("/json", methods=['PUT'])
        async def _put_data():
            s = await self.server
            msg = await request.get_data()
            if msg:
                msg = json.loads(msg)
                await s.process_request(msg)
            msg = json.dumps([])
            return Response(msg, content_type="application/json")

        @self.app.route("/json", methods=['POST'])
        async def _post_data():
            s = await self.server
            msg = await request.get_data()
            if msg:
                msg = json.loads(msg)
                await s.process_request(msg)
            msg = await s.make_reply()
            return Response(msg, content_type="application/json")

    def _make_server(self, name):
        return self.factory(name, self.cfg)

    @property
    async def server(self):
        sn = request.headers["Mudlet-Instance"]
        try:
            return self._server[sn]
        except KeyError:
            self._server[sn] = s = self._make_server(sn)
            self._srv_w.send_nowait(s)
        await s.is_running
        return s


    async def run_one(self, s):
        try:
            self._server[s.name] = s
            await s.run()
        except Exception as exc:
            logger.exception("Oops: %s died: %r", s.name, exc)
        finally:
            del self._server[s.name]


    async def run(self, * task_status=trio.TASK_STATUS_IGNORED) -> 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"
        self._srv_w,rdr = trio.open_memory_channel(1)

        async with trio.open_nursery() as n:
            n.start_soon(hyper_serve, self.app, config)
            task_status.started()
            async for s in rdr:
                n.start_soon(self.run_one, s)
            pass # end nursery