remove flask-limiter, make sure redis isnt initialised early

This commit is contained in:
codl 2017-09-24 16:37:38 +02:00
parent fe1db3cf36
commit 520560ce79
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
3 changed files with 8 additions and 28 deletions

29
app.py
View File

@ -1,13 +1,9 @@
from flask import Flask, request
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import MetaData, event
from sqlalchemy.engine import Engine
from sqlalchemy import MetaData
from flask_migrate import Migrate
import version
from libforget.cachebust import cachebust
from flask_limiter import Limiter
from libforget.auth import get_viewer
import os
import mimetypes
import libforget.brotli
import libforget.img_proxy
@ -39,12 +35,7 @@ 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:
if 'CELERY_BROKER' not in app.config:
uri = app.config['REDIS_URI']
if uri.startswith('unix://'):
uri = url.replace('unix', 'redis+socket', 1)
@ -66,20 +57,6 @@ def inject_static():
return {'st': static}
def rate_limit_key():
viewer = get_viewer()
if viewer:
return viewer.id
for address in request.access_route:
if address != '127.0.0.1':
print(address)
return address
return request.remote_addr
limiter = Limiter(app, key_func=rate_limit_key)
@app.after_request
def install_security_headers(resp):
csp = ("default-src 'none';"

View File

@ -12,14 +12,15 @@ class BrotliCache(object):
self.redis = redis.StrictRedis.from_url(redis_uri)
self.timeout = timeout
self.expire = expire
self.redis.client_setname('brotlicache')
def compress_and_cache(self, cache_key, lock_key, body, mode=brotli_.MODE_GENERIC):
self.redis.client_setname('brotlicache')
encbody = brotli_.compress(body, mode=mode)
self.redis.set(cache_key, encbody, px=int(self.expire*1000))
self.redis.delete(lock_key)
def wrap_response(self, response):
self.redis.client_setname('brotlicache')
if 'br' not in request.accept_encodings or response.is_streamed:
return response

View File

@ -16,7 +16,6 @@ class ImgProxyCache(object):
self.timeout = timeout
self.expire = expire
self.prefix = prefix
self.redis.client_setname('img_proxy')
self.hash = hmac_hash
self.hmac_key = None
@ -25,6 +24,7 @@ class ImgProxyCache(object):
prefix=self.prefix, args=":".join(args))
def token(self):
self.redis.client_setname('img_proxy')
if not self.hmac_key:
t = self.redis.get(self.key('hmac_key'))
if not t:
@ -54,6 +54,7 @@ class ImgProxyCache(object):
return url
def fetch_and_cache(self, url):
self.redis.client_setname('img_proxy')
resp = requests.get(url)
if(resp.status_code != 200):
return
@ -83,6 +84,7 @@ class ImgProxyCache(object):
resp.content, px=expire*1000)
def respond(self, identifier):
self.redis.client_setname('img_proxy')
url = self.url_for(identifier)
if not url:
return abort(403)