-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstarlette_tmp_middleware.py
More file actions
29 lines (24 loc) · 969 Bytes
/
starlette_tmp_middleware.py
File metadata and controls
29 lines (24 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import logging
from starlette.middleware.base import BaseHTTPMiddleware
from submodules.model.business_objects import general
import traceback
from fast_api.routes.client_response import GENERIC_FAILURE_RESPONSE
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
class DatabaseSessionHandler(BaseHTTPMiddleware):
def __init__(self, app):
super().__init__(app)
async def dispatch(self, request, call_next):
if request.url.path.startswith("/api"):
# fast api middleware handles these
return await call_next(request)
general.get_ctx_token()
try:
response = await call_next(request)
# finally is still called even if returned response
return response
except Exception:
print(traceback.format_exc(), flush=True)
return GENERIC_FAILURE_RESPONSE
finally:
general.remove_and_refresh_session()