
- Replace DB_DIR class variable with proper DB_PATH string field - Add initialization logic to safely create database directory - Add fallback to in-memory database if directory creation fails - Make database path configurable via environment variable - Fix docstring linting issues
46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
"""Application configuration settings."""
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from pydantic_settings import BaseSettings
|
|
|
|
# Use a safe default path for the database
|
|
DEFAULT_DB_PATH = Path(os.getenv("DB_PATH", "/app/storage/db"))
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
"""Application settings."""
|
|
PROJECT_NAME: str = "Task Manager API"
|
|
PROJECT_DESCRIPTION: str = "A FastAPI-based Task Manager API"
|
|
VERSION: str = "0.1.0"
|
|
API_V1_STR: str = "/api/v1"
|
|
|
|
# Database
|
|
DB_PATH: str = str(DEFAULT_DB_PATH)
|
|
SQLALCHEMY_DATABASE_URL: str = ""
|
|
|
|
model_config = {
|
|
"case_sensitive": True,
|
|
"env_file": ".env",
|
|
}
|
|
|
|
def __init__(self, **kwargs):
|
|
"""Initialize settings and set up database directory and URL.
|
|
|
|
Creates the database directory if it doesn't exist and sets the
|
|
database URL accordingly. Falls back to an in-memory database if
|
|
directory creation fails.
|
|
"""
|
|
super().__init__(**kwargs)
|
|
# Create the DB directory if it doesn't exist
|
|
db_dir = Path(self.DB_PATH)
|
|
try:
|
|
db_dir.mkdir(parents=True, exist_ok=True)
|
|
self.SQLALCHEMY_DATABASE_URL = f"sqlite:///{db_dir}/db.sqlite"
|
|
except (OSError, PermissionError):
|
|
# Fallback to an in-memory database if we can't create the directory
|
|
self.SQLALCHEMY_DATABASE_URL = "sqlite://"
|
|
print("WARNING: Could not create database directory. Using in-memory SQLite database.")
|
|
|
|
|
|
settings = Settings() |