45 lines
1018 B
Python
45 lines
1018 B
Python
import os
|
|
|
|
from apiflask import APIFlask
|
|
from flask_migrate import Migrate
|
|
from flask_cors import CORS
|
|
|
|
from .config import config
|
|
from .dependencies import db, ma
|
|
from .commands.startapp import startapp
|
|
from .commands.clear_db import clear_db
|
|
from .utils import import_models
|
|
from .api import api_bp
|
|
from .errors import register_error_handlers
|
|
|
|
|
|
def create_app(config_name: str = '') -> APIFlask:
|
|
if config_name is None:
|
|
config_name = os.environ.get("FLASK_ENV")
|
|
|
|
app = APIFlask(__name__, docs_path='/')
|
|
app.config.from_object(config.get(config_name) or config.get("development"))
|
|
|
|
if app.config['ENABLE_CORS']:
|
|
CORS(app)
|
|
|
|
db.init_app(app)
|
|
ma.init_app(app)
|
|
|
|
if config_name != "production":
|
|
with app.app_context():
|
|
import_models()
|
|
|
|
Migrate(app, db)
|
|
|
|
app.register_blueprint(api_bp)
|
|
|
|
# register commands
|
|
app.cli.add_command(startapp)
|
|
app.cli.add_command(clear_db)
|
|
|
|
# register errors
|
|
register_error_handlers(app)
|
|
|
|
return app
|