Implement milestone M2 (backend setup):
- FastAPI app factory (core/app.py) with CORS, lifespan table
creation, and global error handlers using a standardized
{detail, code, errors?} envelope
- Endpoints: GET /health, GET /api/v1/health, GET /api/v1/version
- SQLAlchemy 2.0 ORM: Article, Author, Category models with
relationships, unique constraints, and FK pragma for SQLite
- Pydantic v2 schemas (create/update/read) for all models
- Env-driven config (DATABASE_URL, CORS_ORIGINS) + .env.example
- pytest suite (19 tests, ~95% coverage) with per-test SQLite
databases via fixtures
- Dockerfile (python:3.11-slim) and docker-compose.yml with a
persistent ./data/db volume and healthcheck
46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
"""Application configuration.
|
|
|
|
Settings are read from environment variables so the same code runs
|
|
locally, in Docker (see ``docker-compose.yml``) and in CI.
|
|
See ``.env.example`` for the documented variable set.
|
|
"""
|
|
|
|
import os
|
|
from functools import lru_cache
|
|
|
|
# --- Application metadata -------------------------------------------------
|
|
APP_NAME = "newzy"
|
|
APP_VERSION = "0.1.0"
|
|
API_V1_PREFIX = "/api/v1"
|
|
|
|
# Default location of the SQLite database file (relative to the working
|
|
# directory). In Docker this resolves inside the mounted volume
|
|
# (``./data/db`` -> ``/app/data/db`` on the host's ``./data/db``).
|
|
DEFAULT_DATABASE_URL = "sqlite:///./data/db/newzy.db"
|
|
|
|
# Default origins allowed by CORS (frontend dev servers).
|
|
DEFAULT_CORS_ORIGINS = "http://localhost:3000,http://127.0.0.1:3000"
|
|
|
|
|
|
class Settings:
|
|
"""Runtime settings loaded from the environment."""
|
|
|
|
def __init__(self) -> None:
|
|
self.database_url: str = os.getenv("DATABASE_URL", DEFAULT_DATABASE_URL)
|
|
self.cors_origins: list[str] = [
|
|
origin.strip()
|
|
for origin in os.getenv("CORS_ORIGINS", DEFAULT_CORS_ORIGINS).split(",")
|
|
if origin.strip()
|
|
]
|
|
|
|
@property
|
|
def is_sqlite(self) -> bool:
|
|
"""True when the configured database is SQLite."""
|
|
return self.database_url.startswith("sqlite")
|
|
|
|
|
|
@lru_cache
|
|
def get_settings() -> Settings:
|
|
"""Return the cached application settings."""
|
|
return Settings()
|