65 lines
1.6 KiB
Python
65 lines
1.6 KiB
Python
from flask import Flask, Blueprint
|
|
from . import commands, login
|
|
from .settings import ProdConfig, Config
|
|
from .extensions import db, migrate, jwt
|
|
from .exceptions import ApiException
|
|
|
|
|
|
def create_app(config: Config = ProdConfig) -> Flask:
|
|
"""An application factory, as explained here:
|
|
http://flask.pocoo.org/docs/patterns/appfactories/.
|
|
|
|
:param config_object: The configuration object to use.
|
|
"""
|
|
app = Flask(__name__.split('.')[0])
|
|
app.url_map.strict_slashes = False
|
|
app.config.from_object(config)
|
|
register_extensions(app)
|
|
register_blueprints(app)
|
|
register_errorhandlers(app)
|
|
register_shellcontext(app)
|
|
register_commands(app)
|
|
|
|
return app
|
|
|
|
|
|
def register_extensions(app: Flask):
|
|
"""Register Flask extensions."""
|
|
db.init_app(app)
|
|
migrate.init_app(app, db)
|
|
jwt.init_app(app)
|
|
|
|
|
|
def register_blueprints(app: Flask):
|
|
"""Register Flask blueprints."""
|
|
api_blueprint = Blueprint('api', __name__, url_prefix='/api')
|
|
|
|
api_blueprint.register_blueprint(login.views.blueprint, url_prefix='/login')
|
|
|
|
app.register_blueprint(api_blueprint)
|
|
|
|
|
|
def register_errorhandlers(app: Flask):
|
|
def errorHandler(error: ApiException):
|
|
return error.to_response()
|
|
|
|
app.errorhandler(ApiException)(errorHandler)
|
|
pass
|
|
|
|
|
|
def register_shellcontext(app: Flask):
|
|
"""Register shell context objects."""
|
|
def shell_context():
|
|
"""Shell context objects."""
|
|
return {
|
|
'db': db,
|
|
}
|
|
|
|
app.shell_context_processor(shell_context)
|
|
|
|
|
|
def register_commands(app: Flask):
|
|
"""Register Click commands."""
|
|
app.cli.add_command(commands.clean)
|
|
app.cli.add_command(commands.urls)
|