about summary refs log tree commit diff
path: root/uploader/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'uploader/__init__.py')
-rw-r--r--uploader/__init__.py48
1 files changed, 46 insertions, 2 deletions
diff --git a/uploader/__init__.py b/uploader/__init__.py
index cae531b..98e8141 100644
--- a/uploader/__init__.py
+++ b/uploader/__init__.py
@@ -3,19 +3,33 @@ import os
 import sys
 import logging
 from pathlib import Path
+from typing import Optional
 
 from flask import Flask, request
+
+from cachelib import FileSystemCache
+
+from gn_libs import jobs as gnlibs_jobs
+
 from flask_session import Session
 
+
 from uploader.oauth2.client import user_logged_in, authserver_authorise_uri
 
 from . import session
 from .base_routes import base
 from .files.views import files
 from .species import speciesbp
+from .publications import pubbp
 from .oauth2.views import oauth2
 from .expression_data import exprdatabp
 from .errors import register_error_handlers
+from .background_jobs import background_jobs_bp
+
+logging.basicConfig(
+    format=("%(asctime)s — %(filename)s:%(lineno)s — %(levelname)s "
+            "(%(thread)d:%(threadName)s): %(message)s")
+)
 
 def override_settings_with_envvars(
         app: Flask, ignore: tuple[str, ...]=tuple()) -> None:
@@ -50,10 +64,26 @@ def setup_logging(app: Flask) -> Flask:
         "SERVER_SOFTWARE", "").split('/')
     return __log_gunicorn__(app) if bool(software) else __log_dev__(app)
 
+def setup_modules_logging(app_logger, modules):
+    """Setup module-level loggers to the same log-level as the application."""
+    loglevel = logging.getLevelName(app_logger.getEffectiveLevel())
+    for module in modules:
+        _logger = logging.getLogger(module)
+        _logger.setLevel(loglevel)
+
+
+def create_app(config: Optional[dict] = None):
+    """The application factory.
+
+    config: dict
+      Useful to override settings in the settings files and environment
+      especially in environments such as testing."""
+    if config is None:
+        config = {}
 
-def create_app():
-    """The application factory"""
     app = Flask(__name__)
+
+    ### BEGIN: Application configuration
     app.config.from_pyfile(
         Path(__file__).parent.joinpath("default_settings.py"))
     if "UPLOADER_CONF" in os.environ:
@@ -68,8 +98,19 @@ def create_app():
         if secretsfile.exists():
             # Silently ignore secrets if the file does not exist.
             app.config.from_pyfile(secretsfile)
+    app.config.update(config) # Override everything with passed in config
+    ### END: Application configuration
+
+    app.config["SESSION_CACHELIB"] = FileSystemCache(
+        cache_dir=Path(app.config["SESSION_FILESYSTEM_CACHE_PATH"]).absolute(),
+        threshold=int(app.config["SESSION_FILESYSTEM_CACHE_THRESHOLD"]),
+        default_timeout=int(app.config["SESSION_FILESYSTEM_CACHE_TIMEOUT"]))
 
     setup_logging(app)
+    setup_modules_logging(app.logger, (
+        "uploader.publications.models",
+        "uploader.publications.datatables",
+        "uploader.phenotypes.models"))
 
     # setup jinja2 symbols
     app.add_template_global(lambda : request.url, name="request_url")
@@ -86,6 +127,9 @@ def create_app():
     app.register_blueprint(files, url_prefix="/files")
     app.register_blueprint(oauth2, url_prefix="/oauth2")
     app.register_blueprint(speciesbp, url_prefix="/species")
+    app.register_blueprint(pubbp, url_prefix="/publications")
+    app.register_blueprint(background_jobs_bp, url_prefix="/background-jobs/")
 
     register_error_handlers(app)
+    gnlibs_jobs.init_app(app)
     return app