Skip to content

Commit

Permalink
initial code
Browse files Browse the repository at this point in the history
  • Loading branch information
initcron committed Jan 15, 2019
0 parents commit a40e824
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 0 deletions.
47 changes: 47 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from flask import Flask, render_template, request, make_response, g
from redis import Redis
import os
import socket
import random
import json

option_a = os.getenv('OPTION_A', "Emacs")
option_b = os.getenv('OPTION_B', "Vi")
hostname = socket.gethostname()
version = 'v1'

app = Flask(__name__)

def get_redis():
if not hasattr(g, 'redis'):
g.redis = Redis(host="redis", db=0, socket_timeout=5)
return g.redis

@app.route("/", methods=['POST','GET'])
def hello():
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':
redis = get_redis()
vote = request.form['vote']
data = json.dumps({'voter_id': voter_id, 'vote': vote})
redis.rpush('votes', data)

resp = make_response(render_template(
'index.html',
option_a=option_a,
option_b=option_b,
hostname=hostname,
vote=vote,
version=version,
))
resp.set_cookie('voter_id', voter_id)
return resp


if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True, threaded=True)
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Flask
Redis
gunicorn
129 changes: 129 additions & 0 deletions static/stylesheets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
@import url(//fonts.googleapis.com/css?family=Open+Sans:400,700,600);

*{
box-sizing:border-box;
}
html,body{
margin: 0;
padding: 0;
background-color: #F7F8F9;
height: 100vh;
font-family: 'Open Sans';
}

button{
border-radius: 0;
width: 100%;
height: 50%;
}

button[type="submit"] {
-webkit-appearance:none; -webkit-border-radius:0;
}

button i{
float: right;
padding-right: 30px;
margin-top: 3px;
}

button.a{
background-color: #1aaaf8;
}

button.b{
background-color: #00cbca;
}

#tip{
text-align: left;
color: #c0c9ce;
font-size: 14px;
}

#hostname{
position: absolute;
bottom: 100px;
right: 0;
left: 0;
color: #8f9ea8;
font-size: 24px;
}

#content-container{
z-index: 2;
position: relative;
margin: 0 auto;
display: table;
padding: 10px;
max-width: 940px;
height: 100%;
}
#content-container-center{
display: table-cell;
text-align: center;
}

#content-container-center h3{
color: #254356;
}

#choice{
transition: all 300ms linear;
line-height: 1.3em;
display: inline;
vertical-align: middle;
font-size: 3em;
}
#choice a{
text-decoration:none;
}
#choice a:hover, #choice a:focus{
outline:0;
text-decoration:underline;
}

#choice button{
display: block;
height: 80px;
width: 330px;
border: none;
color: white;
text-transform: uppercase;
font-size:18px;
font-weight: 700;
margin-top: 10px;
margin-bottom: 10px;
text-align: left;
padding-left: 50px;
}

#choice button.a:hover{
background-color: #1488c6;
}

#choice button.b:hover{
background-color: #00a2a1;
}

#choice button.a:focus{
background-color: #1488c6;
}

#choice button.b:focus{
background-color: #00a2a1;
}

#background-stats{
z-index:1;
height:100%;
width:100%;
position:absolute;
}
#background-stats div{
transition: width 400ms ease-in-out;
display:inline-block;
margin-bottom:-4px;
width:50%;
height:100%;
}
50 changes: 50 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{option_a}} vs {{option_b}}!</title>
<base href="/index.html">
<meta name = "viewport" content = "width=device-width, initial-scale = 1.0">
<meta name="keywords" content="docker-compose, docker, stack">
<meta name="author" content="Tutum dev team">
<link rel='stylesheet' href="{{ url_for('static',filename='stylesheets/style.css') }}" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
</head>
<body>
<div id="content-container">
<div id="content-container-center">
<h2> App Version {{version}}</h2>
<h3>{{option_a}} vs {{option_b}}!</h3>
<form id="choice" name='form' method="POST" action="/">
<button id="a" type="submit" name="vote" class="a" value="a">{{option_a}}</button>
<button id="b" type="submit" name="vote" class="b" value="b">{{option_b}}</button>
</form>
<div id="tip">
(Tip: you can change your vote)
</div>
<div id="hostname">
Processed by container ID {{hostname}}
</div>
</div>
</div>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>

{% if vote %}
<script>
var vote = "{{vote}}";

if(vote == "a"){
$(".a").prop('disabled', true);
$(".a").html('{{option_a}} <i class="fa fa-check-circle"></i>');
$(".b").css('opacity','0.5');
}
if(vote == "b"){
$(".b").prop('disabled', true);
$(".b").html('{{option_b}} <i class="fa fa-check-circle"></i>');
$(".a").css('opacity','0.5');
}
</script>
{% endif %}
</body>
</html>

0 comments on commit a40e824

Please sign in to comment.