Chris Jones avatar Chris Jones committed 7ab31f7

get_redis_connection method now pings Redis to make sure the connection is working. Added a new setup method and template to the app which handles setting Redis connection info if the user is not using the standard Redis values. Added checks to all methods which connect to redis to handle connection failures. Intentionally left styling off the setup form since Ken always changes it anyways.

Comments (0)

Files changed (2)

 from flask import Flask, render_template, redirect, url_for, request, flash
 from flask import session, jsonify
 import redis
+from redis.exceptions import ConnectionError
 import settings
 
 SECRET_KEY = '781b0650af13493089a6ffafac755a61'
 app.debug = True
 
 def get_redis_connection(session):
-    """ Get Redis connection with session values """
-    return redis.Redis(
+    """
+    Get Redis connection with session values. Ping Redis
+    to make sure connection is working.
+    """
+    r = redis.Redis(
         host=session.get('redis_host', settings.REDIS_HOST),
         port=session.get('redis_port', settings.REDIS_PORT),
         db=session.get('redis_db', settings.REDIS_DB))
 
+    try:
+        r.ping()
+    except ConnectionError:
+        return None
+
+    return r
+
 def set_session_defaults(session):
     """ Setup default session """
     session['redis_db'] = settings.REDIS_DB
 def info():
     """ View for info about your redis set up. """
     r = get_redis_connection(session)
+    if not r:
+        return redirect(url_for('setup'))
+
     info = r.info().items()
 
     return render_template('info.html', info=info)
 
+@app.route('/setup/', methods=['GET', 'POST'])
+def setup():
+    """
+    If a connection error with Redis occurs, users will be redirected
+    here to setup the connection information.
+    """
+    if request.method == 'POST':
+        host = request.form['host'] or settings.REDIS_HOST
+        try:
+            port = int(request.form['port'])
+        except ValueError:
+            port = settings.REDIS_PORT
+            flash('Port number must be an integer. Default used.')
+
+        session['redis_host'] = host
+        session['redis_port'] = port
+
+        return redirect(url_for('index'))
+
+
+    return render_template('setup.html')
+
 @app.route('/')
 def index():
     """ All available keys. """
         set_session_defaults(session)
     r = get_redis_connection(session)
 
+    if not r:
+        return redirect(url_for('setup'))
+
     keys = r.keys()
     return render_template('index.html', keys=keys)
 
         set_session_defaults(session)
     r = get_redis_connection(session)
 
+    if not r:
+        return redirect(url_for('setup'))
+
     keys = r.keys()
     return jsonify(keys=keys)
 
     """ Info for the key. """
     r = get_redis_connection(session)
 
+    if not r:
+        return redirect(url_for('setup'))
+
     rtype = r.type(key)
     if rtype == 'hash':
         output = r.hgetall(key)
     """ Update the value of a key. """
     r = get_redis_connection(session)
 
+    if not r:
+        return redirect(url_for('setup'))
+
     rtype = r.type(key)
     value = request.form['value']
 
     """ Delete key """
     r = get_redis_connection(session)
 
+    if not r:
+        return redirect(url_for('setup'))
+
     if r.exists(key):
         r.delete(key)
         return jsonify(flash="Key '" + key + "' was deleted successfully")

templates/setup.html

+{% extends "layout.html" %}
+
+{% block leftblock %}
+<header id="keyheader">
+	<h1>Setup</h1>
+</header>
+<p>There was an error connecting to Redis. Either Redis is not running or you 
+are using a custom host/port. Use the form below to setup your Redis
+connection.</p>
+
+<form action="" method="post">
+	<fieldset>
+		<label for="host">Host:</label>
+		<input type="text" name="host" placeholder="{{ session.redis_host }}">
+		<label for="port">Port:</label>
+		<input type="text" name="port" placeholder="{{ session.redis_port }}">
+		<input type="submit" value="Submit">
+	</fieldset>
+</form>
+{% endblock %}
+
+{% block content %}
+{% endblock %}
+
+{% block extra_scripts %}
+{% endblock %}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.