Skip to content

Commit 5ce97c0

Browse files
committed
Lazily connect to Redis in voting app
Allows voting app to boot up without anything else. Signed-off-by: Ben Firshman <[email protected]>
1 parent e7b4670 commit 5ce97c0

File tree

2 files changed

+7
-23
lines changed

2 files changed

+7
-23
lines changed

voting-app/app.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
from flask import Flask
2-
from flask import render_template
3-
from flask import request
4-
from flask import make_response
5-
from utils import connect_to_redis
1+
from flask import Flask, render_template, request, make_response, g
2+
from redis import Redis
63
import os
74
import socket
85
import random
@@ -12,9 +9,12 @@
129
option_b = os.getenv('OPTION_B', "Dogs")
1310
hostname = socket.gethostname()
1411

15-
redis = connect_to_redis("redis")
1612
app = Flask(__name__)
1713

14+
def get_redis():
15+
if not hasattr(g, 'redis'):
16+
g.redis = Redis(host="redis", db=0)
17+
return g.redis
1818

1919
@app.route("/", methods=['POST','GET'])
2020
def hello():
@@ -25,6 +25,7 @@ def hello():
2525
vote = None
2626

2727
if request.method == 'POST':
28+
redis = get_redis()
2829
vote = request.form['vote']
2930
data = json.dumps({'voter_id': voter_id, 'vote': vote})
3031
redis.rpush('votes', data)

voting-app/utils/__init__.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)