Skip to content

Commit

Permalink
feat: add health_check view
Browse files Browse the repository at this point in the history
  • Loading branch information
SverreNystad authored Apr 4, 2024
1 parent d2fe404 commit 3627d3c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion backend/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from django.urls import path

# from users.views import login, register_user
from .views import health_check
from flashcards.views import create_flashcards, generate_mock_flashcard

urlpatterns = [
# path("create-user/", register_user, name="create-user"),
# path("login/", login, name="login"),
path("health-check/", health_check, name="Health_check"),
path("generate-mock-flashcard/", generate_mock_flashcard, name="generate-mock-flashcards"),
path("create-flashcards/", create_flashcards, name="create-flashcards"),
path("generate-mock-flashcard/", generate_mock_flashcard, name="generate-mock-flashcard"),
Expand Down
29 changes: 24 additions & 5 deletions backend/api/views.py
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")

0 comments on commit 3627d3c

Please sign in to comment.