feat: backend project setup with FastAPI structure and dependencies
- Create directory structure per IMPLEMENTATION_PLAN.md Section 12 - Add requirements.txt with FastAPI, SQLAlchemy, CrewAI, etc. - Add core/config.py for environment variable configuration - Add core/database.py for SQLite connection - Add core/security.py for password hashing and JWT - Add FastAPI app entry point (main.py) with all API routers - Add Uvicorn runner (run.py) - Add API route stubs (auth, bots, backtest, simulate, config) - Add db/models.py with SQLAlchemy models - Add db/schemas.py with Pydantic schemas - Add service stubs (ai_agent, backtest, simulate engines) - Add .env.example with all required environment variables - Verify server starts correctly
This commit is contained in:
40
src/backend/app/core/database.py
Normal file
40
src/backend/app/core/database.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import os
|
||||
from sqlalchemy import create_engine, event
|
||||
from sqlalchemy.orm import sessionmaker, declarative_base
|
||||
from sqlalchemy.engine import Engine
|
||||
from .config import get_settings
|
||||
|
||||
settings = get_settings()
|
||||
|
||||
if settings.DATABASE_URL.startswith("sqlite"):
|
||||
db_path = settings.DATABASE_URL.replace("sqlite:///", "")
|
||||
os.makedirs(
|
||||
os.path.dirname(db_path) if os.path.dirname(db_path) else ".", exist_ok=True
|
||||
)
|
||||
|
||||
@event.listens_for(Engine, "connect")
|
||||
def set_sqlite_pragma(dbapi_conn, connection_record):
|
||||
cursor = dbapi_conn.cursor()
|
||||
cursor.execute("PRAGMA foreign_keys=ON")
|
||||
cursor.close()
|
||||
|
||||
|
||||
engine = create_engine(
|
||||
settings.DATABASE_URL,
|
||||
connect_args={"check_same_thread": False}
|
||||
if settings.DATABASE_URL.startswith("sqlite")
|
||||
else {},
|
||||
echo=settings.DEBUG,
|
||||
)
|
||||
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
Reference in New Issue
Block a user