Skip to main content

Flask-middlewares

Allows you to use middleware in Flask

Installation

pip install Flask-Middlewares

Example

from typing import Callable

from flask import Flask, Blueprint
from pyhandling.annotations import decorator

from flask_middlewares import MultipleMiddlewareRegistrar
from flask_middlewares.tools import EVERYTHING


app = Flask(__name__)

api_blueprint = Blueprint('api', __name__)
view_blueprint = Blueprint('views', __name__)


def concatenation_by(line: str) -> decorator:
    """
    Function for a decorator that concatenates the result of the decorated
    function with the input result of this function.
    """

    def decorator(func: Callable) -> Callable:
        def wrapper(*args, **kwargs) -> str:
            return func(*args, **kwargs) + ' ' + line

        return wrapper

    return decorator


# Config for creating a middleware registrar for your application
# See the MiddlewareRegistrar.from_config documentation.

app.config["ENVIRONMENTS"] = {
    "global": {
        "MIDDLEWARES": [concatenation_by("from global")],
        "VIEW_NAMES": EVERYTHING
    },
    'api': {
        "USE_FOR_BLUEPRINT": True,
        "MIDDLEWARES": [concatenation_by("from api")]
    }
}

@app.route('/')
def index():
    return "Real but fake home page" # Real but fake home page from global


@view_blueprint.route('/home')
def home_endpoint():
    return "Real home page" # Real home page from global


@api_blueprint.route('/users')
def user_api_endpoint():
    return "\"Some user data\"" # "Some user data" from global from api


app.register_blueprint(api_blueprint, url_prefix='/api')
app.register_blueprint(view_blueprint)

MultipleMiddlewareRegistrar.from_config(app.config, environments_only=True).init_app(app)

if __name__ == '__main__':
    app.run(debug=True, port='8048')

Metadata

Release files for Flask-Middlewares 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Flask-Middlewares 2.0.0
File Size Uploaded
Flask-Middlewares-2.0.0.tar.gz 20.6 kB Details

Release files / Flask-Middlewares-2.0.0.tar.gz

Download URL Flask-Middlewares-2.0.0.tar.gz
Size 20.6 kB
Tags Source
SHA-256 checksum
How to use checksums
6f30dc0d436e3bf64531147ad858e58f713f6637bb16d65f2e16dc24f103ad19
BLAKE2b-256 checksum
How to use checksums
0320191653d5c9b300449d9f599956bfe0c5fbc060a27e993f323b789d0b8f1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.0

Release history Release notifications | RSS feed

This release

2.0.0 This release

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page