giga_tcg/main.py
2025-02-07 13:52:28 -05:00

94 lines
2.6 KiB
Python

from fastapi import FastAPI, Depends
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
import logging
import sys
from db.models import TCGPlayerGroups
from sqlalchemy.orm import Session
# Import your dependency functions
from dependencies import (
get_task_service,
get_tcgplayer_service,
get_pricing_service,
get_file_service,
get_product_service,
get_storage_service,
)
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(sys.stdout),
logging.FileHandler('app.log')
]
)
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)
@app.on_event("startup")
async def startup_event():
try:
# Check database connection
if not check_db_connection():
logger.error("Database connection failed")
raise Exception("Database connection failed")
# Initialize database
init_db()
# Get database session
db = next(get_db())
# Use dependency injection to get services
file_service = get_file_service(db)
storage_service = get_storage_service(db)
tcgplayer_service = get_tcgplayer_service(db, file_service)
pricing_service = get_pricing_service(db, file_service, tcgplayer_service)
product_service = get_product_service(db, file_service, tcgplayer_service, storage_service)
task_service = get_task_service(db, product_service, tcgplayer_service)
# Start task service
await task_service.start()
logger.info("Application started successfully")
except Exception as e:
logger.error(f"Startup failed: {str(e)}")
raise
@app.on_event("shutdown")
async def shutdown_event():
logger.info("Application shutting down")
pass
@app.get("/")
async def root():
return {"message": "Card Management API"}
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)