about summary refs log tree commit diff
path: root/gn3/app.py
diff options
context:
space:
mode:
Diffstat (limited to 'gn3/app.py')
-rw-r--r--gn3/app.py37
1 files changed, 35 insertions, 2 deletions
diff --git a/gn3/app.py b/gn3/app.py
index e9a2bbe..57d9fa0 100644
--- a/gn3/app.py
+++ b/gn3/app.py
@@ -1,4 +1,5 @@
 """Entry point from spinning up flask"""
+
 import os
 import sys
 import logging
@@ -29,6 +30,35 @@ from gn3.api.llm import gnqa
 from gn3.case_attributes import caseattr
 
 
+class ConfigurationError(Exception):
+    """Raised in case of a configuration error."""
+
+
+def verify_app_config(app: Flask) -> None:
+    """Verify that configuration variables are as expected
+    It includes:
+        1. making sure mandatory settings are defined
+        2. provides examples for what to set as config variables (helps local dev)
+    """
+    app_config = {
+        "BCRYPT_SALT": """set BCRYPT_SALT to $2b$12$mxLvu9XRLlIaaSeDxt8Sle for local dev work
+        or run `python3 -c "import bcrypt; print(bcrypt.gensalt().decode())` to get a new one
+        """,
+        "AUTH_SERVER_URL": """AUTH_SERVER_URL is used for api requests that need login.
+        For local dev, use the running auth server url, which defaults to http://127.0.0.1:8081
+        """,
+    }
+    error_message = []
+
+    for setting, err in app_config.items():
+        print(f"{setting}: {app.config.get(setting)}")
+        if setting in app.config and bool(app.config[setting]):
+            continue
+        error_message.append(err)
+    if error_message:
+        raise ConfigurationError("\n".join(error_message))
+
+
 def create_app(config: Union[Dict, str, None] = None) -> Flask:
     """Create a new flask object"""
     app = Flask(__name__)
@@ -37,7 +67,7 @@ def create_app(config: Union[Dict, str, None] = None) -> Flask:
 
     # Load environment configuration
     if "GN3_CONF" in os.environ:
-        app.config.from_envvar('GN3_CONF')
+        app.config.from_envvar("GN3_CONF")
 
     # Load app specified configuration
     if config is not None:
@@ -51,6 +81,7 @@ def create_app(config: Union[Dict, str, None] = None) -> Flask:
     if secrets_file and Path(secrets_file).exists():
         app.config.from_envvar("GN3_SECRETS")
     # END: SECRETS
+    verify_app_config(app)
     setup_app_handlers(app)
     # DO NOT log anything before this point
     logging.info("Guix Profile: '%s'.", os.environ.get("GUIX_PROFILE"))
@@ -60,7 +91,9 @@ def create_app(config: Union[Dict, str, None] = None) -> Flask:
         app,
         origins=app.config["CORS_ORIGINS"],
         allow_headers=app.config["CORS_HEADERS"],
-        supports_credentials=True, intercept_exceptions=False)
+        supports_credentials=True,
+        intercept_exceptions=False,
+    )
 
     app.register_blueprint(general, url_prefix="/api/")
     app.register_blueprint(gemma, url_prefix="/api/gemma")