generated from SverreNystad/template_python_application
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2fe404
commit 3627d3c
Showing
2 changed files
with
26 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,26 @@ | ||
from rest_framework.decorators import api_view | ||
from rest_framework.response import Response | ||
from drf_yasg.utils import swagger_auto_schema | ||
from django.http import HttpResponse | ||
from django.db import DatabaseError | ||
from django.core.cache import cache | ||
from drf_yasg import openapi | ||
from drf_yasg.utils import swagger_auto_schema | ||
from rest_framework.decorators import api_view, permission_classes | ||
from rest_framework import permissions, serializers | ||
from rest_framework.response import Response | ||
|
||
@swagger_auto_schema( | ||
method="get", | ||
operation_description="Health check endpoint", | ||
responses={200: "OK"}, | ||
) | ||
@api_view(["GET"]) | ||
@permission_classes([permissions.AllowAny]) | ||
def health_check(request) -> Response: | ||
try: | ||
# Check cache | ||
cache.set('health_check', 'ok', timeout=30) | ||
if cache.get('health_check') != 'ok': | ||
raise ValueError("Cache not working") | ||
|
||
# Define your expected request body as a serializer | ||
from rest_framework import serializers | ||
return Response("OK", content_type="text/plain") | ||
except (DatabaseError, ValueError) as e: | ||
return Response(str(e), status=500, content_type="text/plain") |