2022-05-15 21:20:05 +02:00
|
|
|
import os
|
|
|
|
|
2022-06-06 21:30:30 +02:00
|
|
|
from apiflask import APIFlask
|
2022-05-15 21:20:05 +02:00
|
|
|
from flask_migrate import Migrate
|
2022-05-17 16:33:34 +02:00
|
|
|
from flask_cors import CORS
|
2022-05-15 21:20:05 +02:00
|
|
|
|
|
|
|
from .config import config
|
|
|
|
from .dependencies import db, ma
|
|
|
|
from .commands.startapp import startapp
|
2022-06-07 19:27:13 +02:00
|
|
|
from .commands.init_db import init_db
|
2022-10-27 19:53:39 +02:00
|
|
|
from .commands.clear_db import clear_db
|
2022-05-15 21:20:05 +02:00
|
|
|
from .utils import import_models
|
2022-05-17 22:16:45 +02:00
|
|
|
from .api import api_bp
|
2022-06-06 21:30:30 +02:00
|
|
|
from .errors import request_entity_too_large, register_error_handlers
|
2022-05-15 21:20:05 +02:00
|
|
|
|
|
|
|
|
2022-10-26 10:33:35 +02:00
|
|
|
def create_app(config_name: str = '') -> APIFlask:
|
2022-05-15 21:20:05 +02:00
|
|
|
if config_name is None:
|
|
|
|
config_name = os.environ.get("FLASK_ENV")
|
|
|
|
|
2022-06-06 21:30:30 +02:00
|
|
|
app = APIFlask(__name__, docs_path='/')
|
2022-05-15 21:20:05 +02:00
|
|
|
app.config.from_object(config.get(config_name) or config.get("development"))
|
|
|
|
|
2022-05-17 16:33:34 +02:00
|
|
|
if app.config['ENABLE_CORS']:
|
|
|
|
CORS(app)
|
|
|
|
|
2022-05-15 21:20:05 +02:00
|
|
|
db.init_app(app)
|
|
|
|
ma.init_app(app)
|
|
|
|
|
|
|
|
if config_name != "production":
|
|
|
|
with app.app_context():
|
|
|
|
import_models()
|
|
|
|
|
|
|
|
Migrate(app, db)
|
|
|
|
|
2022-05-17 22:16:45 +02:00
|
|
|
app.register_blueprint(api_bp)
|
2022-05-15 21:20:05 +02:00
|
|
|
|
|
|
|
# register commands
|
|
|
|
app.cli.add_command(startapp)
|
2022-06-07 19:27:13 +02:00
|
|
|
app.cli.add_command(init_db)
|
2022-10-27 19:53:39 +02:00
|
|
|
app.cli.add_command(clear_db)
|
2022-05-15 21:20:05 +02:00
|
|
|
|
2022-05-19 18:15:11 +02:00
|
|
|
# register errors
|
2022-06-06 21:30:30 +02:00
|
|
|
register_error_handlers(app)
|
|
|
|
# app.register_error_handler(413, request_entity_too_large)
|
2022-05-19 18:15:11 +02:00
|
|
|
|
2022-05-15 21:20:05 +02:00
|
|
|
return app
|