Compare commits
No commits in common. "4b9d973f40348e6070071fef2ac2c912693179c0" and "31df889dcb101fade4287c6a432027a606594fa9" have entirely different histories.
4b9d973f40
...
31df889dcb
|
|
@ -1,6 +1 @@
|
||||||
# Secret key (CHANGE TO RANDOM STRING IN PRODUCTION!)
|
|
||||||
SECRET_KEY: 'development'
|
SECRET_KEY: 'development'
|
||||||
|
|
||||||
# Database configuration
|
|
||||||
SQLALCHEMY_DATABASE_URI: 'sqlite:////tmp/test.db'
|
|
||||||
SQLALCHEMY_ECHO: false
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,3 @@
|
||||||
FLASK_APP=tofu_api.app
|
FLASK_APP=tofu_api.app
|
||||||
FLASK_ENV=development
|
FLASK_ENV=development
|
||||||
FLASK_CONFIG_FILE=config.dev.yml
|
FLASK_CONFIG_FILE=config.dev.yml
|
||||||
|
|
||||||
# Show SQLAlchemy 2.0 deprecation warnings
|
|
||||||
SQLALCHEMY_WARN_20=1
|
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,17 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
import warnings
|
|
||||||
|
|
||||||
|
import yaml
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
|
||||||
from tofu_api.api import TofuApiBlueprint
|
from tofu_api.api import TofuApiBlueprint
|
||||||
from tofu_api.common.config import Config
|
from tofu_api.config import DefaultConfig
|
||||||
from tofu_api.dependencies import Dependencies
|
from tofu_api.dependencies import Dependencies
|
||||||
|
|
||||||
# Enable deprecation warnings in dev environment
|
|
||||||
if not sys.warnoptions and os.getenv('FLASK_ENV') == 'development':
|
|
||||||
warnings.filterwarnings('default', module='tofu_api.*')
|
|
||||||
|
|
||||||
|
|
||||||
class App(Flask):
|
class App(Flask):
|
||||||
"""
|
"""
|
||||||
Flask application for Tofu API.
|
Flask application for Tofu API.
|
||||||
"""
|
"""
|
||||||
# Override Flask classes
|
|
||||||
config_class = Config
|
|
||||||
|
|
||||||
# Set type hint for config
|
|
||||||
config: Config
|
|
||||||
|
|
||||||
# Dependencies container
|
# Dependencies container
|
||||||
dependencies: Dependencies
|
dependencies: Dependencies
|
||||||
|
|
||||||
|
|
@ -36,9 +25,10 @@ class App(Flask):
|
||||||
instance_path=project_root_dir,
|
instance_path=project_root_dir,
|
||||||
instance_relative_config=True,
|
instance_relative_config=True,
|
||||||
)
|
)
|
||||||
|
self.config.from_object(DefaultConfig)
|
||||||
|
|
||||||
# Load app configuration from YAML file
|
# Load app configuration from YAML file
|
||||||
self.config.from_yaml(os.getenv('FLASK_CONFIG_FILE', default='config.yml'))
|
self.config.from_file(os.getenv('FLASK_CONFIG_FILE', default='config.yml'), load=yaml.safe_load)
|
||||||
|
|
||||||
# Initialize DI container
|
# Initialize DI container
|
||||||
self.dependencies = Dependencies()
|
self.dependencies = Dependencies()
|
||||||
|
|
|
||||||
|
|
@ -1,2 +0,0 @@
|
||||||
from .config import Config
|
|
||||||
from .defaults import DefaultConfig
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
import yaml
|
|
||||||
from flask import Config as BaseConfig
|
|
||||||
|
|
||||||
from .defaults import DefaultConfig
|
|
||||||
|
|
||||||
|
|
||||||
class Config(BaseConfig):
|
|
||||||
"""
|
|
||||||
Custom config class for the Flask application.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
|
|
||||||
# Set app-specific default values
|
|
||||||
self.from_object(DefaultConfig)
|
|
||||||
|
|
||||||
def from_yaml(self, filename: str, *, silent: bool = False) -> bool:
|
|
||||||
"""
|
|
||||||
Update config from a YAML file.
|
|
||||||
"""
|
|
||||||
return self.from_file(filename, load=yaml.safe_load, silent=silent)
|
|
||||||
|
|
||||||
# Properties for type safe access to config values
|
|
||||||
|
|
||||||
@property
|
|
||||||
def sqlalchemy_database_uri(self) -> str:
|
|
||||||
return self.get('SQLALCHEMY_DATABASE_URI')
|
|
||||||
|
|
||||||
@property
|
|
||||||
def sqlalchemy_echo(self) -> bool:
|
|
||||||
return bool(self.get('SQLALCHEMY_ECHO'))
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
__all__ = [
|
|
||||||
'DefaultConfig',
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
class DefaultConfig:
|
|
||||||
"""
|
|
||||||
This class defines default values for the app configuration.
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Database configuration
|
|
||||||
SQLALCHEMY_DATABASE_URI = None
|
|
||||||
SQLALCHEMY_ECHO = False
|
|
||||||
|
|
@ -5,7 +5,6 @@ from sqlalchemy import MetaData, create_engine
|
||||||
from sqlalchemy.engine import Engine
|
from sqlalchemy.engine import Engine
|
||||||
from sqlalchemy.orm import Session, scoped_session, sessionmaker
|
from sqlalchemy.orm import Session, scoped_session, sessionmaker
|
||||||
|
|
||||||
from tofu_api.common.config import Config
|
|
||||||
from .metadata import metadata_obj
|
from .metadata import metadata_obj
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
|
|
@ -24,22 +23,19 @@ class SQLAlchemy:
|
||||||
"""
|
"""
|
||||||
Initializes the SQLAlchemy engine.
|
Initializes the SQLAlchemy engine.
|
||||||
"""
|
"""
|
||||||
self._engine = self._create_engine(app.config)
|
self._engine = self._create_engine()
|
||||||
self._scoped_session = self._create_scoped_session()
|
self._scoped_session = self._create_scoped_session()
|
||||||
|
|
||||||
@app.teardown_appcontext
|
@app.teardown_appcontext
|
||||||
def shutdown_session(_exception=None):
|
def shutdown_session(_exception=None):
|
||||||
self._scoped_session.remove()
|
self._scoped_session.remove()
|
||||||
|
|
||||||
@staticmethod
|
def _create_engine(self) -> Engine:
|
||||||
def _create_engine(config: Config) -> Engine:
|
|
||||||
"""
|
"""
|
||||||
Create the database engine using the app configuration.
|
Create the database engine using the app configuration.
|
||||||
"""
|
"""
|
||||||
return create_engine(
|
# TODO: Use config
|
||||||
config.sqlalchemy_database_uri,
|
return create_engine('sqlite:////tmp/test.db')
|
||||||
echo=config.sqlalchemy_echo,
|
|
||||||
)
|
|
||||||
|
|
||||||
def _create_scoped_session(self) -> scoped_session:
|
def _create_scoped_session(self) -> scoped_session:
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
class DefaultConfig:
|
||||||
|
"""
|
||||||
|
This class defined default values for the app configuration.
|
||||||
|
"""
|
||||||
|
# TODO
|
||||||
Loading…
Reference in New Issue