aleph icon indicating copy to clipboard operation
aleph copied to clipboard

FEATURE: Use an global object with a constructor for Flask settings intead of the module `settings.py`

Open brassy-endomorph opened this issue 3 years ago • 0 comments

Is your feature request related to a problem? Please describe.

I cannot set arbitrary environment variables and in particular I need to set SQLALCHEMY_POOL_RECYCLE.

Describe the solution you'd like

The module settings.py should not be the object that is used to configure the flask app. It should (ideally) be injected as parameter and passed around to create_app and create_view functions, but as less invasive method, having a global object like this in settings.py will be minimal refactoring.

# in settings.py

class Settings:
    def __init__(self) -> None:
        self.SOME_VAR = env.get("ALEPH_SOME_VAR")
        
        other_var = env.get("ALEPH_OTHER_VAR")
        if other_var is not None:
            self.OTHER_VAR = other_var

        # etc.

SETTINGS = Settings()

# in core.py
def create_app(config={}):
    configure_logging(level=logging.DEBUG)
    app = Flask("aleph")
    app.config.from_object(settings.SETTINGS)

Describe alternatives you've considered

In theory all this logic could be done in the module itself, but I've been doing flask development for a decade, and I genuinely think that many of the patterns in the flask docs are weird and obtuse, especially in large production applications.

Not that a single loop over all ALEPH_* env vars would not be enough as it seems that the aleph devs want to be able to set some defaults and also some flask extensions don't coerce strings to ints so this would have to be handled by known cases.

In some applications I've worked on in the past, we'd treated all env vars as JSON and then we could loop over them more efficiently and arbitrarily and doing a json.loads(env_var). This would be a breaking change (maybe) depending on how the helm charts are constructed.

brassy-endomorph avatar Aug 01 '22 07:08 brassy-endomorph