Added support for dict/json environment variables for docker image

This commit is contained in:
Rauno Tuul 2023-03-10 15:20:18 +02:00
parent aa70951964
commit c707f1e1c5
2 changed files with 18 additions and 2 deletions

View File

@ -27,6 +27,7 @@ legal_envvars = (
'SALT', 'SALT',
'SQLALCHEMY_TRACK_MODIFICATIONS', 'SQLALCHEMY_TRACK_MODIFICATIONS',
'SQLALCHEMY_DATABASE_URI', 'SQLALCHEMY_DATABASE_URI',
'SQLALCHEMY_ENGINE_OPTIONS',
'MAIL_SERVER', 'MAIL_SERVER',
'MAIL_PORT', 'MAIL_PORT',
'MAIL_DEBUG', 'MAIL_DEBUG',
@ -98,14 +99,25 @@ legal_envvars_bool = (
'CAPTCHA_ENABLE', 'CAPTCHA_ENABLE',
) )
legal_envvars_dict = (
'SQLALCHEMY_ENGINE_OPTIONS',
)
# import everything from environment variables # import everything from environment variables
import os import os
import sys import sys
import json
def str2bool(v): def str2bool(v):
return v.lower() in ("true", "yes", "1") return v.lower() in ("true", "yes", "1")
def dictfromstr(v,ret):
try:
return json.loads(ret)
except Exception as e:
print('Cannot parse json {} for variable {}'.format(ret, v))
print(e)
raise ValueError
for v in legal_envvars: for v in legal_envvars:
@ -129,4 +141,6 @@ for v in legal_envvars:
ret = str2bool(ret) ret = str2bool(ret)
if v in legal_envvars_int: if v in legal_envvars_int:
ret = int(ret) ret = int(ret)
if v in legal_envvars_dict:
ret = dictfromstr(v, ret)
sys.modules[__name__].__dict__[v] = ret sys.modules[__name__].__dict__[v] = ret

View File

@ -57,6 +57,8 @@
| SESSION_COOKIE_SECURE | | SESSION_COOKIE_SECURE |
| SIGNUP_ENABLED | | SIGNUP_ENABLED |
| SQLALCHEMY_DATABASE_URI | SQL Alchemy URI to connect to database | N | no default | | SQLALCHEMY_DATABASE_URI | SQL Alchemy URI to connect to database | N | no default |
| SQLALCHEMY_TRACK_MODIFICATIONS | | SQLALCHEMY_TRACK_MODIFICATIONS |
| SQLALCHEMY_ENGINE_OPTIONS | json string. e.g. '{"pool_recycle":600,"echo":1}' [^2] |
[^1]: Flask secret key (see https://flask.palletsprojects.com/en/1.1.x/config/#SECRET_KEY for how to generate) [^1]: Flask secret key (see https://flask.palletsprojects.com/en/1.1.x/config/#SECRET_KEY for how to generate)
[^2]: See Flask-SQLAlchemy Documentation for all engine options.