60 lines
1.6 KiB
Python
60 lines
1.6 KiB
Python
from fastapi import FastAPI, Depends, HTTPException
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy import text
|
|
from app.database import get_db, engine
|
|
from app import models, seed
|
|
from app.routers import auth, users, chests, profiles
|
|
from app.config import get_settings
|
|
|
|
from app.middleware import IdempotencyMiddleware
|
|
|
|
settings = get_settings()
|
|
|
|
# Create all tables
|
|
models.Base.metadata.create_all(bind=engine)
|
|
|
|
app = FastAPI(title="Card Game Backend")
|
|
|
|
# CORS Middleware (before other middleware)
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.cors_origins,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
app.add_middleware(IdempotencyMiddleware)
|
|
|
|
app.include_router(auth.router)
|
|
app.include_router(users.router)
|
|
app.include_router(chests.router)
|
|
app.include_router(profiles.router)
|
|
|
|
@app.on_event("startup")
|
|
def startup_event():
|
|
db = next(get_db())
|
|
seed.seed_data(db)
|
|
|
|
@app.get("/health")
|
|
def health_check():
|
|
return {"status": "ok"}
|
|
|
|
@app.get("/catalog/cards")
|
|
def get_cards(db: Session = Depends(get_db)):
|
|
return db.query(models.Card).all()
|
|
|
|
@app.get("/catalog/chests")
|
|
def get_chests(db: Session = Depends(get_db)):
|
|
return db.query(models.Chest).all()
|
|
|
|
@app.get("/db-check")
|
|
def db_check(db: Session = Depends(get_db)):
|
|
try:
|
|
# Execute simple query to check connection
|
|
db.execute(text("SELECT 1"))
|
|
return {"db": "ok"}
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Database connection failed: {str(e)}")
|