Skip to content

Commit 675c34d

Browse files
committed
feat: add /health endpoint
* add a new route * add CHANGELOG.md * Update Dockerfile * Update tests
1 parent 2e1a425 commit 675c34d

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## 2.2.0
2+
3+
Added /health endpoint
4+
5+
## 2.1.0
6+
7+
Added Authentication functionality
8+
9+
## 2.0.0
10+
11+
Multi threading and improved performance
12+
13+
## 1.0.0
14+
15+
Initial Release

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ WORKDIR /app
1818

1919
COPY --from=build /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages
2020
COPY app.py ./
21+
COPY CHANGELOG.md ./
2122

2223
RUN chown -R app:app /app
2324

app.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
"""App Module"""
22
import os
3+
import re
34
import logging
45
from concurrent.futures import ThreadPoolExecutor
56

67
import xml.etree.ElementTree as ET
78
import requests
8-
from flask import Flask, request, make_response
9+
from flask import Flask, request, make_response, jsonify
910
from flask_basicauth import BasicAuth
1011

1112
logging.basicConfig(level=logging.INFO)
@@ -149,6 +150,29 @@ def index():
149150
return response
150151

151152

153+
@app.route('/health')
154+
def health():
155+
"""Endpoint for checking the health status.
156+
157+
Returns:
158+
flask.Response: JSON response containing the status and version.
159+
"""
160+
161+
with open('CHANGELOG.md', 'r', encoding='utf-8') as changelog_file:
162+
changelog_content = changelog_file.read()
163+
164+
latest_version_match = re.search(r'##\s*(\d+\.\d+\.\d+)', changelog_content)
165+
latest_version = latest_version_match.group(1) if latest_version_match else 'Unknown'
166+
167+
168+
response = {
169+
'status': 'ok',
170+
'version': f'{latest_version}'
171+
}
172+
173+
return jsonify(response)
174+
175+
152176
@app.errorhandler(Exception)
153177
def handle_error(exception):
154178
"""Error handler for handling exceptions raised in the application.

tests/test_app.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ def setUp(self):
1717
'Authorization': 'Basic dXNlcjpwYXNz'
1818
}
1919

20+
def test_health_endpoint(self):
21+
"""Test health check status & version in the health route."""
22+
response = self.client.get('/health')
23+
self.assertEqual(response.status_code, 200)
24+
self.assertIn(b'{"status":"ok","version":"2.2.0"}', response.data)
25+
2026
def test_index_missing_parameters(self):
2127
"""Test handling missing parameters in the index route."""
2228
response = self.client.get('/', headers=self.headers)

0 commit comments

Comments
 (0)