Skip to content

Commit f03c861

Browse files
committed
refactor: streamline environment variable retrieval for database and broker URLs
1 parent 3665c6d commit f03c861

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

invenio_config/env.py

+11-14
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.
@@ -68,11 +68,9 @@ def build_db_uri():
6868
"""
6969
default_uri = "postgresql+psycopg2://invenio-app-rdm:invenio-app-rdm@localhost/invenio-app-rdm"
7070

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

7775
db_params = _get_env_var(
7876
"INVENIO_DB", ["user", "password", "host", "port", "name", "protocol"]
@@ -97,9 +95,9 @@ def build_broker_url():
9795
"""
9896
default_url = "amqp://guest:guest@localhost:5672/"
9997

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

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

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

133130
redis_params = _get_env_var(
134131
"INVENIO_KV_CACHE", ["host", "port", "password", "protocol"]

0 commit comments

Comments
 (0)