Switch to use decorator for checking if redis server is up.
- setup.py removed empty line flake8 was complaining.
This commit is contained in:
parent
c9db491485
commit
9694d7da7f
2 changed files with 16 additions and 13 deletions
1
setup.py
1
setup.py
|
@ -36,4 +36,3 @@ setup(
|
|||
],
|
||||
zip_safe=False,
|
||||
)
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ import sys
|
|||
import uuid
|
||||
|
||||
import redis
|
||||
from redis.exceptions import ConnectionError
|
||||
from redis.exceptions import ConnectionError, TimeoutError
|
||||
|
||||
from flask import abort, Flask, render_template, request
|
||||
|
||||
|
@ -23,27 +23,31 @@ time_conversion = {
|
|||
'hour': 3600
|
||||
}
|
||||
|
||||
def check_redis_alive(force_exit=False):
|
||||
|
||||
def check_redis_alive(fn):
|
||||
def inner(*args, **kwargs):
|
||||
try:
|
||||
redis_client.ping()
|
||||
except (ConnectionError, TimeoutError) as e:
|
||||
if force_exit:
|
||||
print(e)
|
||||
if fn.__name__ == "main":
|
||||
sys.exit(0)
|
||||
else:
|
||||
abort(500)
|
||||
return abort(500)
|
||||
return fn(*args, **kwargs)
|
||||
return inner
|
||||
|
||||
|
||||
@check_redis_alive
|
||||
def set_password(password, ttl):
|
||||
check_redis_alive()
|
||||
key = uuid.uuid4().hex
|
||||
redis_client.set(key, password)
|
||||
redis_client.expire(key, ttl)
|
||||
return key
|
||||
|
||||
|
||||
@check_redis_alive
|
||||
def get_password(key):
|
||||
check_redis_alive()
|
||||
password = redis_client.get(key)
|
||||
if password is not None:
|
||||
password = password.decode('utf-8')
|
||||
|
@ -96,8 +100,8 @@ def show_password(password_key):
|
|||
return render_template('password.html', password=password)
|
||||
|
||||
|
||||
@check_redis_alive
|
||||
def main():
|
||||
check_redis_alive(force_exit=True)
|
||||
app.run(host='0.0.0.0', debug=True)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue