FastAPI backend with a single-strategy async engine, paper and Binance brokers, pure-python indicators, a backtester that shares the engine's portfolio math, and a Vue dashboard with a live WebSocket feed. Adds the Gitea Actions CI/CD workflow that deploys to Traefik via docker_default.
39 lines
1.0 KiB
Python
39 lines
1.0 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Iterator
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import Session, sessionmaker
|
|
|
|
from app.config import REPO_ROOT, get_settings
|
|
from app.models import Base
|
|
|
|
_settings = get_settings()
|
|
|
|
|
|
def _normalize_url(url: str) -> tuple[str, dict]:
|
|
connect_args: dict = {}
|
|
if url.startswith("sqlite"):
|
|
prefix, _, path = url.partition("sqlite:///")
|
|
if path and not path.startswith("/"):
|
|
url = f"{prefix}sqlite:///{REPO_ROOT / path}"
|
|
connect_args = {"check_same_thread": False}
|
|
return url, connect_args
|
|
|
|
|
|
_url, _connect_args = _normalize_url(_settings.database_url)
|
|
engine = create_engine(_url, connect_args=_connect_args, pool_pre_ping=True)
|
|
SessionLocal = sessionmaker(bind=engine, autoflush=False, expire_on_commit=False)
|
|
|
|
|
|
def init_db() -> None:
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
|
|
def get_session() -> Iterator[Session]:
|
|
session = SessionLocal()
|
|
try:
|
|
yield session
|
|
finally:
|
|
session.close()
|