config: allow spreading config over multiple files

This commit enables spreading the config in /etc/sr.ht - and, crucially,
_only_ in /etc/sr.ht - over multiple .ini files.

As before, if a file ./config.ini is found, it (and only it) is loaded
and any config in /etc is ignored.

Spreading the config over multiple files will make it much easier to
create containerized versions, where e.g. different secrets can be made
available in different files, but rendering it all into one big file
would require some preprocessing.
This commit is contained in:
Conrad Hoffmann 2023-12-12 12:26:08 +01:00
parent 30ca6902c6
commit bb005f2b38
1 changed files with 13 additions and 6 deletions

View File

@ -1,3 +1,4 @@
from glob import glob
from urllib.parse import urlparse
from configparser import ConfigParser
from werkzeug.local import LocalProxy
@ -11,17 +12,23 @@ _config = None
config = LocalProxy(lambda: _config)
def load_one(path):
try:
with open(path) as f:
_config.read_file(f)
return True
except FileNotFoundError:
return False
def load_config():
global _config
_config = ConfigParser()
paths = ["config.ini", "/etc/sr.ht/config.ini"]
# Loads _either_ config.ini _or_ all .ini files in /etc/sr.ht
paths = ["config.ini", "/etc/sr.ht/*.ini"]
for path in paths:
try:
with open(path) as f:
_config.read_file(f)
loaded = any(map(lambda p: load_one(p), glob(path)))
if loaded:
break
except FileNotFoundError:
pass
load_config()