Files
trading-journal/backend/app.py

53 lines
1.5 KiB
Python
Raw Normal View History

2025-09-22 14:33:32 +02:00
import asyncio
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
2025-09-11 18:24:36 +00:00
2025-09-22 17:35:10 +02:00
from fastapi import FastAPI, HTTPException, Request
2025-09-11 18:24:36 +00:00
2025-09-22 14:33:32 +02:00
import settings
2025-09-22 17:35:10 +02:00
from trading_journal import db, service
from trading_journal.db import Database
from trading_journal.dto import UserCreate, UserRead
2025-09-11 18:24:36 +00:00
2025-09-22 14:33:32 +02:00
_db = db.create_database(settings.settings.database_url)
2025-09-11 18:24:36 +00:00
2025-09-22 14:33:32 +02:00
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]: # noqa: ARG001
await asyncio.to_thread(_db.init_db)
try:
yield
finally:
await asyncio.to_thread(_db.dispose)
2025-09-11 18:24:36 +00:00
2025-09-22 14:33:32 +02:00
app = FastAPI(lifespan=lifespan)
2025-09-22 17:35:10 +02:00
app.add_middleware(service.AuthMiddleWare)
app.state.db_factory = _db
2025-09-11 18:24:36 +00:00
2025-09-22 17:35:10 +02:00
@app.get(f"{settings.settings.api_base}/status")
2025-09-22 14:33:32 +02:00
async def get_status() -> dict[str, str]:
return {"status": "ok"}
2025-09-22 17:35:10 +02:00
@app.post(f"{settings.settings.api_base}/register")
async def register_user(request: Request, user_in: UserCreate) -> UserRead:
db_factory: Database = request.app.state.db_factory
def sync_work() -> UserRead:
with db_factory.get_session_ctx_manager() as db:
return service.register_user_service(db, user_in)
try:
return await asyncio.to_thread(sync_work)
except service.UserAlreadyExistsError as e:
raise HTTPException(status_code=400, detail=str(e)) from e
except Exception as e:
raise HTTPException(status_code=500, detail="Internal server error" + str(e)) from e
@app.get(f"{settings.settings.api_base}/trades")
async def get_trades() -> dict[str, str]:
return {"trades": []}