- Bump version to 0.5.0-beta - Add Apache-2.0 LICENSE (official text) + NOTICE - Add Docker support: multi-stage Dockerfile + docker-compose * non-root user, tini PID 1, gunicorn + uvicorn workers * healthcheck, OCI labels, persistent /data volume - Add GitHub Actions CI (lint/import-check + Docker smoke-test) - Backend polish: * modern lifespan handler (replaces deprecated on_event) * explicit sort validation (400 instead of 500) * title min_length=1 schema validation * configurable data dir + http timeout via env * avg_rating helper extracted (DRY) - Frontend polish: * beta badge in title, footer with live version * loading spinner on initial fetch * updated README, badges, config table, roadmap
47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
"""Datenbank-Engine und Session-Verwaltung."""
|
||
# SPDX-License-Identifier: Apache-2.0
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
from pathlib import Path
|
||
|
||
from sqlalchemy import create_engine
|
||
from sqlalchemy.orm import DeclarativeBase, sessionmaker
|
||
|
||
# SQLite-Datei: überschreibbar via WATCHSTACK_DATA_DIR (für Docker)
|
||
_BASE_DIR = Path(__file__).resolve().parent.parent
|
||
DATA_DIR = Path(os.environ.get("WATCHSTACK_DATA_DIR", _BASE_DIR / "data"))
|
||
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
||
DB_PATH = DATA_DIR / "watchstack.db"
|
||
|
||
DATABASE_URL = f"sqlite:///{DB_PATH}"
|
||
|
||
engine = create_engine(
|
||
DATABASE_URL,
|
||
echo=False,
|
||
future=True,
|
||
connect_args={"check_same_thread": False},
|
||
)
|
||
|
||
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False, expire_on_commit=False)
|
||
|
||
|
||
class Base(DeclarativeBase):
|
||
"""Basisklasse für alle ORM-Modelle."""
|
||
|
||
|
||
def get_db():
|
||
"""FastAPI-Dependency: liefert eine Session, schließt sie danach."""
|
||
db = SessionLocal()
|
||
try:
|
||
yield db
|
||
finally:
|
||
db.close()
|
||
|
||
|
||
def init_db() -> None:
|
||
"""Erstellt alle Tabellen, falls noch nicht vorhanden."""
|
||
from app import models # noqa: F401 – registriert Modelle bei Base.metadata
|
||
|
||
Base.metadata.create_all(bind=engine)
|