## Flask-middlewares
Allows you to use middleware in Flask
### Installation
`pip install Flask-Middlewares`
### Example
```python
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')
```
Raw data
{
"_id": null,
"home_page": "https://github.com/TheArtur128/Flask-middlewares",
"name": "Flask-Middlewares",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.11",
"maintainer_email": "",
"keywords": "flask,middleware,library,middlewares",
"author": "Arthur",
"author_email": "s9339307190@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/03/20/191653d5c9b300449d9f599956bfe0c5fbc060a27e993f323b789d0b8f1f/Flask-Middlewares-2.0.0.tar.gz",
"platform": null,
"description": "## Flask-middlewares\r\nAllows you to use middleware in Flask\r\n\r\n### Installation\r\n`pip install Flask-Middlewares`\r\n\r\n### Example\r\n```python\r\nfrom typing import Callable\r\n\r\nfrom flask import Flask, Blueprint\r\nfrom pyhandling.annotations import decorator\r\n\r\nfrom flask_middlewares import MultipleMiddlewareRegistrar\r\nfrom flask_middlewares.tools import EVERYTHING\r\n\r\n\r\napp = Flask(__name__)\r\n\r\napi_blueprint = Blueprint('api', __name__)\r\nview_blueprint = Blueprint('views', __name__)\r\n\r\n\r\ndef concatenation_by(line: str) -> decorator:\r\n \"\"\"\r\n Function for a decorator that concatenates the result of the decorated\r\n function with the input result of this function.\r\n \"\"\"\r\n\r\n def decorator(func: Callable) -> Callable:\r\n def wrapper(*args, **kwargs) -> str:\r\n return func(*args, **kwargs) + ' ' + line\r\n\r\n return wrapper\r\n\r\n return decorator\r\n\r\n\r\n# Config for creating a middleware registrar for your application\r\n# See the MiddlewareRegistrar.from_config documentation.\r\n\r\napp.config[\"ENVIRONMENTS\"] = {\r\n \"global\": {\r\n \"MIDDLEWARES\": [concatenation_by(\"from global\")],\r\n \"VIEW_NAMES\": EVERYTHING\r\n },\r\n 'api': {\r\n \"USE_FOR_BLUEPRINT\": True,\r\n \"MIDDLEWARES\": [concatenation_by(\"from api\")]\r\n }\r\n}\r\n\r\n@app.route('/')\r\ndef index():\r\n return \"Real but fake home page\" # Real but fake home page from global\r\n\r\n\r\n@view_blueprint.route('/home')\r\ndef home_endpoint():\r\n return \"Real home page\" # Real home page from global\r\n\r\n\r\n@api_blueprint.route('/users')\r\ndef user_api_endpoint():\r\n return \"\\\"Some user data\\\"\" # \"Some user data\" from global from api\r\n\r\n\r\napp.register_blueprint(api_blueprint, url_prefix='/api')\r\napp.register_blueprint(view_blueprint)\r\n\r\nMultipleMiddlewareRegistrar.from_config(app.config, environments_only=True).init_app(app)\r\n\r\nif __name__ == '__main__':\r\n app.run(debug=True, port='8048')\r\n```\r\n",
"bugtrack_url": null,
"license": "GNU General Public License v3.0",
"summary": "Middlware library for your Flask application",
"version": "2.0.0",
"split_keywords": [
"flask",
"middleware",
"library",
"middlewares"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0320191653d5c9b300449d9f599956bfe0c5fbc060a27e993f323b789d0b8f1f",
"md5": "aacbeafc3181d2c4d0f64b85ae4cd45e",
"sha256": "6f30dc0d436e3bf64531147ad858e58f713f6637bb16d65f2e16dc24f103ad19"
},
"downloads": -1,
"filename": "Flask-Middlewares-2.0.0.tar.gz",
"has_sig": false,
"md5_digest": "aacbeafc3181d2c4d0f64b85ae4cd45e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11",
"size": 20552,
"upload_time": "2023-02-02T16:41:21",
"upload_time_iso_8601": "2023-02-02T16:41:21.090410Z",
"url": "https://files.pythonhosted.org/packages/03/20/191653d5c9b300449d9f599956bfe0c5fbc060a27e993f323b789d0b8f1f/Flask-Middlewares-2.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-02-02 16:41:21",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "TheArtur128",
"github_project": "Flask-middlewares",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "flask-middlewares"
}