From d00f4f907050987e6762b7c61fc7ae34b23129cd Mon Sep 17 00:00:00 2001
From: Arie Peterson <arie@greenhost.nl>
Date: Thu, 8 Jun 2023 11:14:41 +0200
Subject: [PATCH] Auto-detect dev or prod mode

---
 backend/app.py     | 10 +++++-----
 backend/config.py  |  5 +----
 docker-compose.yml |  2 +-
 3 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/backend/app.py b/backend/app.py
index fd993e41..129e9353 100644
--- a/backend/app.py
+++ b/backend/app.py
@@ -96,21 +96,21 @@ def init_routines():
         cluster_config.populate_oauthclients()
 
 # `init_routines`should only run once per dashboard instance. To enforce this we
-# have a different mode for production and development mode:
+# have different behaviour for production and development mode:
 # * we have "preload" on for gunicorn, so this file is loaded only once, before
 #   workers are forked (production);
 # * we make sure that in development mode we run this only once, even though
 #   this file is loaded twice by flask for some reason.
-if DEV_MODE:
+if RUN_BY_GUNICORN:
+    logging.info("Running initialization code (production mode).")
+    init_routines()
+else:
     logging.info("WERKZEUG_RUN_MAIN: {}".format(os.environ.get("WERKZEUG_RUN_MAIN", "unset")))
     if os.environ.get("WERKZEUG_RUN_MAIN") == "true":
         logging.info("Running initialization code (dev mode).")
         init_routines()
     else:
         logging.info("Not running initialization code (dev mode).")
-else:
-    logging.info("Running initialization code (production mode).")
-    init_routines()
 
 app.register_blueprint(api_v1)
 app.register_blueprint(web)
diff --git a/backend/config.py b/backend/config.py
index b2404b94..04039e5a 100644
--- a/backend/config.py
+++ b/backend/config.py
@@ -21,9 +21,6 @@ SQLALCHEMY_TRACK_MODIFICATIONS = False
 # running in a Kubernetes pod. Set it to "false" to load the config from the
 # `KUBECONFIG` environment variable.
 LOAD_INCLUSTER_CONFIG = os.environ.get("LOAD_INCLUSTER_CONFIG").lower() == "true"
-# We use this to detect whether we run in production mode (gunicorn, as
-# specified in the docker image) or dev mode (flask run, as specified in docker
-# compose config).
-DEV_MODE = os.environ.get("DASHBOARD_DEV_MODE", "False").lower() in ('true', '1')
+RUN_BY_GUNICORN = "gunicorn" in os.environ.get("SERVER_SOFTWARE", "")
 
 DEMO_INSTANCE = os.environ.get("DASHBOARD_DEMO_INSTANCE", "False").lower() in ('true', '1')
diff --git a/docker-compose.yml b/docker-compose.yml
index be4376c2..00b2d76d 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -54,7 +54,7 @@ services:
       - "$KUBECONFIG:/.kube/config"
     depends_on:
       - kube_port_mysql
-    entrypoint: ["bash", "-c", "DASHBOARD_DEV_MODE=true flask run --host $$(hostname -i)"]
+    entrypoint: ["bash", "-c", "flask run --host $$(hostname -i)"]
   kube_port_kratos_admin:
     image: bitnami/kubectl:1.27.2
     user: "${KUBECTL_UID}:${KUBECTL_GID}"
-- 
GitLab