- Created docker-compose.yml with 4 services: - postgres: PostgreSQL 16 database with persistent volume - redis: Redis 7 message broker - app: Flask web application (port 5000) - celery: Celery worker for async downloads - Created Dockerfile with Python 3.14, FFmpeg, and uv - Added psycopg2-binary dependency for PostgreSQL driver - Updated database.py to use DATABASE_URL environment variable - Supports PostgreSQL in production - Falls back to SQLite for local development - Updated celery_app.py to use environment variables: - CELERY_BROKER_URL and CELERY_RESULT_BACKEND - Created .env.example with all configuration variables - Created .dockerignore to optimize Docker builds - Updated .gitignore to exclude .env and Docker files - Updated CLAUDE.md with comprehensive Docker documentation: - Quick start with docker-compose commands - Environment variable configuration - Local development setup instructions - Service architecture overview All services have health checks and automatic restart configured. Start entire stack with: docker-compose up 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
78 lines
1.9 KiB
Python
78 lines
1.9 KiB
Python
"""Database configuration and session management."""
|
|
|
|
import os
|
|
from contextlib import contextmanager
|
|
from typing import Generator
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker, Session
|
|
|
|
from models import Base
|
|
|
|
|
|
# Database configuration from environment variable
|
|
# Falls back to SQLite for local development if DATABASE_URL not set
|
|
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///yottob.db")
|
|
|
|
# Create engine with appropriate configuration
|
|
engine_kwargs = {
|
|
"echo": False, # Set to True for SQL query logging
|
|
}
|
|
|
|
# SQLite-specific configuration
|
|
if DATABASE_URL.startswith("sqlite"):
|
|
engine_kwargs["connect_args"] = {"check_same_thread": False}
|
|
|
|
engine = create_engine(DATABASE_URL, **engine_kwargs)
|
|
|
|
# Session factory
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
def init_db() -> None:
|
|
"""Initialize the database by creating all tables.
|
|
|
|
This function should be called when the application starts.
|
|
It creates all tables defined in the models if they don't exist.
|
|
"""
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
|
|
@contextmanager
|
|
def get_db_session() -> Generator[Session, None, None]:
|
|
"""Context manager for database sessions.
|
|
|
|
Usage:
|
|
with get_db_session() as session:
|
|
# Use session here
|
|
session.query(...)
|
|
|
|
The session is automatically committed on success and rolled back on error.
|
|
"""
|
|
session = SessionLocal()
|
|
try:
|
|
yield session
|
|
session.commit()
|
|
except Exception:
|
|
session.rollback()
|
|
raise
|
|
finally:
|
|
session.close()
|
|
|
|
|
|
def get_db() -> Generator[Session, None, None]:
|
|
"""Dependency function for Flask routes to get a database session.
|
|
|
|
Usage in Flask:
|
|
session = next(get_db())
|
|
try:
|
|
# Use session
|
|
finally:
|
|
session.close()
|
|
"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|