Fred808 commited on
Commit
a9082d9
·
verified ·
1 Parent(s): a5cecb5

Update app/app.py

Browse files
Files changed (1) hide show
  1. app/app.py +70 -70
app/app.py CHANGED
@@ -1,71 +1,71 @@
1
- import asyncio
2
- import platform
3
- from fastapi import FastAPI, HTTPException
4
- from fastapi.middleware.cors import CORSMiddleware
5
- from fastapi.responses import JSONResponse
6
- from app.core.config import settings
7
- from app.api.api_v1.api import api_router
8
- from app.db.database import init_db
9
- import logging
10
- import sys
11
-
12
- # Configure Windows-specific event loop policy
13
- if platform.system() == 'Windows':
14
- asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
15
-
16
- # Configure detailed logging
17
- logging.basicConfig(
18
- level=logging.DEBUG,
19
- format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
20
- stream=sys.stdout
21
- )
22
- logger = logging.getLogger(__name__)
23
-
24
- app = FastAPI(
25
- title=settings.PROJECT_NAME,
26
- openapi_url=f"{settings.API_V1_STR}/openapi.json"
27
- )
28
-
29
- # Configure CORS
30
- app.add_middleware(
31
- CORSMiddleware,
32
- allow_origins=["*"],
33
- allow_credentials=True,
34
- allow_methods=["*"],
35
- allow_headers=["*"]
36
- )
37
-
38
- # Include API router
39
- app.include_router(api_router, prefix=settings.API_V1_STR)
40
-
41
- @app.exception_handler(HTTPException)
42
- async def http_exception_handler(request, exc):
43
- return JSONResponse(
44
- status_code=exc.status_code,
45
- content={"detail": exc.detail}
46
- )
47
-
48
- @app.get("/")
49
- async def root():
50
- return {"message": "POS Backend API", "version": "1.0.0"}
51
-
52
- @app.on_event("startup")
53
- async def startup_event():
54
- try:
55
- logger.info("Starting application initialization...")
56
- await init_db()
57
- logger.info("Database initialized successfully")
58
- except Exception as e:
59
- logger.error(f"Failed to initialize database: {str(e)}", exc_info=True)
60
- raise
61
-
62
- if __name__ == "__main__":
63
- import uvicorn
64
- logger.info("Starting application with Windows-compatible event loop...")
65
- uvicorn.run(
66
- "app:app",
67
- host="0.0.0.0",
68
- port=7860,
69
- reload=True,
70
- log_level="debug"
71
  )
 
1
+ import asyncio
2
+ import platform
3
+ from fastapi import FastAPI, HTTPException
4
+ from fastapi.middleware.cors import CORSMiddleware
5
+ from fastapi.responses import JSONResponse
6
+ from app.core.config import settings
7
+ from app.db.database import init_db # Direct import from database.py
8
+ from app.api.api_v1.api import api_router
9
+ import logging
10
+ import sys
11
+
12
+ # Configure Windows-specific event loop policy
13
+ if platform.system() == 'Windows':
14
+ asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
15
+
16
+ # Configure detailed logging
17
+ logging.basicConfig(
18
+ level=logging.DEBUG,
19
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
20
+ stream=sys.stdout
21
+ )
22
+ logger = logging.getLogger(__name__)
23
+
24
+ app = FastAPI(
25
+ title=settings.PROJECT_NAME,
26
+ openapi_url=f"{settings.API_V1_STR}/openapi.json"
27
+ )
28
+
29
+ # Configure CORS
30
+ app.add_middleware(
31
+ CORSMiddleware,
32
+ allow_origins=["*"],
33
+ allow_credentials=True,
34
+ allow_methods=["*"],
35
+ allow_headers=["*"]
36
+ )
37
+
38
+ # Include API router
39
+ app.include_router(api_router, prefix=settings.API_V1_STR)
40
+
41
+ @app.exception_handler(HTTPException)
42
+ async def http_exception_handler(request, exc):
43
+ return JSONResponse(
44
+ status_code=exc.status_code,
45
+ content={"detail": exc.detail}
46
+ )
47
+
48
+ @app.get("/")
49
+ async def root():
50
+ return {"message": "POS Backend API", "version": "1.0.0"}
51
+
52
+ @app.on_event("startup")
53
+ async def startup_event():
54
+ try:
55
+ logger.info("Starting application initialization...")
56
+ await init_db()
57
+ logger.info("Database initialized successfully")
58
+ except Exception as e:
59
+ logger.error(f"Failed to initialize database: {str(e)}", exc_info=True)
60
+ raise
61
+
62
+ if __name__ == "__main__":
63
+ import uvicorn
64
+ logger.info("Starting application with Windows-compatible event loop...")
65
+ uvicorn.run(
66
+ "app:app",
67
+ host="0.0.0.0",
68
+ port=7860,
69
+ reload=True,
70
+ log_level="debug"
71
  )