Skip to content

Commit 336e9e7

Browse files
committed
refactor: add prefix support
1 parent e346972 commit 336e9e7

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

invenio_config/env.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,21 @@ def init_app(self, app):
4646
app.config[varname] = value
4747

4848

49-
def _get_env_var(prefix, keys):
49+
def _get_env_var(prefix: str, keys: list) -> dict:
5050
"""Retrieve environment variables with a given prefix."""
5151
return {k: os.environ.get(f"{prefix}_{k.upper()}") for k in keys}
5252

5353

54-
def build_db_uri():
54+
def _check_prefix(prefix: str) -> str:
55+
"""Check if prefix ends with an underscore and remove it."""
56+
if not prefix:
57+
return "INVENIO"
58+
if prefix.endswith("_"):
59+
prefix = prefix[:-1]
60+
return prefix
61+
62+
63+
def build_db_uri(prefix="INVENIO"):
5564
"""
5665
Build database URI from environment variables or use default.
5766
@@ -64,14 +73,15 @@ def build_db_uri():
6473
Note: For option 3, to assert that the INVENIO_DB_* settings take effect,
6574
you need to set SQLALCHEMY_DATABASE_URI="" in your environment.
6675
"""
76+
prefix = _check_prefix(prefix)
6777
default_uri = "postgresql+psycopg2://invenio-app-rdm:invenio-app-rdm@localhost/invenio-app-rdm"
6878

69-
for key in ["INVENIO_SQLALCHEMY_DATABASE_URI", "SQLALCHEMY_DATABASE_URI"]:
79+
for key in [f"{prefix}_SQLALCHEMY_DATABASE_URI", "SQLALCHEMY_DATABASE_URI"]:
7080
if uri := os.environ.get(key):
7181
return uri
7282

7383
db_params = _get_env_var(
74-
"INVENIO_DB", ["user", "password", "host", "port", "name", "protocol"]
84+
f"{prefix}_DB", ["user", "password", "host", "port", "name", "protocol"]
7585
)
7686
if all(db_params.values()):
7787
uri = f"{db_params['protocol']}://{db_params['user']}:{db_params['password']}@{db_params['host']}:{db_params['port']}/{db_params['name']}"
@@ -80,7 +90,7 @@ def build_db_uri():
8090
return default_uri
8191

8292

83-
def build_broker_url():
93+
def build_broker_url(prefix="INVENIO"):
8494
"""
8595
Build broker URL from environment variables or use default.
8696
@@ -91,23 +101,25 @@ def build_broker_url():
91101
4. Default URL
92102
Note: see: https://docs.celeryq.dev/en/stable/userguide/configuration.html#new-lowercase-settings
93103
"""
104+
prefix = _check_prefix(prefix)
94105
default_url = "amqp://guest:guest@localhost:5672/"
95106

96-
for key in ["INVENIO_BROKER_URL", "BROKER_URL"]:
107+
for key in [f"{prefix}_BROKER_URL", "BROKER_URL"]:
97108
if broker_url := os.environ.get(key):
98109
return broker_url
99110

100111
broker_params = _get_env_var(
101-
"INVENIO_AMQP_BROKER", ["user", "password", "host", "port", "protocol"]
112+
f"{prefix}_AMQP_BROKER", ["user", "password", "host", "port", "protocol"]
102113
)
103114
if all(broker_params.values()):
104-
vhost = f"{os.environ.get('INVENIO_AMQP_BROKER_VHOST').removeprefix('/')}"
115+
broker_env_var = f"{prefix}_AMQP_BROKER_VHOST"
116+
vhost = f"{os.environ.get(broker_env_var).removeprefix('/')}"
105117
broker_url = f"{broker_params['protocol']}://{broker_params['user']}:{broker_params['password']}@{broker_params['host']}:{broker_params['port']}/{vhost}"
106118
return broker_url
107119
return default_url
108120

109121

110-
def build_redis_url(db=None):
122+
def build_redis_url(db=None, prefix="INVENIO"):
111123
"""
112124
Build Redis URL from environment variables or use default.
113125
@@ -117,16 +129,17 @@ def build_redis_url(db=None):
117129
3. INVENIO_KV_CACHE_* specific environment variables
118130
4. Default URL
119131
"""
132+
prefix = _check_prefix(prefix)
120133
db = db if db is not None else 0
121134
default_url = f"redis://localhost:6379/{db}"
122135

123-
for key in ["INVENIO_CACHE_REDIS_URL", "CACHE_REDIS_URL"]:
136+
for key in [f"{prefix}_CACHE_REDIS_URL", "CACHE_REDIS_URL"]:
124137
if cache_url := os.environ.get(key):
125138
if cache_url.startswith(("redis://", "rediss://", "unix://")):
126139
return cache_url
127140

128141
redis_params = _get_env_var(
129-
"INVENIO_KV_CACHE", ["host", "port", "password", "protocol"]
142+
f"{prefix}_KV_CACHE", ["host", "port", "password", "protocol"]
130143
)
131144

132145
if redis_params["host"] and redis_params["port"]:

0 commit comments

Comments
 (0)