93 lines
2.5 KiB
Python
93 lines
2.5 KiB
Python
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
import uvicorn
|
|
from app.routes.routes import router
|
|
from app.db.database import init_db, check_db_connection, get_db
|
|
import logging
|
|
import sys
|
|
|
|
# Import your dependency functions
|
|
from app.dependencies import (
|
|
get_task_service,
|
|
get_tcgplayer_service,
|
|
get_pricing_service,
|
|
get_file_service,
|
|
get_product_service,
|
|
get_storage_service,
|
|
get_inventory_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)
|
|
inventory_service = get_inventory_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, pricing_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) |