71 lines
1.8 KiB
Python
71 lines
1.8 KiB
Python
import os
|
|
import sys
|
|
import warnings
|
|
|
|
from flask import Flask
|
|
|
|
from tofu_api.api import TofuApiBlueprint
|
|
from tofu_api.common.config import Config
|
|
from tofu_api.common.json import JSONProvider
|
|
from tofu_api.dependencies import Dependencies
|
|
|
|
# Enable deprecation warnings in dev environment
|
|
if not sys.warnoptions and os.getenv('FLASK_DEBUG'):
|
|
warnings.filterwarnings('default', module='tofu_api.*')
|
|
|
|
|
|
class App(Flask):
|
|
"""
|
|
Flask application for Tofu API.
|
|
"""
|
|
# Override Flask classes
|
|
config_class = Config
|
|
json_provider_class = JSONProvider
|
|
|
|
# Set type hint for config
|
|
config: Config
|
|
|
|
# Dependencies container
|
|
dependencies: Dependencies
|
|
|
|
def __init__(self):
|
|
# Set instance path to the project root directory
|
|
project_root_dir = os.path.abspath('.')
|
|
|
|
# Create and configure the app
|
|
super().__init__(
|
|
'tofu_api',
|
|
instance_path=project_root_dir,
|
|
instance_relative_config=True,
|
|
)
|
|
|
|
# Load app configuration from YAML file
|
|
self.config.from_yaml(os.getenv('FLASK_CONFIG_FILE', default='config.yml'))
|
|
|
|
# Initialize DI container
|
|
self.dependencies = Dependencies()
|
|
|
|
# Initialize dependencies
|
|
self.init_database()
|
|
|
|
# Register blueprints
|
|
self.register_blueprint(TofuApiBlueprint(self))
|
|
|
|
def init_database(self) -> None:
|
|
"""
|
|
Initialize database connection and models.
|
|
"""
|
|
# Initialize SQLAlchemy, create the database engine based on the app config
|
|
db = self.dependencies.get_sqlalchemy()
|
|
db.init_database(self)
|
|
|
|
# Import models to populate the database metadata
|
|
import tofu_api.models # noqa (unused import)
|
|
|
|
|
|
def create_app() -> App:
|
|
"""
|
|
App factory, returns a Flask app object.
|
|
"""
|
|
return App()
|