Coverage for gwcelery/flask.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-11-14 05:52 +0000

1"""Flask web application setup.""" 

2import os 

3 

4from flask import Flask 

5from flask_caching import Cache 

6from werkzeug.middleware.proxy_fix import ProxyFix 

7 

8from . import app as celery_app 

9 

10__all__ = ('app', 'cache') 

11 

12 

13# Adapted from http://flask.pocoo.org/snippets/69/ 

14class RemoteUserMiddleware: 

15 

16 def __init__(self, app): 

17 self.app = app 

18 

19 def __call__(self, environ, start_response): 

20 user = environ.pop('HTTP_X_PROXY_REMOTE_USER', None) 

21 environ['REMOTE_USER'] = user 

22 return self.app(environ, start_response) 

23 

24 

25app = Flask(__name__) 

26app.wsgi_app = ProxyFix(app.wsgi_app, x_host=1, x_prefix=1) 

27app.wsgi_app = RemoteUserMiddleware(app.wsgi_app) 

28 

29app.config.update( 

30 # Default secret key: secure and random. However, sessions are not 

31 # preserved across different Python processes. 

32 SECRET_KEY=os.urandom(24), 

33 # When running Flask in development mode, reload the application upon 

34 # changes to the templates. 

35 TEMPLATES_AUTO_RELOAD=True 

36) 

37 

38# Set up a server-side cache to store autocomplete responses in order to reduce 

39# traffic to GraceDB. The cache's backend is the same Redis database that 

40# Celery uses, although the Redis keys will have a different prefix so that 

41# they are ignored by Celery. 

42cache = Cache(app, config={'CACHE_DEFAULT_TIMEOUT': 30, # lifetime in seconds 

43 'CACHE_REDIS_HOST': celery_app.backend.client, 

44 'CACHE_TYPE': 'redis'})