-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
62 lines (46 loc) · 1.54 KB
/
main.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
50
51
52
53
54
55
56
57
58
59
60
61
62
# System Imports
from os import getenv
# Third party imports
from flask import Flask, jsonify
from flask_restplus import Api
from flask_migrate import Migrate
from flask_marshmallow import Marshmallow
from flask_bcrypt import Bcrypt
# Local ImportS
from config import config
from api import api_blueprint
from api.models.database import db
from api.middleware.base_validation import middleware_blueprint, ValidationError
config_name = getenv('FLASK_ENV', default='production')
api = Api(api_blueprint, doc=False)
def initialize_blueprint(application):
application.register_blueprint(api_blueprint)
application.register_blueprint(middleware_blueprint)
migrate = Migrate()
bcrypt = Bcrypt()
def create_app(config=config.get(config_name)):
"""Return app object given config object."""
app = Flask(__name__)
app.config.from_object(config)
initialize_blueprint(app)
app.url_map.strict_slashes = False
bcrypt.init_app(app)
# initialize database
db.init_app(app)
# initialize migration app
migrate.init_app(app,db)
# initialize marshmallow
from api.schema.schema import ma
ma.init_app(app)
# import all models
from api.models import usermodel
# import views
import api.views
return app
@api.errorhandler(ValidationError)
@middleware_blueprint.app_errorhandler(ValidationError)
def handle_exception(error):
"""Error handler called when a ValidationError Exception is raised"""
response = jsonify(error.to_dict())
response.status_code = error.status_code
return response