예제 #1
0
    def run(self):
        http_config = self.config['http']
        https_config = self.config['https']

        wsgi_app = wsgi.WSGIPathInfoDispatcher({'/': self.app})
        cherrypy.server.unsubscribe()
        cherrypy.config.update({'environment': 'production'})

        if https_config['enabled']:
            try:
                bind_addr_https = (https_config['listen'],
                                   https_config['port'])
                server_https = wsgi.WSGIServer(bind_addr=bind_addr_https,
                                               wsgi_app=wsgi_app)
                server_https.ssl_adapter = http_helpers.ssl_adapter(
                    https_config['certificate'], https_config['private_key'])

                ServerAdapter(cherrypy.engine, server_https).subscribe()
                logger.debug(
                    'WSGIServer starting... uid: %s, listen: %s:%s',
                    os.getuid(),
                    bind_addr_https[0],
                    bind_addr_https[1],
                )
            except IOError as e:
                logger.warning("HTTPS server won't start: %s", e)
        else:
            logger.debug('HTTPS server is disabled')

        if http_config['enabled']:
            bind_addr_http = (http_config['listen'], http_config['port'])
            server_http = wsgi.WSGIServer(bind_addr=bind_addr_http,
                                          wsgi_app=wsgi_app)
            ServerAdapter(cherrypy.engine, server_http).subscribe()
            logger.debug(
                'WSGIServer starting... uid: %s, listen: %s:%s',
                os.getuid(),
                bind_addr_http[0],
                bind_addr_http[1],
            )
        else:
            logger.debug('HTTP server is disabled')

        if not http_config['enabled'] and not https_config['enabled']:
            logger.critical('No HTTP/HTTPS server enabled')
            exit()

        list_routes(self.app)

        try:
            cherrypy.engine.start()
            cherrypy.engine.wait(states.EXITING)
        except KeyboardInterrupt:
            logger.warning('Stopping wazo-phoned: KeyboardInterrupt')
            cherrypy.engine.exit()
예제 #2
0
def run_server(app):
    http_config = app.config['rest_api']['http']
    https_config = app.config['rest_api']['https']

    signal.signal(signal.SIGTERM, signal_handler)
    if app.config['profile']:
        app.wsgi_app = ProfilerMiddleware(app.wsgi_app,
                                          profile_dir=app.config['profile'])

    wsgi_app = wsgiserver.WSGIPathInfoDispatcher({'/': app})

    cherrypy.server.unsubscribe()
    cherrypy.config.update({'environment': 'production'})

    if not (http_config['enabled'] or https_config['enabled']):
        logger.critical('No HTTP/HTTPS server enabled')
        exit()

    if https_config['enabled']:
        try:
            bind_addr_https = (https_config['listen'], https_config['port'])
            server_https = CherryPyWSGIServer(bind_addr=bind_addr_https,
                                              wsgi_app=wsgi_app)
            server_https.ssl_adapter = http_helpers.ssl_adapter(https_config['certificate'],
                                                                https_config['private_key'],
                                                                https_config['ciphers'])
            ServerAdapter(cherrypy.engine, server_https).subscribe()

            logger.debug('HTTPS server starting on %s:%s', *bind_addr_https)

        except IOError as e:
            logger.warning("HTTPS server won't start: %s", e)
    else:
        logger.debug('HTTPS server is disabled')

    if http_config['enabled']:
        bind_addr_http = (http_config['listen'], http_config['port'])
        server_http = CherryPyWSGIServer(bind_addr=bind_addr_http,
                                         wsgi_app=wsgi_app)
        ServerAdapter(cherrypy.engine, server_http).subscribe()

        logger.debug('HTTP server starting on %s:%s', *bind_addr_http)
    else:
        logger.debug('HTTP server is disabled')

    try:
        cherrypy.engine.start()
        cherrypy.engine.block()
    except KeyboardInterrupt:
        cherrypy.engine.stop()
예제 #3
0
    def setup_kws_server(self):
        """
        sets up the KWS server to run on a separate port
        """
        # based on http://docs.cherrypy.org/stable/refman/process/servers.html

        if not self.config['keyword_search']['kws_service_on']:
            return

        # load KWSWebService here since it will be loaded after self.configure
        # which brings secmodv2 into cherrypy.tools
        from DAS.web.kws_web_srv import KWSWebService
        kws_service = KWSWebService(self.config)
        kws_wsgi_app = _cptree.Tree()
        kws_wsgi_app.mount(kws_service, '/das')

        config = self.config['web_server']
        port = int(config.get("kws_port", 8214))
        host = config.get("kws_host", '0.0.0.0')

        if CherryPyWSGIServer:
            kws_server = CherryPyWSGIServer(
                bind_addr=(host, port),
                wsgi_app=kws_wsgi_app,
                numthreads=int(config.get("thread_pool_kws", 10)),
                request_queue_size=cpconfig["server.socket_queue_size"],
                # below are cherrypy default settings...
                # max=-1,
                # timeout=10,
                # shutdown_timeout=5
            )
            srv_adapter = ServerAdapter(engine, kws_server)
            srv_adapter.subscribe()
