-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter-service.py
73 lines (62 loc) · 2.3 KB
/
counter-service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from flask import Flask, request, jsonify
import os
app = Flask(__name__)
# Define the path for the counter file to store the data in Docker Volume
COUNTER_FILE = "/data/counter.txt"
def read_counter():
"""
Reads and returns the current counter value from the file.
If the file doesn't exist, it return 0.
Returns:
int: The current counter value.
"""
if os.path.exists(COUNTER_FILE):
with open(COUNTER_FILE, "r") as f:
return int(f.read().strip())
else:
return 0
def update_counter(counter):
"""
Updates the counter file with the new counter value.
Args:
counter (int): The new counter value to write to the file.
"""
with open(COUNTER_FILE, "w") as f:
f.write(str(counter))
@app.route('/', methods=['GET', 'POST'])
def handle_request():
"""
Handles GET and POST requests to the root endpoint.
GET request returns the current count of POST requests.
POST request increments the counter and returns the updated count.
Returns:
str: The response message with the current or updated counter.
"""
counter = read_counter()
if request.method == 'POST':
# Increment the counter for each POST request and update the file.
counter += 1
update_counter(counter)
return f"POST requests counter updated. Current count: {counter}"
else:
# For GET requests, just return the current count.
return f"Current POST requests count: {counter}"
@app.route('/health', methods=['GET'])
def health_check():
"""
Performs a simple health check of the application.
It tries to read the counter file as a basic check.
Returns:
tuple: A JSON response indicating the health status and the HTTP status code.
"""
try:
# Basic health check: Ensure the counter file is accessible.
read_counter()
return jsonify({"status": "healthy"}), 200
except Exception as e:
# Return an unhealthy status if any error occurs, e.g., file access issues.
return jsonify({"status": "unhealthy", "reason": str(e)}), 500
if __name__ == '__main__':
# Run the Flask app with binding to all interfaces on port 8080.
# Debug mode is turned off for production use.
app.run(host='0.0.0.0', port=8080, debug=False)