diff --git a/Readme.md b/Readme.md index 41553f3..cc87be6 100644 --- a/Readme.md +++ b/Readme.md @@ -48,9 +48,6 @@ wget https://github.com/chanzuckerberg/cellxgene/raw/master/example-dataset/pbmc ```bash export CELLXGENE_DATA=../cellxgene_data # change this directory if you put data in a different place. export CELLXGENE_LOCATION=`which cellxgene` -export GATEWAY_HOST=localhost:5005 -export GATEWAY_PROTOCOL=http -export GATEWAY_IP=127.0.0.1 ``` 3. Now, execute the cellxgene gateway: @@ -63,10 +60,11 @@ 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` -* `GATEWAY_HOST` - the hostname and port that the gateway will run on, typically `localhost:5005` if running locally -* `GATEWAY_PROTOCOL` - typically http when running locally, can be https when deployed if the gateway is behind a load balancer or reverse proxy. -* `GATEWAY_IP` - ip addess of instance gateway is running on, mostly used to display SSH instructions Optional environment variables: +* `EXTERNAL_HOST` - the hostname and port from the perspective of the web browser, typically `localhost:5005` if running locally. Defaults to "localhost:{GATEWAY_PORT}" +* `EXTERNAL_PROTOCOL` - typically http when running locally, can be https when deployed if the gateway is behind a load balancer or reverse proxy that performs https termination. Default value "http" +* `GATEWAY_IP` - ip addess of instance gateway is running on, mostly used to display SSH instructions. Defaults to `socket.gethostbyname(socket.gethostname())` +* `GATEWAY_PORT` - local port that the gateway should bind to, defaults to 5005 * `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. diff --git a/cellxgene_gateway/cache_entry.py b/cellxgene_gateway/cache_entry.py index 6a2f5ee..56c9616 100644 --- a/cellxgene_gateway/cache_entry.py +++ b/cellxgene_gateway/cache_entry.py @@ -96,7 +96,7 @@ class CacheEntry: dataset = self.dataset gateway_basepath = ( - f"{env.gateway_protocol}://{env.gateway_host}/view/{dataset}/" + f"{env.external_protocol}://{env.external_host}/view/{dataset}/" ) subpath = path[len(dataset) :] # noqa: E203 diff --git a/cellxgene_gateway/env.py b/cellxgene_gateway/env.py index 4ee105f..cf397a1 100644 --- a/cellxgene_gateway/env.py +++ b/cellxgene_gateway/env.py @@ -9,11 +9,13 @@ import os import logging +import socket cellxgene_location = os.environ.get("CELLXGENE_LOCATION") cellxgene_data = os.environ.get("CELLXGENE_DATA") -gateway_host = os.environ.get("GATEWAY_HOST") -gateway_protocol = os.environ.get("GATEWAY_PROTOCOL") +gateway_port = int(os.environ.get("GATEWAY_PORT", "5005")) +external_host = os.environ.get("EXTERNAL_HOST", os.environ.get("GATEWAY_HOST", f"localhost:{gateway_port}")) +external_protocol = os.environ.get("EXTERNAL_PROTOCOL", os.environ.get("GATEWAY_PROTOCOL", "http")) ip = os.environ.get("GATEWAY_IP") extra_scripts = os.environ.get("GATEWAY_EXTRA_SCRIPTS") ttl = os.environ.get("GATEWAY_TTL") @@ -22,12 +24,13 @@ enable_upload = os.environ.get("GATEWAY_ENABLE_UPLOAD", "").lower() in ['true', env_vars = { "CELLXGENE_LOCATION": cellxgene_location, "CELLXGENE_DATA": cellxgene_data, - "GATEWAY_HOST": gateway_host, - "GATEWAY_PROTOCOL": gateway_protocol, "GATEWAY_IP": ip, } optional_env_vars = { + "EXTERNAL_HOST": external_host, + "EXTERNAL_PROTOCOL": external_protocol, + "GATEWAY_PORT": gateway_port, "GATEWAY_EXTRA_SCRIPTS": extra_scripts, "GATEWAY_TTL": ttl, "GATEWAY_ENABLE_UPLOAD": enable_upload, @@ -47,8 +50,6 @@ def validate(): export CELLXGENE_LOCATION=~/anaconda/envs/cellxgene-dev/bin/cellxgene export CELLXGENE_DATA=../cellxgene_data - export GATEWAY_HOST=localhost:5005 - export GATEWAY_PROTOCOL=http export GATEWAY_IP=127.0.0.1 """ ) diff --git a/cellxgene_gateway/extra_scripts.py b/cellxgene_gateway/extra_scripts.py index db15ab0..7ef49e5 100644 --- a/cellxgene_gateway/extra_scripts.py +++ b/cellxgene_gateway/extra_scripts.py @@ -14,6 +14,6 @@ from json import loads def get_extra_scripts(): # can be array of script tags to inject on every page, e.g. for google analytics could be # ['https://www.googletagmanager.com/gtag/js?id=UA-123456-2', - # f"{env.gateway_protocol}://{env.gateway_host}/static/js/google_ua.js"] + # f"{env.external_protocol}://{env.external_host}/static/js/google_ua.js"] # where google_ua.js is a script you add to the static/js folder prior to deployment. return [] if env.extra_scripts is None else loads(env.extra_scripts) diff --git a/cellxgene_gateway/gateway.py b/cellxgene_gateway/gateway.py index e219fb0..47dafa3 100644 --- a/cellxgene_gateway/gateway.py +++ b/cellxgene_gateway/gateway.py @@ -37,7 +37,7 @@ from cellxgene_gateway.util import current_time_stamp app = Flask(__name__) cache = BackendCache() -location = f"{env.gateway_protocol}://{env.gateway_host}" +location = f"{env.external_protocol}://{env.external_host}" @app.errorhandler(CellxgeneException) @@ -232,7 +232,7 @@ def main(): background_thread.start() app.launchtime = current_time_stamp() - app.run(host="0.0.0.0", port=5005, debug=False) + app.run(host="0.0.0.0", port=env.gateway_port, debug=False) if __name__ == "__main__": diff --git a/run.sh.example b/run.sh.example index 095f57a..cd0629c 100644 --- a/run.sh.example +++ b/run.sh.example @@ -1,8 +1,5 @@ export CELLXGENE_LOCATION=$(pwd)/.cellxgene-gateway/bin/cellxgene export CELLXGENE_DATA=../cellxgene_data -export DEPLOYMENT_ENV=dev -export GATEWAY_HOST=localhost:5005 -export GATEWAY_PROTOCOL=http export GATEWAY_IP=127.0.0.1 #Once these are set, you run like a normal Flask app