예제 #4
0
    def run(self):
        wsgi_app_https = ReverseProxied(
            ProxyFix(wsgi.WSGIPathInfoDispatcher({'/': app})))
        wsgi_app_http = ReverseProxied(
            ProxyFix(wsgi.WSGIPathInfoDispatcher({'/': adapter_app})))
        cherrypy.server.unsubscribe()
        cherrypy.config.update({'environment': 'production'})

        bind_addr = (self.config['listen'], self.config['port'])

        server_https = wsgi.WSGIServer(bind_addr=bind_addr,
                                       wsgi_app=wsgi_app_https)
        server_https.ssl_adapter = http_helpers.ssl_adapter(
            self.config['certificate'], self.config['private_key'])
        ServerAdapter(cherrypy.engine, server_https).subscribe()
        logger.debug('WSGIServer starting... uid: %s, listen: %s:%s',
                     os.getuid(), bind_addr[0], bind_addr[1])

        for route in http_helpers.list_routes(app):
            logger.debug(route)

        if self.adapter_config['enabled']:
            bind_addr = (self.adapter_config['listen'],
                         self.adapter_config['port'])
            server_adapter = wsgi.WSGIServer(bind_addr=bind_addr,
                                             wsgi_app=wsgi_app_http)
            ServerAdapter(cherrypy.engine, server_adapter).subscribe()
            logger.debug('WSGIServer starting... uid: %s, listen: %s:%s',
                         os.getuid(), bind_addr[0], bind_addr[1])

            for route in http_helpers.list_routes(adapter_app):
                logger.debug(route)

        else:
            logger.debug('Adapter server is disabled')

        try:
            cherrypy.engine.start()
            cherrypy.engine.wait(states.EXITING)
        except KeyboardInterrupt:
            logger.warning('Stopping xivo-ctid-ng: KeyboardInterrupt')
            cherrypy.engine.exit()
예제 #5
0
    def add_server(self, netloc, path, config):
        """Add a new CherryPy Application for a Virtual Host.
        Creates a new CherryPy WSGI Server instance if the host resolves
        to a different IP address or port.
        """

        from cherrypy._cpwsgi_server import CPWSGIServer
        from cherrypy.process.servers import ServerAdapter

        host, port = urllib.splitnport(netloc, 80)
        host = socket.gethostbyname(host)
        bind_addr = (host, port)
        if bind_addr not in self.servers:
            self.servers.append(bind_addr)
            server = CPWSGIServer()
            server.bind_addr = bind_addr
            adapter = ServerAdapter(cherrypy.engine, server, server.bind_addr)
            adapter.subscribe()
        self.domains[netloc] = cherrypy.Application(
            root=None, config={path.rstrip('/') or '/': config})
예제 #6
0
def configure_http_server(port):
    # Mount the application
    from kolibri.deployment.default.wsgi import application

    whitenoise_settings = {
        "static_root": settings.STATIC_ROOT,
        "static_prefix": settings.STATIC_URL,
        # Use 1 day as the default cache time for static assets
        "max_age": 24 * 60 * 60,
        # Add a test for any file name that contains a semantic version number
        # or a 32 digit number (assumed to be a file hash)
        # these files will be cached indefinitely
        "immutable_file_test":
        r"((0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)|[a-f0-9]{32})",
        "autorefresh": getattr(settings, "DEVELOPER_MODE", False),
    }

    # Mount static files
    application = DjangoWhiteNoise(application, **whitenoise_settings)

    cherrypy.tree.graft(application, "/")

    # Mount media files
    cherrypy.tree.mount(
        cherrypy.tools.staticdir.handler(section="/", dir=settings.MEDIA_ROOT),
        settings.MEDIA_URL,
    )

    # Mount content files
    CONTENT_ROOT = "/" + paths.get_content_url(
        conf.OPTIONS["Deployment"]["URL_PATH_PREFIX"]).lstrip("/")
    content_dirs = [paths.get_content_dir_path()
                    ] + paths.get_content_fallback_paths()
    dispatcher = MultiStaticDispatcher(content_dirs)
    content_handler = cherrypy.tree.mount(
        None,
        CONTENT_ROOT,
        config={
            "/": {
                "tools.caching.on": False,
                "request.dispatch": dispatcher
            }
        },
    )

    cherrypy_server_config = {
        "server.socket_host":
        LISTEN_ADDRESS,
        "server.socket_port":
        port,
        "server.thread_pool":
        conf.OPTIONS["Server"]["CHERRYPY_THREAD_POOL"],
        "server.socket_timeout":
        conf.OPTIONS["Server"]["CHERRYPY_SOCKET_TIMEOUT"],
        "server.accepted_queue_size":
        conf.OPTIONS["Server"]["CHERRYPY_QUEUE_SIZE"],
        "server.accepted_queue_timeout":
        conf.OPTIONS["Server"]["CHERRYPY_QUEUE_TIMEOUT"],
    }

    # Configure the server
    cherrypy.config.update(cherrypy_server_config)

    alt_port_addr = (
        LISTEN_ADDRESS,
        conf.OPTIONS["Deployment"]["ZIP_CONTENT_PORT"],
    )

    # Mount static files
    alt_port_app = wsgi.PathInfoDispatcher({
        "/": get_application(),
        CONTENT_ROOT: content_handler
    })
    alt_port_app = DjangoWhiteNoise(alt_port_app, **whitenoise_settings)

    alt_port_server = ServerAdapter(
        cherrypy.engine,
        wsgi.Server(
            alt_port_addr,
            alt_port_app,
            numthreads=conf.OPTIONS["Server"]["CHERRYPY_THREAD_POOL"],
            request_queue_size=conf.OPTIONS["Server"]["CHERRYPY_QUEUE_SIZE"],
            timeout=conf.OPTIONS["Server"]["CHERRYPY_SOCKET_TIMEOUT"],
            accepted_queue_size=conf.OPTIONS["Server"]["CHERRYPY_QUEUE_SIZE"],
            accepted_queue_timeout=conf.OPTIONS["Server"]
            ["CHERRYPY_QUEUE_TIMEOUT"],
        ),
        alt_port_addr,
    )
    # Subscribe these servers
    cherrypy.server.subscribe()
    alt_port_server.subscribe()