forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·71 lines (53 loc) · 2.11 KB
/
main.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
# Copied from https://github.com/Azure-Samples/azure-voting-app-redis
from flask import Flask, request, render_template
import os
import random
import redis
import socket
import sys
app = Flask(__name__)
# Load configurations
app.config.from_pyfile('config_file.cfg')
button1 = app.config['VOTE1VALUE']
button2 = app.config['VOTE2VALUE']
title = app.config['TITLE']
# Redis configurations
redis_server = os.environ['REDIS']
redis_port = os.environ['REDIS_PORT']
redis_password = os.environ['REDIS_PWD']
# Redis Connection
try:
r = redis.StrictRedis(host=redis_server, port=redis_port, password=redis_password)
r.ping()
except redis.ConnectionError:
exit('Failed to connect to Redis, terminating.')
# Init Redis
if not r.get(button1): r.set(button1,0)
if not r.get(button2): r.set(button2,0)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
# Get current values
vote1 = r.get(button1).decode('utf-8')
vote2 = r.get(button2).decode('utf-8')
# Return index with values
return render_template("index.html", value1=int(vote1), value2=int(vote2), button1=button1, button2=button2, title=title)
elif request.method == 'POST':
if request.form['vote'] == 'reset':
# Empty table and return results
r.set(button1,0)
r.set(button2,0)
vote1 = r.get(button1).decode('utf-8')
vote2 = r.get(button2).decode('utf-8')
return render_template("index.html", value1=int(vote1), value2=int(vote2), button1=button1, button2=button2, title=title)
else:
# Insert vote result into DB
vote = request.form['vote']
r.incr(vote,1)
# Get current values
vote1 = r.get(button1).decode('utf-8')
vote2 = r.get(button2).decode('utf-8')
# Return results
return render_template("index.html", value1=int(vote1), value2=int(vote2), button1=button1, button2=button2, title=title)
if __name__ == "__main__":
app.run()