Skip to content

Commit f8a4aa0

Browse files
committed
fix: cors
wip: infra as code
1 parent e17ef32 commit f8a4aa0

File tree

6 files changed

+92
-46
lines changed

6 files changed

+92
-46
lines changed

.helm/deployments/rc/server.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
sapi:
2+
flavor: "rc"
3+
4+
image:
5+
tag: rc-server
6+
pullPolicy: Always
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
sapi:
2+
flavor: "rc"
3+
version: "0.3.22"
4+
worker: "api.worker.app"
5+
queue: "0.3.22.rc.compounds"
6+
resources:
7+
flavor: "high"
8+
9+
image:
10+
spec: rc-worker

.helm/deployments/rc/worker-core.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
sapi:
2+
flavor: "rc"
3+
version: "0.3.22"
4+
worker: "api.worker.app"
5+
queue: "0.3.22.rc.core"
6+
resources:
7+
flavor: "low"
8+
9+
image:
10+
spec: rc-worker
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
sapi:
2+
flavor: "rc"
3+
version: "0.3.22"
4+
worker: "api.worker.app"
5+
queue: "0.3.22.rc.features"
6+
resources:
7+
flavor: "high"
8+
9+
image:
10+
spec: rc-worker

.helm/deployments/rc/worker-v4.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
sapi:
2+
flavor: "rc"
3+
version: "0.3.22"
4+
worker: ""
5+
queue: ""
6+
resources:
7+
flavor: "low"
8+
9+
image:
10+
spec: rc-worker-v4

api/server/api.py

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,52 @@
5757

5858
add_pagination(siibra_api)
5959

60+
@siibra_api.exception_handler(RuntimeError)
61+
async def exception_runtime(request: Request, exc: RuntimeError) -> JSONResponse:
62+
"""Handling RuntimeErrors.
63+
Most of the RuntimeErrors are thrown by the siibra-python library when other Services are not responding.
64+
To be more resilient and not throw a simple and unplanned HTTP 500 response, this handler will return an HTTP 503
65+
status."""
66+
general_logger.warning(f"Error handler: exception_runtime: {str(exc)}")
67+
return JSONResponse(
68+
status_code=503,
69+
content={
70+
"detail": "This part of the siibra service is temporarily unavailable",
71+
"error": str(exc)
72+
},
73+
)
74+
75+
76+
@siibra_api.exception_handler(SapiBaseException)
77+
def exception_sapi(request: Request, exc: SapiBaseException):
78+
"""Handle sapi errors"""
79+
general_logger.warning(f"Error handler: exception_sapi: {str(exc)}")
80+
raise HTTPException(400, str(exc))
81+
82+
@siibra_api.exception_handler(IndexError)
83+
async def handle_index_error(request: Request, exc: IndexError):
84+
return JSONResponse(status_code=404, content={
85+
"detail": "Cannot find item",
86+
"error": str(exc)
87+
})
88+
89+
@siibra_api.exception_handler(Exception)
90+
async def exception_other(request: Request, exc: Exception):
91+
"""Catch all exception handler"""
92+
general_logger.warning(f"Error handler: exception_other: {str(exc)}")
93+
return JSONResponse(
94+
status_code=500,
95+
content={
96+
"detail": "Some error occurred",
97+
"error": str(exc)
98+
}
99+
)
100+
101+
# Versioning for api endpoints
102+
exception_handlers = siibra_api.exception_handlers
103+
siibra_api = VersionedFastAPI(siibra_api)
104+
105+
60106
# Allow Cors
61107

62108
origins = ["*"]
@@ -247,8 +293,6 @@ async def middleware_add_version_header(request: Request, call_next):
247293
@siibra_api.middleware("http")
248294
async def middleware_access_log(request: Request, call_next):
249295
"""Access log middleware"""
250-
251-
print("AccessLog middleware called")
252296

253297
if request.url.path in do_not_logs:
254298
return await call_next(request)
@@ -290,50 +334,6 @@ async def append_origin_header(request: Request, call_next):
290334
request.scope["headers"] = new_headers
291335
return await call_next(request)
292336

293-
@siibra_api.exception_handler(RuntimeError)
294-
async def exception_runtime(request: Request, exc: RuntimeError) -> JSONResponse:
295-
"""Handling RuntimeErrors.
296-
Most of the RuntimeErrors are thrown by the siibra-python library when other Services are not responding.
297-
To be more resilient and not throw a simple and unplanned HTTP 500 response, this handler will return an HTTP 503
298-
status."""
299-
general_logger.warning(f"Error handler: exception_runtime: {str(exc)}")
300-
return JSONResponse(
301-
status_code=503,
302-
content={
303-
"detail": "This part of the siibra service is temporarily unavailable",
304-
"error": str(exc)
305-
},
306-
)
307-
308-
309-
@siibra_api.exception_handler(SapiBaseException)
310-
def exception_sapi(request: Request, exc: SapiBaseException):
311-
"""Handle sapi errors"""
312-
general_logger.warning(f"Error handler: exception_sapi: {str(exc)}")
313-
raise HTTPException(400, str(exc))
314-
315-
@siibra_api.exception_handler(IndexError)
316-
async def handle_index_error(request: Request, exc: IndexError):
317-
return JSONResponse(status_code=404, content={
318-
"detail": "Cannot find item",
319-
"error": str(exc)
320-
})
321-
322-
@siibra_api.exception_handler(Exception)
323-
async def exception_other(request: Request, exc: Exception):
324-
"""Catch all exception handler"""
325-
general_logger.warning(f"Error handler: exception_other: {str(exc)}")
326-
return JSONResponse(
327-
status_code=500,
328-
content={
329-
"detail": "Some error occurred",
330-
"error": str(exc)
331-
}
332-
)
333-
334-
# Versioning for api endpoints
335-
exception_handlers = siibra_api.exception_handlers
336-
siibra_api = VersionedFastAPI(siibra_api)
337337

338338
# add error handlers to versioned fastapi
339339
# see https://github.com/DeanWay/fastapi-versioning/issues/30

0 commit comments

Comments
 (0)