finish api basic setup
This commit is contained in:
60
api/__init__.py
Normal file
60
api/__init__.py
Normal file
@@ -0,0 +1,60 @@
|
||||
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."""
|
||||
pass
|
||||
|
||||
|
||||
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)
|
||||
Reference in New Issue
Block a user