gold eggs backend
Some checks failed
Build and Push to Docker Hub / build-test-push (push) Failing after 1m55s
Some checks failed
Build and Push to Docker Hub / build-test-push (push) Failing after 1m55s
This commit is contained in:
4
core/middlewares/__init__.py
Executable file
4
core/middlewares/__init__.py
Executable file
@@ -0,0 +1,4 @@
|
||||
from .core import ExceptionMiddleware # noqa
|
||||
from .cache_middleware import * # noqa
|
||||
from .logging_middleware import * # noqa
|
||||
from .fcm_token import * # noqa
|
||||
13
core/middlewares/cache_middleware.py
Executable file
13
core/middlewares/cache_middleware.py
Executable file
@@ -0,0 +1,13 @@
|
||||
class CacheMiddleware:
|
||||
def __init__(self, get_response):
|
||||
self.get_response = get_response
|
||||
|
||||
def __call__(self, request):
|
||||
response = self.get_response(request)
|
||||
vary_headers = set(
|
||||
response.get("Vary", "").replace(" ", "").split(",")
|
||||
)
|
||||
vary_headers.update(["Accept-Language"])
|
||||
# Authorization
|
||||
response["Vary"] = ", ".join(vary_headers)
|
||||
return response
|
||||
62
core/middlewares/core.py
Executable file
62
core/middlewares/core.py
Executable file
@@ -0,0 +1,62 @@
|
||||
from django.http import response
|
||||
|
||||
from core import exceptions
|
||||
|
||||
|
||||
class ExceptionMiddleware:
|
||||
"""
|
||||
This class is used to handle exceptions that occur during the request/response cycle.
|
||||
It is a middleware that is added to the Django middleware pipeline.
|
||||
"""
|
||||
|
||||
def __init__(self, get_response):
|
||||
"""
|
||||
Initialize the middleware.
|
||||
|
||||
Args:
|
||||
get_response: The next middleware in the pipeline.
|
||||
"""
|
||||
self.get_response = get_response
|
||||
|
||||
def __call__(self, request):
|
||||
"""
|
||||
This method is called for each request.
|
||||
It retrieves the response from the next middleware in the pipeline,
|
||||
and handles any exceptions that occur.
|
||||
|
||||
Args:
|
||||
request: The incoming request.
|
||||
|
||||
Returns:
|
||||
The response from the next middleware in the pipeline.
|
||||
"""
|
||||
try:
|
||||
response = self.get_response(request)
|
||||
except exceptions.BreakException as e:
|
||||
return self.process_exception(request, e)
|
||||
return response
|
||||
|
||||
def process_exception(self, request, e):
|
||||
"""
|
||||
Process an exception that occurred during the request/response cycle.
|
||||
|
||||
Args:
|
||||
request: The incoming request.
|
||||
e: The exception that occurred.
|
||||
|
||||
Returns:
|
||||
A JSON response containing information about the exception.
|
||||
"""
|
||||
if isinstance(e, exceptions.BreakException):
|
||||
"""
|
||||
If the exception is a BreakException, construct a JSON response containing the error message, data, and
|
||||
any additional arguments passed to the BreakException.
|
||||
"""
|
||||
error_data = {
|
||||
"message": e.message,
|
||||
"data": e.data,
|
||||
"errors": [
|
||||
e.args.__str__(),
|
||||
],
|
||||
}
|
||||
return response.JsonResponse(error_data)
|
||||
27
core/middlewares/fcm_token.py
Normal file
27
core/middlewares/fcm_token.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import jwt
|
||||
from django.conf import settings
|
||||
from django.utils.deprecation import MiddlewareMixin
|
||||
|
||||
from core.http.models import User
|
||||
|
||||
|
||||
class JWTFCMMiddleware(MiddlewareMixin):
|
||||
def process_request(self, request):
|
||||
auth_header = request.headers.get("Authorization")
|
||||
if auth_header and auth_header.startswith("Bearer "):
|
||||
token = auth_header.split(" ")[1]
|
||||
try:
|
||||
payload = jwt.decode(
|
||||
token, settings.SECRET_KEY, algorithms=["HS256"]
|
||||
)
|
||||
user_id = payload.get("user_id")
|
||||
fcm_token = request.headers.get("FCM-Token")
|
||||
if user_id:
|
||||
user = User.objects.get(id=user_id)
|
||||
if fcm_token:
|
||||
user.fcm_token = fcm_token
|
||||
user.save()
|
||||
except jwt.ExpiredSignatureError:
|
||||
pass
|
||||
except jwt.InvalidTokenError:
|
||||
pass
|
||||
14
core/middlewares/logging_middleware.py
Executable file
14
core/middlewares/logging_middleware.py
Executable file
@@ -0,0 +1,14 @@
|
||||
class LoggingMiddleware:
|
||||
def __init__(self, get_response):
|
||||
self.get_response = get_response
|
||||
|
||||
def __call__(self, request):
|
||||
print(
|
||||
f"Request: {request.method} {request.path} from {request.META['REMOTE_ADDR']}"
|
||||
)
|
||||
|
||||
response = self.get_response(request)
|
||||
|
||||
print(f"Response: {response.status_code}")
|
||||
|
||||
return response
|
||||
Reference in New Issue
Block a user