From 055e3d7f429fca9f2fb406f7b923342a15edfbfe Mon Sep 17 00:00:00 2001 From: AnthonyDuan Date: Sun, 23 Nov 2025 12:39:02 +0800 Subject: [PATCH] fix: fallback to memory limiter and SimpleCache/session filesystem when Redis unavailable --- app.py | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/app.py b/app.py index b11322e..0f919ba 100644 --- a/app.py +++ b/app.py @@ -53,37 +53,30 @@ def get_remote_address() -> str: limiter = Limiter( get_remote_address, app=app, - # default_limits=[], - # storage_uri="memory://", - # Redis - storage_uri=os.environ.get("REDIS_URI", "redis://127.0.0.1:6379/"), - # Redis cluster - # storage_uri="redis+cluster://localhost:7000,localhost:7001,localhost:70002", - # Memcached - # storage_uri="memcached://localhost:11211", - # Memcached Cluster - # storage_uri="memcached://localhost:11211,localhost:11212,localhost:11213", - # MongoDB - # storage_uri="mongodb://localhost:27017", - # Etcd - # storage_uri="etcd://localhost:2379", - strategy="fixed-window", # or "moving-window" + strategy="fixed-window", + storage_uri=os.environ.get("REDIS_URI") or "memory://", ) client = MongoClient(host=os.environ.get("TAIKO_WEB_MONGO_HOST") or take_config('MONGO', required=True)['host']) basedir = take_config('BASEDIR') or '/' app.secret_key = take_config('SECRET_KEY') or 'change-me' -app.config['SESSION_TYPE'] = 'redis' redis_config = take_config('REDIS', required=True) redis_config['CACHE_REDIS_HOST'] = os.environ.get("TAIKO_WEB_REDIS_HOST") or redis_config['CACHE_REDIS_HOST'] -app.config['SESSION_REDIS'] = Redis( - host=redis_config['CACHE_REDIS_HOST'], - port=redis_config['CACHE_REDIS_PORT'], - password=redis_config['CACHE_REDIS_PASSWORD'], - db=redis_config['CACHE_REDIS_DB'] -) -app.cache = Cache(app, config=redis_config) +try: + _r = Redis( + host=redis_config['CACHE_REDIS_HOST'], + port=redis_config['CACHE_REDIS_PORT'], + password=redis_config['CACHE_REDIS_PASSWORD'], + db=redis_config['CACHE_REDIS_DB'] + ) + _r.ping() + app.config['SESSION_TYPE'] = 'redis' + app.config['SESSION_REDIS'] = _r + app.cache = Cache(app, config=redis_config) +except Exception: + app.config['SESSION_TYPE'] = 'filesystem' + app.cache = Cache(app, config={'CACHE_TYPE': 'SimpleCache'}) sess = Session() sess.init_app(app) #csrf = CSRFProtect(app)