Spaces:
Paused
Paused
Update app/main.py
Browse files- app/main.py +86 -72
app/main.py
CHANGED
|
@@ -1,72 +1,86 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import sys
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
from fastapi
|
| 7 |
-
from .
|
| 8 |
-
from .
|
| 9 |
-
from .
|
| 10 |
-
import
|
| 11 |
-
import
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
-
app.
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import asyncio
|
| 4 |
+
import platform
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from fastapi import FastAPI, HTTPException
|
| 7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
from fastapi.responses import JSONResponse
|
| 9 |
+
from .core.config import settings
|
| 10 |
+
from .api.v1.endpoints import auth, menu, orders, payments, reports
|
| 11 |
+
from .db.database import init_db
|
| 12 |
+
import logging
|
| 13 |
+
|
| 14 |
+
# Set up Python path
|
| 15 |
+
BASE_DIR = Path(__file__).resolve().parent.parent
|
| 16 |
+
sys.path.append(str(BASE_DIR))
|
| 17 |
+
|
| 18 |
+
# Configure Windows-specific event loop policy
|
| 19 |
+
if platform.system() == 'Windows':
|
| 20 |
+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
| 21 |
+
|
| 22 |
+
# Configure detailed logging
|
| 23 |
+
logging.basicConfig(
|
| 24 |
+
level=logging.DEBUG,
|
| 25 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 26 |
+
stream=sys.stdout
|
| 27 |
+
)
|
| 28 |
+
logger = logging.getLogger(__name__)
|
| 29 |
+
|
| 30 |
+
def create_app() -> FastAPI:
|
| 31 |
+
app = FastAPI(
|
| 32 |
+
title=settings.PROJECT_NAME,
|
| 33 |
+
openapi_url=f"{settings.API_V1_STR}/openapi.json"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Configure CORS
|
| 37 |
+
app.add_middleware(
|
| 38 |
+
CORSMiddleware,
|
| 39 |
+
allow_origins=["*"],
|
| 40 |
+
allow_credentials=True,
|
| 41 |
+
allow_methods=["*"],
|
| 42 |
+
allow_headers=["*"]
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Include API routers
|
| 46 |
+
app.include_router(auth.router, prefix=f"{settings.API_V1_STR}/auth", tags=["auth"])
|
| 47 |
+
app.include_router(menu.router, prefix=f"{settings.API_V1_STR}/menu", tags=["menu"])
|
| 48 |
+
app.include_router(orders.router, prefix=f"{settings.API_V1_STR}/orders", tags=["orders"])
|
| 49 |
+
app.include_router(payments.router, prefix=f"{settings.API_V1_STR}/payments", tags=["payments"])
|
| 50 |
+
app.include_router(reports.router, prefix=f"{settings.API_V1_STR}/reports", tags=["reports"])
|
| 51 |
+
|
| 52 |
+
@app.exception_handler(HTTPException)
|
| 53 |
+
async def http_exception_handler(request, exc):
|
| 54 |
+
return JSONResponse(
|
| 55 |
+
status_code=exc.status_code,
|
| 56 |
+
content={"detail": exc.detail}
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
@app.get("/")
|
| 60 |
+
async def root():
|
| 61 |
+
return {"message": "POS Backend API", "version": "1.0.0"}
|
| 62 |
+
|
| 63 |
+
@app.on_event("startup")
|
| 64 |
+
async def startup_event():
|
| 65 |
+
try:
|
| 66 |
+
logger.info("Starting application initialization...")
|
| 67 |
+
await init_db()
|
| 68 |
+
logger.info("Database initialized successfully")
|
| 69 |
+
except Exception as e:
|
| 70 |
+
logger.error(f"Failed to initialize database: {str(e)}", exc_info=True)
|
| 71 |
+
raise
|
| 72 |
+
|
| 73 |
+
return app
|
| 74 |
+
|
| 75 |
+
app = create_app()
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
import uvicorn
|
| 79 |
+
|
| 80 |
+
uvicorn.run(
|
| 81 |
+
"app.main:app",
|
| 82 |
+
host="0.0.0.0",
|
| 83 |
+
port=8000,
|
| 84 |
+
reload=True,
|
| 85 |
+
log_level="info"
|
| 86 |
+
)
|