Python werkzeug.middleware.dispatcher.DispatcherMiddleware() Examples

The following are 4 code examples of werkzeug.middleware.dispatcher.DispatcherMiddleware(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module werkzeug.middleware.dispatcher , or try the search function .
Example #1
Source File: init_wsgi_middlewares.py    From airflow with Apache License 2.0 6 votes vote down vote up
def init_wsgi_middleware(flask_app: Flask):
    """Handle X-Forwarded-* headers and base_url support"""
    # Apply DispatcherMiddleware
    base_url = urlparse(conf.get('webserver', 'base_url'))[2]
    if not base_url or base_url == '/':
        base_url = ""
    if base_url:
        flask_app.wsgi_app = DispatcherMiddleware(  # type: ignore
            _root_app, mounts={base_url: flask_app.wsgi_app}
        )

    # Apply ProxyFix middleware
    if conf.getboolean('webserver', 'ENABLE_PROXY_FIX'):
        flask_app.wsgi_app = ProxyFix(  # type: ignore
            flask_app.wsgi_app,
            x_for=conf.getint("webserver", "PROXY_FIX_X_FOR", fallback=1),
            x_proto=conf.getint("webserver", "PROXY_FIX_X_PROTO", fallback=1),
            x_host=conf.getint("webserver", "PROXY_FIX_X_HOST", fallback=1),
            x_port=conf.getint("webserver", "PROXY_FIX_X_PORT", fallback=1),
            x_prefix=conf.getint("webserver", "PROXY_FIX_X_PREFIX", fallback=1),
        ) 
Example #2
Source File: __init__.py    From squealy with MIT License 6 votes vote down vote up
def _add_promethueus_middleware(app):
    # Add prometheus wsgi middleware to route /metrics requests
    # application object is then used by wsgi / gunicorn to startup the application
    # NOTE: This means prometheus metrics are not exposed in development mode
    wsgi_app = DispatcherMiddleware(app, {
        '/metrics': make_wsgi_app()
    })
    return wsgi_app 
Example #3
Source File: app.py    From cloudkitty with Apache License 2.0 6 votes vote down vote up
def setup_app():
    root_app = flask.Flask('cloudkitty')
    root_api = flask_restful.Api(root_app)
    root_api.add_resource(api_root.CloudkittyAPIRoot, '/')

    dispatch_dict = {
        '/v1': get_v1_app(),
        '/v2': get_v2_app(),
    }

    # Disabling v2 api in case v1 storage is used
    if CONF.storage.version < 2:
        LOG.warning('v1 storage is used, disabling v2 API')
        dispatch_dict.pop('/v2')

    app = dispatcher.DispatcherMiddleware(root_app, dispatch_dict)
    return app 
Example #4
Source File: cli.py    From fava with MIT License 4 votes vote down vote up
def main(
    filenames, port, host, prefix, incognito, debug, profile, profile_dir
):  # pragma: no cover
    """Start Fava for FILENAMES on http://<host>:<port>.

    If the `BEANCOUNT_FILE` environment variable is set, Fava will use the
    files (space-delimited) specified there in addition to FILENAMES.

    Note you can also specify command-line options via environment variables.
    For example, `--host=0.0.0.0` is equivalent to setting the environment
    variable `FAVA_HOST=0.0.0.0`.
    """

    if profile:
        debug = True

    env_filename = os.environ.get("BEANCOUNT_FILE")
    if env_filename:
        filenames = filenames + tuple(env_filename.split())

    if not filenames:
        raise click.UsageError("No file specified")

    app.config["BEANCOUNT_FILES"] = filenames
    app.config["INCOGNITO"] = incognito

    if prefix:
        app.wsgi_app = DispatcherMiddleware(
            simple_wsgi, {prefix: app.wsgi_app}
        )

    if not debug:
        server = Server((host, port), app)
        print(f"Running Fava on http://{host}:{port}")
        server.safe_start()
    else:
        if profile:
            app.config["PROFILE"] = True
            app.wsgi_app = ProfilerMiddleware(
                app.wsgi_app,
                restrictions=(30,),
                profile_dir=profile_dir if profile_dir else None,
            )

        app.jinja_env.auto_reload = True
        try:
            app.run(host, port, debug)
        except OSError as error:
            if error.errno == errno.EADDRINUSE:
                raise click.UsageError(
                    "Can not start webserver because the port is already in "
                    "use. Please choose another port with the '-p' option."
                )
            raise


# needed for pyinstaller: