From 5f78a131ce0541866eded8f8de763e5f33efd9a8 Mon Sep 17 00:00:00 2001 From: Uwe Winter Date: Mon, 22 Sep 2025 13:12:29 +1000 Subject: [PATCH 1/2] read JWT and CORS from os.env. --- main.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 064fcb46..85f3df6b 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +import os from contextlib import asynccontextmanager from dotenv import dotenv_values @@ -23,10 +24,18 @@ # and load them via config.py. But we need the # allowed_origins before we load the app env_values = dotenv_values(".env") -ALLOWED_ORIGINS = [ - origin.strip() for origin in env_values.get("CORS_ALLOWED_ORIGINS", "").split(",") -] -SECRET_KEY = env_values.get("JWT_SECRET_KEY") + +def read_setting(key: str, default: str | None = None) -> str | None: + value = os.getenv(key) + if value is not None and value.strip(): + return value + from_env_file = env_values.get(key) + if from_env_file is not None and str(from_env_file).strip(): + return from_env_file + return default + +ALLOWED_ORIGINS = [origin.strip() for origin in read_setting("CORS_ALLOWED_ORIGINS", "").split(",") if origin.strip()] +SECRET_KEY = read_setting("JWT_SECRET_KEY") @asynccontextmanager From 8af26d989de323f4efb5ddbaecdb6888484bfde4 Mon Sep 17 00:00:00 2001 From: marius-mather <61438033+marius-mather@users.noreply.github.com> Date: Tue, 23 Sep 2025 10:45:47 +1000 Subject: [PATCH 2/2] Add docstring for read_setting --- main.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/main.py b/main.py index 85f3df6b..1025ff0d 100644 --- a/main.py +++ b/main.py @@ -26,6 +26,10 @@ env_values = dotenv_values(".env") def read_setting(key: str, default: str | None = None) -> str | None: + """ + Read a setting from environment variables or the .env file. + The environment variable will take precedence if present. + """ value = os.getenv(key) if value is not None and value.strip(): return value