system-pri/backend/app/__init__.py

39 lines
819 B
Python
Raw Normal View History

2022-05-15 21:20:05 +02:00
import os
from flask import Flask
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
from .utils import import_models
from .api import api_bp
2022-05-15 21:20:05 +02:00
def create_app(config_name: str = None) -> Flask:
if config_name is None:
config_name = os.environ.get("FLASK_ENV")
app = Flask(__name__)
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)
app.register_blueprint(api_bp)
2022-05-15 21:20:05 +02:00
# register commands
app.cli.add_command(startapp)
return app