Desk-Back2 / app.py
Fred808's picture
Update app.py
edb8ef2 verified
import asyncio
import platform
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from app.core.config import settings
from app.api.api_v1.api import api_router
from app.db.database import init_db
import logging
import sys
# Configure Windows-specific event loop policy
if platform.system() == 'Windows':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
# Configure detailed logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
stream=sys.stdout
)
logger = logging.getLogger(__name__)
app = FastAPI(
title=settings.PROJECT_NAME,
openapi_url=f"{settings.API_V1_STR}/openapi.json"
)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)
# Include API router
app.include_router(api_router, prefix=settings.API_V1_STR)
@app.exception_handler(HTTPException)
async def http_exception_handler(request, exc):
return JSONResponse(
status_code=exc.status_code,
content={"detail": exc.detail}
)
@app.get("/")
async def root():
return {"message": "POS Backend API", "version": "1.0.0"}
@app.on_event("startup")
async def startup_event():
try:
logger.info("Starting application initialization...")
await init_db()
logger.info("Database initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize database: {str(e)}", exc_info=True)
raise
if __name__ == "__main__":
import uvicorn
logger.info("Starting application with Windows-compatible event loop...")
uvicorn.run("main:app", host="0.0.0.0", port=7860, reload=True, log_level="debug")