From f1f4b0c0ca8e0f1d6473ddfa00d205114add2b71 Mon Sep 17 00:00:00 2001 From: Alok Saldanha Date: Sat, 13 Feb 2021 16:41:38 -0500 Subject: [PATCH] added environment variables for ProxyFix --- README.md | 10 +++++++++- cellxgene_gateway/cache_entry.py | 12 +++--------- cellxgene_gateway/env.py | 21 +++++++++++++-------- cellxgene_gateway/filecrawl.py | 2 +- cellxgene_gateway/gateway.py | 16 +++++++++++++++- 5 files changed, 41 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 730f4a9..8974341 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ Here's what the environment variables mean: * `CELLXGENE_LOCATION` - the location of the cellxgene executable, e.g. `~/anaconda2/envs/cellxgene/bin/cellxgene` * `CELLXGENE_DATA` - a directory that can contain subdirectories with `.h5ad` data files, *without* trailing slash, e.g. `/mnt/cellxgene_data` + Optional environment variables: * `CELLXGENE_ARGS` - catch-all variable that can be used to pass additional command line args to cellxgene server * `EXTERNAL_HOST` - the hostname and port from the perspective of the web browser, typically `localhost:5005` if running locally. Defaults to "localhost:{GATEWAY_PORT}" @@ -69,7 +70,14 @@ Optional environment variables: * `GATEWAY_EXTRA_SCRIPTS` - JSON array of script paths, will be embedded into each page and forwarded with `--scripts` to cellxgene server * `GATEWAY_ENABLE_UPLOAD` - Set to `true` or `1` to enable HTTP uploads. This is not recommended for a public server. * `GATEWAY_ENABLE_ANNOTATIONS` - Set to `true` or to `1` to enable cellxgene annotations. -* `GATEWAY_ENABLE_BACKED_MODE` - Set to `true` or to `1` to load AnnData in file-backed mode. This saves memory and speeds up launch time but may reduce overall performance. +* `GATEWAY_ENABLE_BACKED_MODE` - Set to `true` or to `1` to load AnnData in file-backed mode. This saves memory and speeds up launch time but may reduce overall performance. + +If any of the following optional variables are set, [ProxyFix](https://werkzeug.palletsprojects.com/en/1.0.x/middleware/proxy_fix/) will be used. +* `PROXY_FIX_FOR` - Number of upstream proxies setting X-Forwarded-For +* `PROXY_FIX_PROTO` - Number of upstream proxies setting X-Forwarded-Proto +* `PROXY_FIX_HOST` - Number of upstream proxies setting X-Forwarded-Host +* `PROXY_FIX_PORT` - Number of upstream proxies setting X-Forwarded-Port +* `PROXY_FIX_PREFIX` - Number of upstream proxies setting X-Forwarded-Prefix The defaults should be fine if you set up a venv and cellxgene_data folder as above. diff --git a/cellxgene_gateway/cache_entry.py b/cellxgene_gateway/cache_entry.py index ea12390..311e5c0 100644 --- a/cellxgene_gateway/cache_entry.py +++ b/cellxgene_gateway/cache_entry.py @@ -171,15 +171,11 @@ class CacheEntry: cellxgene_response = get(full_path, headers=headers) elif request.method == "PUT": cellxgene_response = put( - full_path, - headers=headers, - data=request.data, + full_path, headers=headers, data=request.data, ) elif request.method == "POST": cellxgene_response = post( - full_path, - headers=headers, - data=request.data, + full_path, headers=headers, data=request.data, ) else: raise CellxgeneException( @@ -199,9 +195,7 @@ class CacheEntry: resp_headers[h] = cellxgene_response.headers[h] gateway_response = make_response( - gateway_content, - cellxgene_response.status_code, - resp_headers, + gateway_content, cellxgene_response.status_code, resp_headers, ) return gateway_response diff --git a/cellxgene_gateway/env.py b/cellxgene_gateway/env.py index 2fcae35..0df5723 100644 --- a/cellxgene_gateway/env.py +++ b/cellxgene_gateway/env.py @@ -31,22 +31,22 @@ enable_upload = os.environ.get("GATEWAY_ENABLE_UPLOAD", "").lower() in [ ] enable_annotations = os.environ.get( "GATEWAY_ENABLE_ANNOTATIONS", "" -).lower() in [ - "true", - "1", -] +).lower() in ["true", "1",] enable_backed_mode = os.environ.get( "GATEWAY_ENABLE_BACKED_MODE", "" -).lower() in [ - "true", - "1", -] +).lower() in ["true", "1",] env_vars = { "CELLXGENE_LOCATION": cellxgene_location, "CELLXGENE_DATA": cellxgene_data, } +proxy_fix_for = int(os.environ.get("PROXY_FIX_FOR", "0")) +proxy_fix_proto = int(os.environ.get("PROXY_FIX_PROTO", "0")) +proxy_fix_host = int(os.environ.get("PROXY_FIX_HOST", "0")) +proxy_fix_port = int(os.environ.get("PROXY_FIX_PORT", "0")) +proxy_fix_prefix = int(os.environ.get("PROXY_FIX_PREFIX", "0")) + optional_env_vars = { "EXTERNAL_HOST": external_host, "EXTERNAL_PROTOCOL": external_protocol, @@ -58,6 +58,11 @@ optional_env_vars = { "GATEWAY_ENABLE_ANNOTATIONS": enable_annotations, "GATEWAY_ENABLE_BACKED_MODE": enable_backed_mode, "CELLXGENE_ARGS": cellxgene_args, + "PROXY_FIX_FOR": proxy_fix_for, + "PROXY_FIX_PROTO": proxy_fix_proto, + "PROXY_FIX_HOST": proxy_fix_host, + "PROXY_FIX_PORT": proxy_fix_port, + "PROXY_FIX_PREFIX": proxy_fix_prefix, } diff --git a/cellxgene_gateway/filecrawl.py b/cellxgene_gateway/filecrawl.py index f0ec1f4..97c2126 100644 --- a/cellxgene_gateway/filecrawl.py +++ b/cellxgene_gateway/filecrawl.py @@ -92,7 +92,7 @@ def render_entries(entries): def get_url(entry): - return url_for("do_view", path=entry["path"].lstrip("/")) +"/"" + return url_for("do_view", path=entry["path"].lstrip("/") + "/") def get_class(entry): diff --git a/cellxgene_gateway/gateway.py b/cellxgene_gateway/gateway.py index 4659b2b..12e9352 100644 --- a/cellxgene_gateway/gateway.py +++ b/cellxgene_gateway/gateway.py @@ -50,7 +50,21 @@ def _force_https(app): app.wsgi_app = _force_https(app.wsgi_app) -app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1, x_prefix=1) +if ( + env.proxy_fix_for > 0 + or env.proxy_fix_proto > 0 + or env.proxy_fix_host > 0 + or env.proxy_fix_port > 0 + or env.proxy_fix_prefix > 0 +): + app.wsgi_app = ProxyFix( + app.wsgi_app, + x_for=env.proxy_fix_for, + x_proto=env.proxy_fix_proto, + x_host=env.proxy_fix_host, + x_port=env.proxy_fix_port, + x_prefix=env.proxy_fix_prefix, + ) cache = BackendCache()