|
| 1 | +# encoding: utf-8 |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +from flasgger import Swagger |
| 6 | +from flask import Flask |
| 7 | + |
| 8 | +from project.config import CONFIG |
| 9 | + |
| 10 | +__author__ = "Alberto Vara" |
| 11 | + |
| 12 | +__version__ = "0.0.1" |
| 13 | + |
| 14 | +ENVIRONMENT = os.environ.get("ENVIRONMENT", "default") |
| 15 | + |
| 16 | +SWAGGER_CONFIG = { |
| 17 | + "headers": [ |
| 18 | + ], |
| 19 | + "specs": [ |
| 20 | + { |
| 21 | + "endpoint": 'apispec_1', |
| 22 | + "route": '{application_root}/apispec_1.json', |
| 23 | + "rule_filter": lambda rule: True, # all in |
| 24 | + "model_filter": lambda tag: True, # all in |
| 25 | + } |
| 26 | + ], |
| 27 | + "info": { |
| 28 | + "title": "API ", |
| 29 | + "description": "API para...", |
| 30 | + "contact": { |
| 31 | + "responsibleOrganization": "ME", |
| 32 | + "responsibleDeveloper": "Me", |
| 33 | + |
| 34 | + }, |
| 35 | + "version": "0.0.1" |
| 36 | + }, |
| 37 | + "securityDefinitions": { |
| 38 | + "APIKeyHeader": {"type": "apiKey", "name": "Authorization", "in": "header"}, |
| 39 | + }, |
| 40 | + "static_url_path": "{application_root}/flasgger_static", |
| 41 | + "swagger_ui": True, |
| 42 | + "uiversion": 2, |
| 43 | + "specs_route": "/apidocs/", |
| 44 | + "basePath": "{application_root}" |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +class PrefixMiddleware(object): |
| 49 | + |
| 50 | + def __init__(self, app, prefix=''): |
| 51 | + self.app = app |
| 52 | + self.prefix = prefix |
| 53 | + |
| 54 | + def __call__(self, environ, start_response): |
| 55 | + |
| 56 | + if environ['PATH_INFO'].startswith(self.prefix): |
| 57 | + environ['PATH_INFO'] = environ['PATH_INFO'][len(self.prefix):] |
| 58 | + environ['SCRIPT_NAME'] = self.prefix |
| 59 | + return self.app(environ, start_response) |
| 60 | + else: |
| 61 | + start_response('404', [('Content-Type', 'text/plain')]) |
| 62 | + return ["This url does not belong to the app.".encode()] |
| 63 | + |
| 64 | + |
| 65 | +def create_app(): |
| 66 | + from project.models import db |
| 67 | + from project.views import views_bp as views_blueprint |
| 68 | + from project.views.oauth import jwt, bcrypt |
| 69 | + environment = os.environ.get("ENVIRONMENT", "default") |
| 70 | + |
| 71 | + app = Flask(__name__) |
| 72 | + app.config.from_object(CONFIG[environment]) |
| 73 | + app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=app.config["APPLICATION_ROOT"]) |
| 74 | + |
| 75 | + db.init_app(app) |
| 76 | + bcrypt.init_app(app) |
| 77 | + jwt.init_app(app) |
| 78 | + |
| 79 | + SWAGGER_CONFIG["specs"][0]["route"] = SWAGGER_CONFIG["specs"][0]["route"].format( |
| 80 | + application_root=app.config["APPLICATION_ROOT"] |
| 81 | + ) |
| 82 | + SWAGGER_CONFIG["static_url_path"] = SWAGGER_CONFIG["static_url_path"].format( |
| 83 | + application_root=app.config["APPLICATION_ROOT"] |
| 84 | + ) |
| 85 | + SWAGGER_CONFIG["specs_route"] = SWAGGER_CONFIG["specs_route"].format( |
| 86 | + application_root=app.config["APPLICATION_ROOT"] |
| 87 | + ) |
| 88 | + SWAGGER_CONFIG["basePath"] = SWAGGER_CONFIG["basePath"].format( |
| 89 | + application_root=app.config["APPLICATION_ROOT"] |
| 90 | + ) |
| 91 | + Swagger(app, config=SWAGGER_CONFIG) |
| 92 | + |
| 93 | + app.register_blueprint(views_blueprint) |
| 94 | + with app.test_request_context(): |
| 95 | + db.create_all() |
| 96 | + return app, db |
0 commit comments