giga_tcg/main.py
2025-02-05 21:51:22 -05:00

82 lines
2.3 KiB
Python

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
from routes.routes import router
from db.database import init_db, check_db_connection, destroy_db, get_db
from db.utils import db_transaction
from services.task import TaskService
import logging
import sys
from services.tcgplayer import TCGPlayerService, PricingService
from services.product import ProductService
from services.file import FileService
from services.storage import StorageService
from db.models import TCGPlayerGroups
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(sys.stdout),
logging.FileHandler('app.log') # Added this line
]
)
logger = logging.getLogger(__name__)
# Create FastAPI instance
app = FastAPI(
title="Card Management API",
description="API for managing card collections and TCGPlayer integration",
version="1.0.0",
debug=True
)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Modify this in production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include routers
app.include_router(router)
# Optional: Add startup and shutdown events
@app.on_event("startup")
async def startup_event():
# Check database connection
if not check_db_connection():
raise Exception("Database connection failed")
# destroy db
#destroy_db()
# Initialize database
init_db()
# get db session
db = next(get_db())
# populate tcgplayer groups
if db.query(TCGPlayerGroups).count() == 0:
with db_transaction(db):
tcgplayer_service = TCGPlayerService(db)
tcgplayer_service.populate_tcgplayer_groups()
# Start task service
task_service = TaskService(db, ProductService(db, FileService(db), TCGPlayerService(db), StorageService(db)), TCGPlayerService(db))
await task_service.start()
@app.on_event("shutdown")
async def shutdown_event():
# Clean up any connections or resources
pass
# Root endpoint
@app.get("/")
async def root():
return {"message": "Card Management API"}
# Run the application
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)