Skip to content

Commit

Permalink
Add healthcheck api for new backend (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzzz-coding authored Jul 1, 2024
1 parent 11ad034 commit d487416
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions backend/api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@

USE_TZ = True

VERSION = '1.0.0'


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/
Expand Down
4 changes: 3 additions & 1 deletion backend/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include


urlpatterns = [
path('admin/', admin.site.urls),
path("ctjbackend/", include("ctjbackend.urls")),
]
1 change: 1 addition & 0 deletions backend/ctjbackend/apps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.apps import AppConfig
import ctjbackend.uptime


class CtjbackendConfig(AppConfig):
Expand Down
3 changes: 3 additions & 0 deletions backend/ctjbackend/uptime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import datetime

start_time = datetime.datetime.now()
6 changes: 6 additions & 0 deletions backend/ctjbackend/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.urls import path
from ctjbackend import views

urlpatterns = [
path('healthcheck/', views.healthcheck, name='healthcheck'),
]
25 changes: 24 additions & 1 deletion backend/ctjbackend/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
from django.shortcuts import render
from django.http import JsonResponse
from django.conf import settings
import datetime
from ctjbackend.uptime import start_time

# Create your views here.

# Healthcheck view that returns the uptime of the app, a healthcheck message, and the version.
def healthcheck(request):
current_time = datetime.datetime.now()
uptime_duration = current_time - start_time
uptime_seconds = int(uptime_duration.total_seconds())

hours = int(uptime_seconds // 3600)
minutes = int((uptime_seconds % 3600) // 60)
seconds = int(uptime_seconds % 60)

uptime_str = f"{hours:02d} hours {minutes:02d} minutes {seconds:02d} seconds"

context = {
"message": "healthcheck",
"uptime": uptime_str,
"version": settings.VERSION,
}

return JsonResponse(context)

0 comments on commit d487416

Please sign in to comment.