Switch to use decorator for checking if redis server is up.

- setup.py removed empty line flake8 was complaining.
This commit is contained in:
Brandon Davis 2016-08-21 21:27:00 -07:00
parent c9db491485
commit 9694d7da7f
2 changed files with 16 additions and 13 deletions

View file

@ -36,4 +36,3 @@ setup(
], ],
zip_safe=False, zip_safe=False,
) )

View file

@ -3,7 +3,7 @@ import sys
import uuid import uuid
import redis import redis
from redis.exceptions import ConnectionError from redis.exceptions import ConnectionError, TimeoutError
from flask import abort, Flask, render_template, request from flask import abort, Flask, render_template, request
@ -23,27 +23,31 @@ time_conversion = {
'hour': 3600 'hour': 3600
} }
def check_redis_alive(force_exit=False):
try: def check_redis_alive(fn):
redis_client.ping() def inner(*args, **kwargs):
except (ConnectionError, TimeoutError) as e: try:
if force_exit: redis_client.ping()
except (ConnectionError, TimeoutError) as e:
print(e) print(e)
sys.exit(0) if fn.__name__ == "main":
else: sys.exit(0)
abort(500) else:
return abort(500)
return fn(*args, **kwargs)
return inner
@check_redis_alive
def set_password(password, ttl): def set_password(password, ttl):
check_redis_alive()
key = uuid.uuid4().hex key = uuid.uuid4().hex
redis_client.set(key, password) redis_client.set(key, password)
redis_client.expire(key, ttl) redis_client.expire(key, ttl)
return key return key
@check_redis_alive
def get_password(key): def get_password(key):
check_redis_alive()
password = redis_client.get(key) password = redis_client.get(key)
if password is not None: if password is not None:
password = password.decode('utf-8') password = password.decode('utf-8')
@ -96,8 +100,8 @@ def show_password(password_key):
return render_template('password.html', password=password) return render_template('password.html', password=password)
@check_redis_alive
def main(): def main():
check_redis_alive(force_exit=True)
app.run(host='0.0.0.0', debug=True) app.run(host='0.0.0.0', debug=True)