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:
parent
30ca6902c6
commit
bb005f2b38
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in New Issue