-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathapp.py
71 lines (59 loc) · 1.77 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
from flask import Flask, render_template, request, make_response, g
from redis import Redis, RedisError
import os
import socket
import random
import json
import collections
hostname = socket.gethostname()
redis = Redis(host="redis", db=0)
app = Flask(__name__)
def getOptions():
option_a = 'Cats'
option_b = 'Dogs'
return option_a, option_b
@app.route("/", methods=['POST','GET'])
def hello():
option_a, option_b = getOptions()
try:
votesA = int(redis.get(option_a) or 0)
votesB = int(redis.get(option_b) or 0)
except RedisError:
votesA = "<i>cannot connect to Redis, counter disabled</i>"
votesB = "<i>cannot connect to Redis, counter disabled</i>"
if request.method == 'POST':
try:
vote = request.form['vote']
if vote == "a":
votesA = redis.incr(option_a)
else:
votesB = redis.incr(option_b)
except Exception as e:
print(e)
votesA = "<i>An error occured</i>"
votesB = "<i>An error occured</i>"
with open('/var/run/secrets/kubernetes.io/serviceaccount/namespace', 'r') as fp:
namespace = fp.read()
resp = make_response(render_template(
'index.html',
option_a=option_a,
option_b=option_b,
hostname=hostname,
namespace=namespace,
votes_a=votesA,
votes_b=votesB,
))
return resp
if __name__ == "__main__":
extra_files = []
if "development" == os.getenv("FLASK_ENV"):
app.jinja_env.auto_reload = True
app.config['TEMPLATES_AUTO_RELOAD'] = True
extra_files=[
"./static/stylesheets/style.css"
]
app.run(
host='0.0.0.0',
port=8080,
extra_files=extra_files
)