This repository has been archived by the owner on Apr 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 967
/
app.py
executable file
·58 lines (46 loc) · 1.63 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask, render_template, request, make_response, g, send_from_directory
import os
import socket
import random
import json
import requests
option_a = os.getenv('OPTION_A', u"Cat 🐺")
option_b = os.getenv('OPTION_B', u"Dog 🐶")
hostname = socket.gethostname()
app = Flask(__name__)
@app.route("/", methods=['POST','GET'])
def hello():
rest_endpoint="http://" + os.environ["VOTING_API_SERVICE_HOST"] + ":" + os.environ["VOTING_API_SERVICE_PORT"]
voter_id = request.cookies.get('voter_id')
if not voter_id:
voter_id = hex(random.getrandbits(64))[2:-1]
vote = None
if request.method == 'POST':
vote = request.form['vote']
data = json.dumps({'voter_id': voter_id, 'vote': vote})
requests.post(url=rest_endpoint + "/vote", data=data)
resp = make_response(render_template(
'index.html',
option_a=option_a,
option_b=option_b,
hostname=hostname,
vote=vote,
))
resp.set_cookie('voter_id', voter_id)
return resp
@app.route("/result", methods=['GET'])
def result():
return make_response(render_template('result/index.html'))
@app.route("/votes", methods=['GET'])
def votes():
rest_endpoint="http://" + os.environ["VOTING_API_SERVICE_HOST"] + ":" + os.environ["VOTING_API_SERVICE_PORT"]
response = requests.get(url=rest_endpoint + "/vote")
print(response.content)
return response.content
@app.route('/templates/<path:path>')
def send_js(path):
return send_from_directory('templates', path)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080, debug=True)