|
3 | 3 | import logging |
4 | 4 | import os |
5 | 5 |
|
6 | | -from flasgger import Swagger |
7 | | -from flask import Flask |
| 6 | +import connexion |
8 | 7 | from flask_injector import FlaskInjector |
9 | 8 | from injector import Injector |
10 | 9 |
|
|
23 | 22 |
|
24 | 23 | ENVIRONMENT = os.environ.get("ENVIRONMENT", "default") |
25 | 24 |
|
26 | | -SWAGGER_CONFIG = { |
27 | | - "headers": [ |
28 | | - ], |
29 | | - "specs": [ |
30 | | - { |
31 | | - "endpoint": 'apispec_1', |
32 | | - "route": '{application_root}/apispec_1.json', |
33 | | - "rule_filter": lambda rule: True, # all in |
34 | | - "model_filter": lambda tag: True, # all in |
35 | | - } |
36 | | - ], |
37 | | - "info": { |
38 | | - "title": "API ", |
39 | | - "description": "API para...", |
40 | | - "contact": { |
41 | | - "responsibleOrganization": "ME", |
42 | | - "responsibleDeveloper": "Me", |
43 | | - "email": "me@me.com", |
44 | | - }, |
45 | | - "version": "0.0.1" |
46 | | - }, |
47 | | - "securityDefinitions": { |
48 | | - "APIKeyHeader": {"type": "apiKey", "name": "Authorization", "in": "header"}, |
49 | | - }, |
50 | | - "static_url_path": "{application_root}/flasgger_static", |
51 | | - "swagger_ui": True, |
52 | | - "uiversion": 2, |
53 | | - "specs_route": "/apidocs/", |
54 | | - "basePath": "{application_root}" |
55 | | -} |
56 | | - |
57 | | - |
58 | | -class PrefixMiddleware(object): |
59 | | - """Set a prefix path to all routes. This action is needed if you have a stack of microservices and each of them |
60 | | - exist in the same domain but different path. Por example: |
61 | | - * mydomain.com/ms1/ |
62 | | - * mydomain.com/ms2/ |
63 | | - """ |
64 | | - |
65 | | - def __init__(self, app, prefix=''): |
66 | | - self.app = app |
67 | | - self.prefix = prefix |
68 | | - |
69 | | - def __call__(self, environ, start_response): |
70 | | - if environ['PATH_INFO'].startswith(self.prefix): |
71 | | - environ['PATH_INFO'] = environ['PATH_INFO'][len(self.prefix):] |
72 | | - environ['SCRIPT_NAME'] = self.prefix |
73 | | - return self.app(environ, start_response) |
74 | | - elif environ['PATH_INFO'].startswith("/healthcheck"): |
75 | | - return self.app(environ, start_response) |
76 | | - else: |
77 | | - start_response('404', [('Content-Type', 'text/plain')]) |
78 | | - return ["This url does not belong to the app.".encode()] |
79 | | - |
80 | 25 |
|
81 | 26 | def create_app(): |
82 | 27 | """Initialize the Flask app, register blueprints and intialize all libraries like Swagger, database, the trace system... |
83 | 28 | return the app and the database objects. |
84 | 29 | :return: |
85 | 30 | """ |
86 | | - |
87 | | - from project.views import views_bp as views_blueprint |
88 | 31 | environment = os.environ.get("ENVIRONMENT", "default") |
89 | 32 |
|
90 | | - app = Flask(__name__) |
91 | | - app.config.from_object(CONFIG[environment]) |
92 | | - app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=app.config["APPLICATION_ROOT"]) |
| 33 | + app = connexion.App(__name__, specification_dir='./swagger/') |
| 34 | + # app.app.json_encoder = encoder.JSONEncoder |
93 | 35 |
|
94 | | - db.init_app(app) |
| 36 | + app.add_api('swagger.yaml', |
| 37 | + arguments={'title': 'Swagger Example Project'}, |
| 38 | + base_path=CONFIG[environment].APPLICATION_ROOT) |
95 | 39 |
|
96 | | - # Initialize Swagger |
97 | | - SWAGGER_CONFIG["specs"][0]["route"] = SWAGGER_CONFIG["specs"][0]["route"].format( |
98 | | - application_root=app.config["APPLICATION_ROOT"] |
99 | | - ) |
100 | | - SWAGGER_CONFIG["static_url_path"] = SWAGGER_CONFIG["static_url_path"].format( |
101 | | - application_root=app.config["APPLICATION_ROOT"] |
102 | | - ) |
103 | | - SWAGGER_CONFIG["specs_route"] = SWAGGER_CONFIG["specs_route"].format( |
104 | | - application_root=app.config["APPLICATION_ROOT"] |
105 | | - ) |
106 | | - SWAGGER_CONFIG["basePath"] = SWAGGER_CONFIG["basePath"].format( |
107 | | - application_root=app.config["APPLICATION_ROOT"] |
108 | | - ) |
109 | | - Swagger(app, config=SWAGGER_CONFIG) |
| 40 | + application = app.app |
| 41 | + application.config.from_object(CONFIG[environment]) |
| 42 | + db.init_app(application) |
110 | 43 |
|
111 | 44 | # Initialize Blueprints |
112 | | - app.register_blueprint(views_blueprint) |
113 | | - app.register_blueprint(healthcheck_blueprint) |
| 45 | + application.register_blueprint(healthcheck_blueprint) |
114 | 46 |
|
115 | 47 | # Inject Modules |
116 | | - # Inject Modules |
117 | | - if not app.config["TESTING"] and not app.config["DEBUG"]: |
| 48 | + if not application.config["TESTING"] and not application.config["DEBUG"]: |
118 | 49 | log_handler = logging.StreamHandler() |
119 | 50 | formatter = CustomJsonFormatter('(timestamp) (level) (name) (module) (funcName) (lineno) (message)') |
120 | | - formatter.add_service_name(app.config["APP_NAME"]) |
121 | | - tracer = TracerModule(app) |
| 51 | + formatter.add_service_name(application.config["APP_NAME"]) |
| 52 | + tracer = TracerModule(application) |
122 | 53 | injector = Injector([tracer]) |
123 | | - FlaskInjector(app=app, injector=injector) |
| 54 | + FlaskInjector(app=application, injector=injector) |
124 | 55 | formatter.add_trace_span(tracer.tracer) |
125 | 56 | log_handler.setFormatter(formatter) |
126 | | - app.logger.addHandler(log_handler) |
127 | | - app.logger.setLevel(logging.INFO) |
| 57 | + application.logger.addHandler(log_handler) |
| 58 | + application.logger.setLevel(logging.INFO) |
128 | 59 |
|
129 | | - with app.test_request_context(): |
| 60 | + with application.test_request_context(): |
130 | 61 | db.create_all() |
131 | | - return app, db |
| 62 | + |
| 63 | + return application, db |
0 commit comments