Skip to main content

Middlware library for your Flask application

Project description

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')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-Middlewares-2.0.0.tar.gz (20.6 kB view details)

Uploaded Source

File details

Details for the file Flask-Middlewares-2.0.0.tar.gz.

File metadata

  • Download URL: Flask-Middlewares-2.0.0.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.0

File hashes

Hashes for Flask-Middlewares-2.0.0.tar.gz
Algorithm Hash digest
SHA256 6f30dc0d436e3bf64531147ad858e58f713f6637bb16d65f2e16dc24f103ad19
MD5 aacbeafc3181d2c4d0f64b85ae4cd45e
BLAKE2b-256 0320191653d5c9b300449d9f599956bfe0c5fbc060a27e993f323b789d0b8f1f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page