75 lines
1.9 KiB
Python
75 lines
1.9 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
|
|
import logging
|
|
import sys
|
|
from services.tcgplayer import TCGPlayerService, PricingService
|
|
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, PricingService(db))
|
|
tcgplayer_service.populate_tcgplayer_groups()
|
|
|
|
|
|
@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) |