Skip to content

Commit e346972

Browse files
committed
refactor: streamline environment variable retrieval for database and broker URLs
1 parent 3c8c7f6 commit e346972

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

invenio_config/env.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# This file is part of Invenio.
44
# Copyright (C) 2015-2018 CERN.
5-
# Copyright (C) 2024 KTH Royal Institute of Technology.
5+
# Copyright (C) 2024-2025 KTH Royal Institute of Technology.
66
#
77
# Invenio is free software; you can redistribute it and/or modify it
88
# under the terms of the MIT License; see LICENSE file for more details.
@@ -66,11 +66,9 @@ def build_db_uri():
6666
"""
6767
default_uri = "postgresql+psycopg2://invenio-app-rdm:invenio-app-rdm@localhost/invenio-app-rdm"
6868

69-
uri = os.environ.get("INVENIO_SQLALCHEMY_DATABASE_URI") or os.environ.get(
70-
"SQLALCHEMY_DATABASE_URI"
71-
)
72-
if uri:
73-
return uri
69+
for key in ["INVENIO_SQLALCHEMY_DATABASE_URI", "SQLALCHEMY_DATABASE_URI"]:
70+
if uri := os.environ.get(key):
71+
return uri
7472

7573
db_params = _get_env_var(
7674
"INVENIO_DB", ["user", "password", "host", "port", "name", "protocol"]
@@ -95,9 +93,9 @@ def build_broker_url():
9593
"""
9694
default_url = "amqp://guest:guest@localhost:5672/"
9795

98-
broker_url = os.environ.get("INVENIO_BROKER_URL") or os.environ.get("BROKER_URL")
99-
if broker_url:
100-
return broker_url
96+
for key in ["INVENIO_BROKER_URL", "BROKER_URL"]:
97+
if broker_url := os.environ.get(key):
98+
return broker_url
10199

102100
broker_params = _get_env_var(
103101
"INVENIO_AMQP_BROKER", ["user", "password", "host", "port", "protocol"]
@@ -122,11 +120,10 @@ def build_redis_url(db=None):
122120
db = db if db is not None else 0
123121
default_url = f"redis://localhost:6379/{db}"
124122

125-
cache_url = os.environ.get("INVENIO_CACHE_REDIS_URL") or os.environ.get(
126-
"CACHE_REDIS_URL"
127-
)
128-
if cache_url and cache_url.startswith(("redis://", "rediss://", "unix://")):
129-
return cache_url
123+
for key in ["INVENIO_CACHE_REDIS_URL", "CACHE_REDIS_URL"]:
124+
if cache_url := os.environ.get(key):
125+
if cache_url.startswith(("redis://", "rediss://", "unix://")):
126+
return cache_url
130127

131128
redis_params = _get_env_var(
132129
"INVENIO_KV_CACHE", ["host", "port", "password", "protocol"]

0 commit comments

Comments
 (0)