feat(settings): enhance configuration handling

Updated settings to ensure "ColdBrew" section exists in the config before accessing it. Defaulted `FIELD_ENCRYPTION_KEY` to a generated value if not present and corrected encoding. Made `ALLOWED_HOSTS` configurable via settings file and adjusted `DEBUG` logic to depend on `ALLOWED_HOSTS`.
This commit is contained in:
Kumi 2024-07-12 08:41:33 +02:00
parent d2893c3ffa
commit 11e8fe8912
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -23,15 +23,18 @@ BASE_DIR = Path(__file__).resolve().parent.parent
ASK = AutoSecretKey("settings.ini")
SECRET_KEY = ASK.secret_key
CONFIG = ASK.config
DEBUG = CONFIG.getboolean("ColdBrew", "Debug", fallback=False)
if not (FIELD_ENCRYPTION_KEY := CONFIG.get("ColdBrew", "EncryptionKey")):
FIELD_ENCRYPTION_KEY = base64.urlsafe_b64encode(os.urandom(32))
if "ColdBrew" not in CONFIG:
CONFIG["ColdBrew"] = {}
if not (FIELD_ENCRYPTION_KEY := CONFIG.get("ColdBrew", "EncryptionKey", fallback=None)):
FIELD_ENCRYPTION_KEY = base64.urlsafe_b64encode(os.urandom(32)).decode()
CONFIG["ColdBrew"]["EncryptionKey"] = FIELD_ENCRYPTION_KEY
ASK.write()
ALLOWED_HOSTS = []
ALLOWED_HOSTS = CONFIG.get("ColdBrew", "AllowedHosts", fallback="*").split(",")
DEBUG = CONFIG.getboolean("ColdBrew", "Debug", fallback=False) if ALLOWED_HOSTS else True
# Application definition