From bb6840ab32d39240442f32c89ac3c4d0722d8372 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Fri, 4 Jan 2019 13:00:38 -0800 Subject: use .env for all config (and document it) --- python/.gitignore | 1 + python/README.md | 4 +++ python/env.example | 8 ++++++ python/fatcat_web/__init__.py | 3 ++- python/fatcat_web/web_config.py | 54 +++++++++++++++++++++++++++++++++++++++++ python/web_config.py | 54 ----------------------------------------- 6 files changed, 69 insertions(+), 55 deletions(-) create mode 100644 python/env.example create mode 100644 python/fatcat_web/web_config.py delete mode 100644 python/web_config.py diff --git a/python/.gitignore b/python/.gitignore index e2dae299..a0bc258b 100644 --- a/python/.gitignore +++ b/python/.gitignore @@ -1,3 +1,4 @@ +.env codegen-out/ build/ dist/ diff --git a/python/README.md b/python/README.md index d8812934..922499f3 100644 --- a/python/README.md +++ b/python/README.md @@ -34,6 +34,10 @@ server on the same machine by default), use: # will listen on http://localhost:9810 by default pipenv run fatcat_webface.py +Almost all configuration is done via environment variables; see `env.example` +for a list of settings. If you copy this file to `.env` it will be sourced by +`pipenv` automatically; you can also load it in your shell like `source .env`. + ## Running Tests Many (though not all) python tests depend on access to a local running API diff --git a/python/env.example b/python/env.example new file mode 100644 index 00000000..c139df07 --- /dev/null +++ b/python/env.example @@ -0,0 +1,8 @@ +FLASK_SECRET_KEY="" +FATCAT_API_AUTH_TOKEN="" +FATCAT_API_HOST="http://localhost:9411/v0" +ELASTICSEARCH_BACKEND="http://localhost:9200" +ELASTICSEARCH_INDEX="fatcat" +GITLAB_CLIENT_ID="" +GITLAB_CLIENT_SECRET="" +SENTRY_DSN="" diff --git a/python/fatcat_web/__init__.py b/python/fatcat_web/__init__.py index 34618076..53947143 100644 --- a/python/fatcat_web/__init__.py +++ b/python/fatcat_web/__init__.py @@ -6,9 +6,10 @@ from flask_login import LoginManager from authlib.flask.client import OAuth from loginpass import create_flask_blueprint, Gitlab from raven.contrib.flask import Sentry -from web_config import Config import fatcat_client +from fatcat_web.web_config import Config + toolbar = DebugToolbarExtension() app = Flask(__name__) app.config.from_object(Config) diff --git a/python/fatcat_web/web_config.py b/python/fatcat_web/web_config.py new file mode 100644 index 00000000..5713738c --- /dev/null +++ b/python/fatcat_web/web_config.py @@ -0,0 +1,54 @@ + +""" +Default configuration for fatcat web interface (Flask application). + +In production, we currently reconfigure these values using environment +variables, not by (eg) deploying a variant copy of this file. + +This config is *only* for the web interface, *not* for any of the workers or +import scripts. +""" + +import os +import raven +import subprocess + +basedir = os.path.abspath(os.path.dirname(__file__)) + +class Config(object): + GIT_REVISION = subprocess.check_output(["git", "describe", "--always"]).strip() + + # This is, effectively, the QA/PROD flag + FATCAT_DOMAIN = os.environ.get("FATCAT_DOMAIN", default="qa.fatcat.wiki") + FATCAT_API_AUTH_TOKEN = os.environ.get("FATCAT_API_AUTH_TOKEN", default=None) + FATCAT_API_HOST = os.environ.get("FATCAT_API_HOST", default="https://{}/v0".format(FATCAT_DOMAIN)) + + # can set this to https://search.fatcat.wiki for some experimentation + ELASTICSEARCH_BACKEND = os.environ.get("ELASTICSEARCH_BACKEND", default="http://localhost:9200") + ELASTICSEARCH_INDEX = os.environ.get("ELASTICSEARCH_INDEX", default="fatcat") + + # for flask things, like session cookies + FLASK_SECRET_KEY = os.environ.get("FLASK_SECRET_KEY", default=None) + SECRET_KEY = FLASK_SECRET_KEY + + GITLAB_CLIENT_ID = os.environ.get("GITLAB_CLIENT_ID", default="bogus") + GITLAB_CLIENT_SECRET = os.environ.get("GITLAB_CLIENT_SECRET", default="bogus") + + try: + GIT_RELEASE = raven.fetch_git_sha('..') + except Exception as e: + print("WARNING: couldn't set sentry git release automatically: " + str(e)) + GIT_RELEASE = None + + SENTRY_CONFIG = { + #'include_paths': ['fatcat_web', 'fatcat_client', 'fatcat_tools'], + 'enable-threads': True, # for uWSGI + 'release': GIT_RELEASE, + 'tags': { + 'fatcat_domain': FATCAT_DOMAIN, + }, + } + + # "Even more verbose" debug options + #SQLALCHEMY_ECHO = True + #DEBUG = True diff --git a/python/web_config.py b/python/web_config.py deleted file mode 100644 index 5713738c..00000000 --- a/python/web_config.py +++ /dev/null @@ -1,54 +0,0 @@ - -""" -Default configuration for fatcat web interface (Flask application). - -In production, we currently reconfigure these values using environment -variables, not by (eg) deploying a variant copy of this file. - -This config is *only* for the web interface, *not* for any of the workers or -import scripts. -""" - -import os -import raven -import subprocess - -basedir = os.path.abspath(os.path.dirname(__file__)) - -class Config(object): - GIT_REVISION = subprocess.check_output(["git", "describe", "--always"]).strip() - - # This is, effectively, the QA/PROD flag - FATCAT_DOMAIN = os.environ.get("FATCAT_DOMAIN", default="qa.fatcat.wiki") - FATCAT_API_AUTH_TOKEN = os.environ.get("FATCAT_API_AUTH_TOKEN", default=None) - FATCAT_API_HOST = os.environ.get("FATCAT_API_HOST", default="https://{}/v0".format(FATCAT_DOMAIN)) - - # can set this to https://search.fatcat.wiki for some experimentation - ELASTICSEARCH_BACKEND = os.environ.get("ELASTICSEARCH_BACKEND", default="http://localhost:9200") - ELASTICSEARCH_INDEX = os.environ.get("ELASTICSEARCH_INDEX", default="fatcat") - - # for flask things, like session cookies - FLASK_SECRET_KEY = os.environ.get("FLASK_SECRET_KEY", default=None) - SECRET_KEY = FLASK_SECRET_KEY - - GITLAB_CLIENT_ID = os.environ.get("GITLAB_CLIENT_ID", default="bogus") - GITLAB_CLIENT_SECRET = os.environ.get("GITLAB_CLIENT_SECRET", default="bogus") - - try: - GIT_RELEASE = raven.fetch_git_sha('..') - except Exception as e: - print("WARNING: couldn't set sentry git release automatically: " + str(e)) - GIT_RELEASE = None - - SENTRY_CONFIG = { - #'include_paths': ['fatcat_web', 'fatcat_client', 'fatcat_tools'], - 'enable-threads': True, # for uWSGI - 'release': GIT_RELEASE, - 'tags': { - 'fatcat_domain': FATCAT_DOMAIN, - }, - } - - # "Even more verbose" debug options - #SQLALCHEMY_ECHO = True - #DEBUG = True -- cgit v1.2.3