unify redis config (closes #8)

also, not sure why brotli was being initialised in routes not in app
This commit is contained in:
codl 2017-09-07 01:10:02 +02:00
parent fda4428572
commit 146dd263c9
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
4 changed files with 26 additions and 32 deletions

17
app.py
View File

@ -9,18 +9,18 @@ from flask_limiter import Limiter
from lib.auth import get_viewer
import os
import mimetypes
import lib.brotli
app = Flask(__name__)
default_config = {
"SQLALCHEMY_TRACK_MODIFICATIONS": False,
"SQLALCHEMY_DATABASE_URI": "postgresql+psycopg2:///forget",
"CELERY_BROKER": "redis://",
"HTTPS": True,
"SENTRY_CONFIG": {},
"RATELIMIT_STORAGE_URL": "redis://",
"REPO_URL": "https://github.com/codl/forget",
"COMMIT_URL": "https://github.com/codl/forget/commits/{hash}",
"REDIS_URI": "redis://",
}
app.config.update(default_config)
@ -38,6 +38,17 @@ metadata = MetaData(naming_convention={
db = SQLAlchemy(app, metadata=metadata)
migrate = Migrate(app, db)
if not 'RATELIMIT_STORAGE_URL' in app.config:
uri = app.config['REDIS_URI']
assert not uri.startswith('unix://'), "flask-limiter does not support redis over a unix socket"
app.config['RATELIMIT_STORAGE_URL'] = uri
if not 'CELERY_BROKER' in app.config:
uri = app.config['REDIS_URI']
if uri.startswith('unix://'):
uri = url.replace('unix', 'redis+socket', 1)
app.config['CELERY_BROKER'] = uri
sentry = None
if 'SENTRY_DSN' in app.config:
from raven.contrib.flask import Sentry
@ -100,3 +111,5 @@ def install_security_headers(resp):
mimetypes.add_type('image/webp', '.webp')
lib.brotli.brotli(app)

View File

@ -13,6 +13,14 @@ only postgresql with psycopg2 driver is officially supported
"""
# SQLALCHEMY_DATABASE_URI='postgresql+psycopg2:///forget'
"""
REDIS URI
see https://redis-py.readthedocs.io/en/latest/#redis.ConnectionPool.from_url
for syntax reference
"""
# REDIS_URI='redis://'
"""
TWITTER CREDENTIALS
@ -27,32 +35,11 @@ this will be necessary so we can tell twitter where to redirect
"""
# SERVER_NAME="localhost:5000"
# CELERY_BROKER='redis://'
# HTTPS=True
# SENTRY_DSN='https://foo:bar@sentry.io/69420'
'''
you can set this to memory:// if you only have one web process
or if you don't care about people exhausting your twitter api
key and your celery workers by making hundreds of login
requests and uploading hundreds of bogus tweet archives
docs here <https://flask-limiter.readthedocs.io/en/stable/#configuration>
'''
# RATELIMIT_STORAGE_URL='redis://'
# REDIS=dict(
# db=0
#
# host='localhost'
# port=6379
# # or...
# unix_socket_path='/var/run/redis/redis.sock'
# # see `pydoc redis.StrictRedis.__init__` for full list of arguments
# )
"""
you can also use any config variable that flask expects here, such as
"""

View File

@ -8,10 +8,8 @@ import mimetypes
class BrotliCache(object):
def __init__(self, redis_kwargs=None, max_wait=0.020, expire=60*60*6):
if not redis_kwargs:
redis_kwargs = {}
self.redis = redis.StrictRedis(**redis_kwargs)
def __init__(self, redis_uri='redis://', max_wait=0.020, expire=60*60*6):
self.redis = redis.StrictRedis.from_url(redis_uri)
self.max_wait = max_wait
self.expire = expire
self.redis.client_setname('brotlicache')
@ -81,5 +79,5 @@ def brotli(app, static=True, dynamic=True):
if static:
app.view_functions['static'] = static_maybe_gzip_brotli
if dynamic:
cache = BrotliCache()
cache = BrotliCache(redis_uri = app.config.get('REDIS_URI'))
app.after_request(cache.wrap_response)

View File

@ -14,7 +14,6 @@ from twitter import TwitterError
from urllib.error import URLError
import version
import lib.version
import lib.brotli
import lib.settings
import lib.json
@ -54,9 +53,6 @@ def touch_viewer(resp):
return resp
lib.brotli.brotli(app)
@app.route('/')
def index():
if g.viewer: