Skip to content

Commit dcb7156

Browse files
authored
[SHARE-497][SHARE-526][Task] Add a status/version endpoint to the API (#584)
* In DEBUG mode, if VERSION is not set it will be pulled from a git repo * The Dockerfile now accepts GIT_TAG which will be passed in as VERSION * [SHARE-526] Also removes "Notify" from browsable API page title
1 parent 3517383 commit dcb7156

6 files changed

Lines changed: 45 additions & 1 deletion

File tree

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ COPY ./ /code/
5252

5353
RUN python manage.py collectstatic --noinput
5454

55+
ARG GIT_TAG=
5556
ARG GIT_COMMIT=
57+
ENV VERSION ${GIT_TAG}
5658
ENV GIT_COMMIT ${GIT_COMMIT}
5759

5860
CMD ["python", "manage.py", "--help"]

api/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def register_url(self, subclass, viewset):
9393
]
9494

9595
urlpatterns = [
96+
url(r'status/?', views.ServerStatusView.as_view(), name='status'),
9697
url(r'rss/?', views.CreativeWorksRSS(), name='rss'),
9798
url(r'atom/?', views.CreativeWorksAtom(), name='atom'),
9899
url(r'graph/?', GraphQLView.as_view(graphiql=True)),

api/views/share.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from rest_framework_json_api import serializers
66

77
from django import http
8+
from django.conf import settings
89
from django.views.decorators.http import require_GET
910
from django.views.generic.base import RedirectView
1011
from django.shortcuts import get_object_or_404
@@ -139,3 +140,15 @@ def get(self, request, *args, **kwargs):
139140
return HttpSmartResponsePermanentRedirect(url)
140141
return HttpSmartResponseRedirect(url)
141142
return http.HttpResponseGone()
143+
144+
145+
class ServerStatusView(views.APIView):
146+
def get(self, request):
147+
return Response({
148+
'id': '1',
149+
'type': 'Status',
150+
'attributes': {
151+
'status': 'up',
152+
'version': settings.VERSION,
153+
}
154+
})

project/settings.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"""
1212

1313
import os
14+
import subprocess
1415

1516
from django.utils.log import DEFAULT_LOGGING
1617

@@ -35,6 +36,14 @@
3536
# SECURITY WARNING: don't run with debug turned on in production!
3637
DEBUG = bool(os.environ.get('DEBUG', True))
3738

39+
if 'VERSION' not in os.environ and DEBUG:
40+
try:
41+
VERSION = subprocess.check_output(['git', 'describe']).decode().strip()
42+
except subprocess.CalledProcessError:
43+
VERSION = 'UNKNOWN'
44+
else:
45+
VERSION = os.environ.get('VERSION') or 'UNKNOWN'
46+
3847
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '').split(' ')
3948

4049
AUTH_USER_MODEL = 'share.ShareUser'

templates/rest_framework/api.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{% load rest_framework %}
33
{% load staticfiles %}
44

5-
{% block title %}SHARE Notify{% endblock %}
5+
{% block title %}SHARE{% endblock %}
66

77
{% block style %}
88
{% block bootstrap_theme %}

tests/api/test_status.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django.test import override_settings
2+
3+
4+
class TestAPIStatusView:
5+
6+
@override_settings(VERSION='TESTCASE')
7+
def test_works(self, client):
8+
resp = client.get('/api/v2/status/')
9+
assert resp.status_code == 200
10+
assert resp.json() == {
11+
'data': {
12+
'id': '1',
13+
'type': 'Status',
14+
'attributes': {
15+
'status': 'up',
16+
'version': 'TESTCASE',
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)