generated from hackforla/.github-hackforla-base-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add healthcheck api for new backend (#547)
- Loading branch information
1 parent
11ad034
commit d487416
Showing
6 changed files
with
39 additions
and
2 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
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,4 +1,5 @@ | ||
from django.apps import AppConfig | ||
import ctjbackend.uptime | ||
|
||
|
||
class CtjbackendConfig(AppConfig): | ||
|
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import datetime | ||
|
||
start_time = datetime.datetime.now() |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.urls import path | ||
from ctjbackend import views | ||
|
||
urlpatterns = [ | ||
path('healthcheck/', views.healthcheck, name='healthcheck'), | ||
] |
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,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) |