Skip to content

Added health routes to backend and frontend #328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/ohq/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
SemesterViewSet,
TagViewSet,
UserView,
HealthView,
)


Expand All @@ -45,6 +46,7 @@
realtime_router.register(AnnouncementViewSet)

additional_urls = [
path("health/", HealthView.as_view(), name="health"),
path("accounts/me/", UserView.as_view(), name="me"),
path("accounts/me/resend/", ResendNotificationView.as_view(), name="resend"),
path("courses/<slug:course_pk>/mass-invite/", MassInviteView.as_view(), name="mass-invite"),
Expand Down
21 changes: 21 additions & 0 deletions backend/ohq/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Subquery,
When,
)
from django.views.generic import View
from django.http import HttpResponseBadRequest, JsonResponse
from django.utils import timezone
from django.utils.crypto import get_random_string
Expand All @@ -32,6 +33,7 @@
from rest_framework.views import APIView
from rest_live.mixins import RealtimeMixin
from schedule.models import Event, EventRelationManager, Occurrence
from http import HTTPStatus

from ohq.filters import CourseStatisticFilter, QuestionSearchFilter, QueueStatisticFilter
from ohq.invite import parse_and_send_invites
Expand Down Expand Up @@ -775,3 +777,22 @@ def list(self, request, *args, **kwargs):

def get_queryset(self):
return Occurrence.objects.filter(pk=self.kwargs["pk"])
class HealthView(View):
def get(self, request):
"""
Health check endpoint to confirm the backend is running.
---
summary: Health Check
responses:
"200":
content:
application/json:
schema:
type: object
properties:
message:
type: string
enum: ["OK"]
---
"""
return JsonResponse({"message": "OK"}, status=HTTPStatus.OK)
9 changes: 9 additions & 0 deletions backend/tests/ohq/test_health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.test import TestCase
from django.urls import reverse

class HealthTestCase(TestCase):
def test_health(self):
url = reverse("health")
resp = self.client.get(url)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.data, {"message": "OK"})
24 changes: 24 additions & 0 deletions frontend/pages/health.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { GetServerSideProps } from "next";

const HealthPage = () => {
return <div>OK</div>;
};

export const getServerSideProps: GetServerSideProps = async ({ req }) => {
const userAgent = req.headers["user-agent"] || "";

if (userAgent !== "service-status") {
return {
redirect: {
destination: "/",
permanent: false,
},
};
}

return {
props: {},
};
};

export default HealthPage;
Loading