Skip to content

Commit

Permalink
added /api/healthcheck endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
LoTerence committed Sep 26, 2024
1 parent b9d27b3 commit ad7903e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
5 changes: 3 additions & 2 deletions backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from pathlib import Path
from decouple import config

VERSION = '1.0.0'

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

Expand All @@ -26,8 +28,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = config('DEBUG', default=False, cast=bool)

ALLOWED_HOSTS = ["localhost", "0.0.0.0", "127.0.0.1"]

ALLOWED_HOSTS = config("DJANGO_ALLOWED_HOSTS", default="localhost").split(" ")

# Application definition

Expand Down
3 changes: 2 additions & 1 deletion backend/ctj_api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

urlpatterns = [
path('opportunities/', views.OpportunitiesList.as_view()),
path('opportunities/<uuid:pk>/', views.OpportunitiesDetails.as_view())
path('opportunities/<uuid:pk>/', views.OpportunitiesDetails.as_view()),
path('healthcheck', views.Healthcheck.as_view(), name='healthcheck')
]
21 changes: 20 additions & 1 deletion backend/ctj_api/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from rest_framework import generics
from rest_framework.response import Response
from rest_framework.views import APIView
from ctj_api.models import Opportunities
from ctj_api.serializers import OpportunitiesSerializer

from django.conf import settings
import time

class OpportunitiesList(generics.ListCreateAPIView):
queryset = Opportunities.objects.all()
Expand All @@ -11,3 +14,19 @@ class OpportunitiesList(generics.ListCreateAPIView):
class OpportunitiesDetails(generics.RetrieveUpdateDestroyAPIView):
queryset = Opportunities.objects.all()
serializer_class = OpportunitiesSerializer

class Healthcheck(APIView):
start_time = time.time()

def get(self, request):
uptime_seconds = time.time() - self.start_time
uptime_hours = uptime_seconds / 3600
hostname = request.get_host()

return Response({
"message": "healthcheck",
"uptime": f"{uptime_hours:.2f} hours",
# "uptime": f"{uptime_seconds:.1f} seconds",
"version": settings.VERSION,
"hostname": hostname
})

0 comments on commit ad7903e

Please sign in to comment.