mirror of
https://github.com/codl/forget
synced 2025-01-19 02:20:40 +01:00
remove flask-limiter, make sure redis isnt initialised early
This commit is contained in:
parent
fe1db3cf36
commit
520560ce79
29
app.py
29
app.py
@ -1,13 +1,9 @@
|
|||||||
from flask import Flask, request
|
from flask import Flask
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from sqlalchemy import MetaData, event
|
from sqlalchemy import MetaData
|
||||||
from sqlalchemy.engine import Engine
|
|
||||||
from flask_migrate import Migrate
|
from flask_migrate import Migrate
|
||||||
import version
|
import version
|
||||||
from libforget.cachebust import cachebust
|
from libforget.cachebust import cachebust
|
||||||
from flask_limiter import Limiter
|
|
||||||
from libforget.auth import get_viewer
|
|
||||||
import os
|
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import libforget.brotli
|
import libforget.brotli
|
||||||
import libforget.img_proxy
|
import libforget.img_proxy
|
||||||
@ -39,12 +35,7 @@ metadata = MetaData(naming_convention={
|
|||||||
db = SQLAlchemy(app, metadata=metadata)
|
db = SQLAlchemy(app, metadata=metadata)
|
||||||
migrate = Migrate(app, db)
|
migrate = Migrate(app, db)
|
||||||
|
|
||||||
if not 'RATELIMIT_STORAGE_URL' in app.config:
|
if 'CELERY_BROKER' not 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']
|
uri = app.config['REDIS_URI']
|
||||||
if uri.startswith('unix://'):
|
if uri.startswith('unix://'):
|
||||||
uri = url.replace('unix', 'redis+socket', 1)
|
uri = url.replace('unix', 'redis+socket', 1)
|
||||||
@ -66,20 +57,6 @@ def inject_static():
|
|||||||
return {'st': 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
|
@app.after_request
|
||||||
def install_security_headers(resp):
|
def install_security_headers(resp):
|
||||||
csp = ("default-src 'none';"
|
csp = ("default-src 'none';"
|
||||||
|
@ -12,14 +12,15 @@ class BrotliCache(object):
|
|||||||
self.redis = redis.StrictRedis.from_url(redis_uri)
|
self.redis = redis.StrictRedis.from_url(redis_uri)
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
self.expire = expire
|
self.expire = expire
|
||||||
self.redis.client_setname('brotlicache')
|
|
||||||
|
|
||||||
def compress_and_cache(self, cache_key, lock_key, body, mode=brotli_.MODE_GENERIC):
|
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)
|
encbody = brotli_.compress(body, mode=mode)
|
||||||
self.redis.set(cache_key, encbody, px=int(self.expire*1000))
|
self.redis.set(cache_key, encbody, px=int(self.expire*1000))
|
||||||
self.redis.delete(lock_key)
|
self.redis.delete(lock_key)
|
||||||
|
|
||||||
def wrap_response(self, response):
|
def wrap_response(self, response):
|
||||||
|
self.redis.client_setname('brotlicache')
|
||||||
if 'br' not in request.accept_encodings or response.is_streamed:
|
if 'br' not in request.accept_encodings or response.is_streamed:
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
@ -16,7 +16,6 @@ class ImgProxyCache(object):
|
|||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
self.expire = expire
|
self.expire = expire
|
||||||
self.prefix = prefix
|
self.prefix = prefix
|
||||||
self.redis.client_setname('img_proxy')
|
|
||||||
self.hash = hmac_hash
|
self.hash = hmac_hash
|
||||||
self.hmac_key = None
|
self.hmac_key = None
|
||||||
|
|
||||||
@ -25,6 +24,7 @@ class ImgProxyCache(object):
|
|||||||
prefix=self.prefix, args=":".join(args))
|
prefix=self.prefix, args=":".join(args))
|
||||||
|
|
||||||
def token(self):
|
def token(self):
|
||||||
|
self.redis.client_setname('img_proxy')
|
||||||
if not self.hmac_key:
|
if not self.hmac_key:
|
||||||
t = self.redis.get(self.key('hmac_key'))
|
t = self.redis.get(self.key('hmac_key'))
|
||||||
if not t:
|
if not t:
|
||||||
@ -54,6 +54,7 @@ class ImgProxyCache(object):
|
|||||||
return url
|
return url
|
||||||
|
|
||||||
def fetch_and_cache(self, url):
|
def fetch_and_cache(self, url):
|
||||||
|
self.redis.client_setname('img_proxy')
|
||||||
resp = requests.get(url)
|
resp = requests.get(url)
|
||||||
if(resp.status_code != 200):
|
if(resp.status_code != 200):
|
||||||
return
|
return
|
||||||
@ -83,6 +84,7 @@ class ImgProxyCache(object):
|
|||||||
resp.content, px=expire*1000)
|
resp.content, px=expire*1000)
|
||||||
|
|
||||||
def respond(self, identifier):
|
def respond(self, identifier):
|
||||||
|
self.redis.client_setname('img_proxy')
|
||||||
url = self.url_for(identifier)
|
url = self.url_for(identifier)
|
||||||
if not url:
|
if not url:
|
||||||
return abort(403)
|
return abort(403)
|
||||||
|
Loading…
Reference in New Issue
Block a user