-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.py
89 lines (73 loc) · 2.5 KB
/
app.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from arguments import args
from flask import Flask, Response, request, json
import logging
import os
import redis
import time
app = Flask(__name__)
LISTEN_HOST = args.host
LISTEN_PORT = args.port
REDIS_HOST = args.redis_host
REDIS_PORT = args.redis_port
redis_client = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, db=0, decode_responses=True)
livenessDelay = 0
@app.route('/')
def root():
data = json.dumps({
"appName": "cloud-native-demo",
"version": "1.0.0",
"redis-host": REDIS_HOST,
"env": {
"host": os.getenv('HOSTNAME'),
"user_defined_1": os.getenv('USER_DEFINED_1'),
"user_defined_2": os.getenv('USER_DEFINED_2'),
"user_defined_3": os.getenv('USER_DEFINED_3')
}
})
return Response(data, mimetype="application/json")
@app.route('/counter')
def counter():
hostname = os.getenv("HOSTNAME")
try:
if redis_client.hexists("hosts", hostname):
redis_client.hincrby("hosts", hostname, amount=1)
else:
redis_client.hset("hosts", hostname, 1)
return Response(json.dumps(redis_client.hgetall("hosts")), status=200, mimetype="application/json")
except:
return Response(json.dumps({"error": "service unavailable"}), status=503, mimetype="application/json")
@app.route('/counter/reset')
def clear_counter():
hosts = redis_client.hgetall("hosts")
[redis_client.hdel("hosts", key) for key in hosts.keys()]
return Response(json.dumps(redis_client.hgetall("hosts")), status=200, mimetype="application/json")
@app.route('/live')
def live_get():
time.sleep(livenessDelay)
return Response(json.dumps({"delay": livenessDelay}), status=200, mimetype="application/json")
@app.route('/live/<int:delay>')
def live_post(delay):
global livenessDelay
livenessDelay = delay
return Response(json.dumps({"delay": livenessDelay}), status=200, mimetype="application/json")
@app.route('/ready')
def ready():
redis_ready = False
try:
redis_ready = redis_client.ping()
except:
logging.warning("python connection down")
response = Response(mimetype="application/json")
if redis_ready:
response.status = "200"
response.response = json.dumps({
"redis_connection": "up"
})
else:
response.status = "503"
response.response = json.dumps({
"redis_connection": "down"
})
return response
if __name__ == '__main__':
app.run(host=LISTEN_HOST, port=LISTEN_PORT)