Python FastAPI ML backend kept intact; auth replaced with central hnf_session cookie verification. Frontend rebuilt on React 18 + TS + Vite with stack design system, Plotly charts retained. Shared Postgres via DATABASE_URL; schema applied on startup. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
"""
|
|
Database connection and session management
|
|
"""
|
|
import os
|
|
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
|
from sqlalchemy.orm import sessionmaker, declarative_base
|
|
from sqlalchemy import create_engine
|
|
|
|
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://forecast:forecast_secret@localhost:5432/forecast")
|
|
|
|
# Convert to async URL
|
|
ASYNC_DATABASE_URL = DATABASE_URL.replace("postgresql://", "postgresql+asyncpg://")
|
|
|
|
# Async engine for FastAPI
|
|
async_engine = create_async_engine(ASYNC_DATABASE_URL, echo=False)
|
|
AsyncSessionLocal = sessionmaker(
|
|
async_engine, class_=AsyncSession, expire_on_commit=False
|
|
)
|
|
|
|
# Sync engine for scheduler jobs and migrations
|
|
sync_engine = create_engine(DATABASE_URL)
|
|
SyncSessionLocal = sessionmaker(bind=sync_engine)
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
async def get_db():
|
|
"""Dependency for FastAPI endpoints"""
|
|
async with AsyncSessionLocal() as session:
|
|
try:
|
|
yield session
|
|
finally:
|
|
await session.close()
|
|
|
|
|
|
def get_sync_db():
|
|
"""Get sync session for scheduler jobs"""
|
|
db = SyncSessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|