Desk-Back2 / main.py
Fred808's picture
Update main.py
bc44233 verified
raw
history blame
2.22 kB
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.db.database import init_db # Direct import from database.py
from app.api.v1.endpoints import auth, menu, orders, payments, reports
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(auth.router, prefix=f"{settings.API_V1_STR}/auth")
app.include_router(menu.router, prefix=f"{settings.API_V1_STR}/menu")
app.include_router(orders.router, prefix=f"{settings.API_V1_STR}/orders")
app.include_router(payments.router, prefix=f"{settings.API_V1_STR}/payments")
app.include_router(reports.router, prefix=f"{settings.API_V1_STR}/reports")
@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")