Implement custom config class; get SQLAlchemy database URI from config
This commit is contained in:
parent
31df889dcb
commit
04d434b4a3
7 changed files with 68 additions and 13 deletions
2
tofu_api/common/config/__init__.py
Normal file
2
tofu_api/common/config/__init__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
from .config import Config
|
||||
from .defaults import DefaultConfig
|
||||
32
tofu_api/common/config/config.py
Normal file
32
tofu_api/common/config/config.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
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'))
|
||||
13
tofu_api/common/config/defaults.py
Normal file
13
tofu_api/common/config/defaults.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
__all__ = [
|
||||
'DefaultConfig',
|
||||
]
|
||||
|
||||
|
||||
class DefaultConfig:
|
||||
"""
|
||||
This class defines default values for the app configuration.
|
||||
"""
|
||||
|
||||
# Database configuration
|
||||
SQLALCHEMY_DATABASE_URI = None
|
||||
SQLALCHEMY_ECHO = False
|
||||
|
|
@ -5,6 +5,7 @@ from sqlalchemy import MetaData, create_engine
|
|||
from sqlalchemy.engine import Engine
|
||||
from sqlalchemy.orm import Session, scoped_session, sessionmaker
|
||||
|
||||
from tofu_api.common.config import Config
|
||||
from .metadata import metadata_obj
|
||||
|
||||
__all__ = [
|
||||
|
|
@ -23,19 +24,22 @@ class SQLAlchemy:
|
|||
"""
|
||||
Initializes the SQLAlchemy engine.
|
||||
"""
|
||||
self._engine = self._create_engine()
|
||||
self._engine = self._create_engine(app.config)
|
||||
self._scoped_session = self._create_scoped_session()
|
||||
|
||||
@app.teardown_appcontext
|
||||
def shutdown_session(_exception=None):
|
||||
self._scoped_session.remove()
|
||||
|
||||
def _create_engine(self) -> Engine:
|
||||
@staticmethod
|
||||
def _create_engine(config: Config) -> Engine:
|
||||
"""
|
||||
Create the database engine using the app configuration.
|
||||
"""
|
||||
# TODO: Use config
|
||||
return create_engine('sqlite:////tmp/test.db')
|
||||
return create_engine(
|
||||
config.sqlalchemy_database_uri,
|
||||
echo=config.sqlalchemy_echo,
|
||||
)
|
||||
|
||||
def _create_scoped_session(self) -> scoped_session:
|
||||
"""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue