-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.py
49 lines (39 loc) · 1.36 KB
/
factory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
from flask import Flask
from flask_cors import CORS
from flask_marshmallow import Marshmallow
ma: Marshmallow = Marshmallow()
class FlaskAppFactory(object):
""" A Factory class to create a configured Flask REST application object. """
@classmethod
def create_app(cls, config_env='config.Config') -> Flask:
"""
Create a Flask application object with the specified configs, extensions, and blueprints.
"""
app = Flask(__name__)
environment_config: str = os.environ.get('CONFIGURATION_ENV', config_env)
app.config.from_object(environment_config)
CORS(app)
cls.initialize_extensions(app)
cls.register_blueprints(app)
return app
@classmethod
def initialize_extensions(
cls,
app: Flask
) -> None:
"""
Bind each Flask extension instance to the Flask application instance.
"""
ma.init_app(app=app)
@classmethod
def register_blueprints(
cls,
app: Flask
) -> None:
"""
Register Blueprints for appropriate route management.
Without Blueprints, Flask won't be able to resolve the endpoints outside of the file containing the Application.
Override this method to register each Blueprint with a specific Flask application instance.
"""
pass