CORS fix
Browse files
main.py
CHANGED
@@ -9,41 +9,36 @@ from App.routers.bonds.routes import router as bonds_router
|
|
9 |
from App.routers.tasks.routes import router as tasks_router
|
10 |
from App.routers.users.routes import router as users_router
|
11 |
from App.routers.portfolio.routes import router as portfolio_router
|
12 |
-
from App.schemas import ResponseModel,AppException
|
13 |
|
14 |
-
from db import init_db, close_db,clear_db
|
15 |
|
16 |
app = FastAPI(title="Uwekezaji API", description="Stock Market Data API")
|
17 |
|
18 |
|
19 |
-
|
20 |
@app.exception_handler(AppException)
|
21 |
async def custom_http_exception_handler(request: Request, exc: AppException):
|
22 |
return JSONResponse(
|
23 |
status_code=exc.status_code,
|
24 |
-
content=ResponseModel(
|
25 |
-
success=False,
|
26 |
-
message=exc.detail,
|
27 |
-
data=exc.data
|
28 |
-
).dict()
|
29 |
)
|
30 |
|
|
|
31 |
@app.exception_handler(RequestValidationError)
|
32 |
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
33 |
return JSONResponse(
|
34 |
status_code=HTTP_400_BAD_REQUEST,
|
35 |
content=ResponseModel(
|
36 |
-
success=False,
|
37 |
-
|
38 |
-
data={"errors": exc.errors()}
|
39 |
-
).dict()
|
40 |
)
|
41 |
|
|
|
42 |
# Configure CORS
|
43 |
app.add_middleware(
|
44 |
CORSMiddleware,
|
45 |
allow_origins=["*"],
|
46 |
-
allow_credentials=
|
47 |
allow_methods=["*"],
|
48 |
allow_headers=["*"],
|
49 |
)
|
@@ -56,18 +51,21 @@ app.include_router(tasks_router)
|
|
56 |
app.include_router(users_router)
|
57 |
app.include_router(portfolio_router)
|
58 |
|
|
|
59 |
# Database initialization and cleanup
|
60 |
@app.on_event("startup")
|
61 |
async def startup_event():
|
62 |
# Clear and reinitialize database on startup
|
63 |
await init_db()
|
64 |
|
|
|
65 |
@app.on_event("shutdown")
|
66 |
async def shutdown_event():
|
67 |
# await clear_db()
|
68 |
await close_db()
|
69 |
|
|
|
70 |
# Root endpoint
|
71 |
@app.get("/")
|
72 |
async def root():
|
73 |
-
return {"message": "Welcome to Uwekezaji API"}
|
|
|
9 |
from App.routers.tasks.routes import router as tasks_router
|
10 |
from App.routers.users.routes import router as users_router
|
11 |
from App.routers.portfolio.routes import router as portfolio_router
|
12 |
+
from App.schemas import ResponseModel, AppException
|
13 |
|
14 |
+
from db import init_db, close_db, clear_db
|
15 |
|
16 |
app = FastAPI(title="Uwekezaji API", description="Stock Market Data API")
|
17 |
|
18 |
|
|
|
19 |
@app.exception_handler(AppException)
|
20 |
async def custom_http_exception_handler(request: Request, exc: AppException):
|
21 |
return JSONResponse(
|
22 |
status_code=exc.status_code,
|
23 |
+
content=ResponseModel(success=False, message=exc.detail, data=exc.data).dict(),
|
|
|
|
|
|
|
|
|
24 |
)
|
25 |
|
26 |
+
|
27 |
@app.exception_handler(RequestValidationError)
|
28 |
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
29 |
return JSONResponse(
|
30 |
status_code=HTTP_400_BAD_REQUEST,
|
31 |
content=ResponseModel(
|
32 |
+
success=False, message="Validation error", data={"errors": exc.errors()}
|
33 |
+
).dict(),
|
|
|
|
|
34 |
)
|
35 |
|
36 |
+
|
37 |
# Configure CORS
|
38 |
app.add_middleware(
|
39 |
CORSMiddleware,
|
40 |
allow_origins=["*"],
|
41 |
+
allow_credentials=False,
|
42 |
allow_methods=["*"],
|
43 |
allow_headers=["*"],
|
44 |
)
|
|
|
51 |
app.include_router(users_router)
|
52 |
app.include_router(portfolio_router)
|
53 |
|
54 |
+
|
55 |
# Database initialization and cleanup
|
56 |
@app.on_event("startup")
|
57 |
async def startup_event():
|
58 |
# Clear and reinitialize database on startup
|
59 |
await init_db()
|
60 |
|
61 |
+
|
62 |
@app.on_event("shutdown")
|
63 |
async def shutdown_event():
|
64 |
# await clear_db()
|
65 |
await close_db()
|
66 |
|
67 |
+
|
68 |
# Root endpoint
|
69 |
@app.get("/")
|
70 |
async def root():
|
71 |
+
return {"message": "Welcome to Uwekezaji API"}
|