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.py54
1 files changed, 46 insertions, 8 deletions
diff --git a/gn3/app.py b/gn3/app.py
index c8f0c5a..9a4269c 100644
--- a/gn3/app.py
+++ b/gn3/app.py
@@ -1,6 +1,7 @@
 """Entry point from spinning up flask"""
 import os
 import sys
+import json
 import logging
 from pathlib import Path
 
@@ -10,7 +11,7 @@ from typing import Union
 from flask import Flask
 from flask_cors import CORS  # type: ignore
 
-from gn3.loggers import setup_app_handlers
+from gn3.loggers import loglevel, setup_app_logging, setup_modules_logging
 from gn3.api.gemma import gemma
 from gn3.api.rqtl import rqtl
 from gn3.api.general import general
@@ -26,19 +27,49 @@ from gn3.api.search import search
 from gn3.api.metadata import metadata
 from gn3.api.sampledata import sampledata
 from gn3.api.llm import gnqa
-from gn3.auth import oauth2
-from gn3.case_attributes import caseattr
+from gn3.api.rqtl2 import rqtl2
+from gn3.api.streaming import streaming
+from gn3.api.case_attributes import caseattr
+from gn3.api.lmdb_sample_data import lmdb_sample_data
+
+
+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 = {
+        "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__)
     # Load default configuration
-    app.config.from_object("gn3.settings")
+    app.config.from_file(
+        Path(__file__).absolute().parent.joinpath("settings.json"),
+        load=json.load)
 
     # 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:
@@ -52,7 +83,10 @@ 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
-    setup_app_handlers(app)
+    verify_app_config(app)
+    setup_app_logging(app)
+    setup_modules_logging(loglevel(app), ("gn3.db.menu",
+                                          "gn_libs.mysqldb"))
     # DO NOT log anything before this point
     logging.info("Guix Profile: '%s'.", os.environ.get("GUIX_PROFILE"))
     logging.info("Python Executable: '%s'.", sys.executable)
@@ -61,7 +95,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")
@@ -76,9 +112,11 @@ def create_app(config: Union[Dict, str, None] = None) -> Flask:
     app.register_blueprint(search, url_prefix="/api/search")
     app.register_blueprint(metadata, url_prefix="/api/metadata")
     app.register_blueprint(sampledata, url_prefix="/api/sampledata")
-    app.register_blueprint(oauth2, url_prefix="/api/oauth2")
     app.register_blueprint(caseattr, url_prefix="/api/case-attribute")
     app.register_blueprint(gnqa, url_prefix="/api/llm")
+    app.register_blueprint(rqtl2, url_prefix="/api/rqtl2")
+    app.register_blueprint(streaming, url_prefix="/api/stream")
+    app.register_blueprint(lmdb_sample_data, url_prefix="/api/lmdb")
 
     register_error_handlers(app)
     return app