1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-26 23:14:19 +00:00
pds-2021-g2-nest/code/backend/nest_backend/app.py

112 lines
2.5 KiB
Python
Raw Normal View History

from flask import Flask
2021-05-07 17:46:14 +00:00
from flask_cors import CORS as FlaskCORS
from flask_jwt_extended import JWTManager as FlaskJWTManager
from flask_sqlalchemy import SQLAlchemy as FlaskSQLAlchemy
from werkzeug.middleware.proxy_fix import ProxyFix as MiddlewareProxyFix
from . import database, routes, gestione, swagger
from .api_spec import spec
# --- MAIN APP ---
app = Flask(__name__)
2021-05-07 17:46:14 +00:00
# --- APP CONFIG
app.config.from_envvar("FLASK_CONFIG")
# --- EXTENSIONS ---
app.config['CORS_HEADERS'] = 'Content-Type'
extension_cors = FlaskCORS(app=app)
extension_jwt = FlaskJWTManager(app=app)
extension_sqlalchemy = database.ext
extension_sqlalchemy.init_app(app=app)
# --- API ROUTES ---
app.add_url_rule(
"/doa",
view_func=routes.page_doa,
methods=["GET", "POST"],
)
app.add_url_rule(
"/api/v1/login",
view_func=routes.page_login,
methods=["POST"],
)
app.add_url_rule(
"/api/v1/users/",
view_func=routes.page_users,
methods=["GET", "POST"],
)
app.add_url_rule(
"/api/v1/users/<string:email>",
view_func=routes.page_user,
methods=["GET", "PATCH", "DELETE"],
)
app.add_url_rule(
"/api/v1/repositories/",
view_func=routes.page_repositories,
methods=["GET", "POST"],
)
app.add_url_rule(
"/api/v1/repositories/<int:rid>",
view_func=routes.page_repository,
methods=["GET", "PATCH", "DELETE", "PUT"],
)
app.add_url_rule(
"/api/v1/repositories/<int:rid>/conditions",
view_func=routes.page_repository_conditions,
methods=["GET", "POST"],
)
2021-05-07 17:51:03 +00:00
app.add_url_rule(
"/api/v1/repositories/<int:rid>/alerts",
view_func=routes.page_repository_alerts,
methods=["GET", "POST"]
)
2021-05-10 14:34:34 +00:00
app.add_url_rule(
"/api/v1/alert/<int:aid>",
view_func=routes.page_alert,
methods=["GET", "PATCH", "DELETE", "PUT"]
)
2021-05-07 17:46:14 +00:00
app.add_url_rule(
"/api/v1/conditions/<int:cid>",
view_func=routes.page_condition,
methods=["GET", "PATCH", "DELETE"],
)
# --- SWAGGER DOCS ---
app.register_blueprint(
swagger.swagger_ui_blueprint,
url_prefix=swagger.SWAGGER_URL
)
with app.test_request_context():
for fn_name in app.view_functions:
if fn_name == 'static':
continue
view_fn = app.view_functions[fn_name]
spec.path(view=view_fn)
app.add_url_rule(
"/docs/swagger.json",
view_func=lambda: gestione.jsonify(spec.to_dict()),
methods=["GET"],
)
# --- ERROR HANDLER ---
app.register_error_handler(
Exception,
gestione.error_handler
)
# --- REVERSE PROXY ---
if not __debug__:
app = MiddlewareProxyFix(app=app, x_for=1, x_proto=0, x_host=1, x_port=0, x_prefix=0)