zerasul / blask

Blog Engine based on Flask
https://getblask.com/
GNU General Public License v3.0
41 stars 28 forks source link

Convert from settings.py to an .env file #32

Open picsldev opened 6 years ago

picsldev commented 6 years ago

pipenv reads the .env files automatically, loading the environment variables that we put into them.

It is a very powerful way to correctly configure an application, following the indications of "The Twelve-Factor App".

An example of a settings.py file converted to an .env file could be the following:

desarrollo@desarrollo:~/Proyectos/pipenv/blask$ cat .env

# Minimal conf for Blask
FLASK_APP=main.py

# Name of the Template Folder.
templateDir="templates"

# Name of the post Folder
postDir="posts"

# default name of the template file.
defaultLayout="template.html"

# Default name of the static Folder
staticDir="static"

# Title of the blog
tittle="Blask | A Simple Blog Engine Based on Flask"

All the environment variables that we want to define, can be included in the .env file.

.gitignore excludes the.env file so that it can not be versioned, so that we can store sensitive data inside it.

A very popular custom is to create a file .env.example, with sample data, so that other developers find it very easy to generate their own.env file from the .env.example file .

zerasul commented 6 years ago

This can be a Good issue. Use enviorement variables instead a py file. Of course, there is another issue for use it with a Dictionary using a json file (#20). So i think we can use enviorement variables too.

JuanjoSalvador commented 6 years ago

Seems fixed on #31

JuanjoSalvador commented 6 years ago

Hey, how can we read this settings (from .env file) to use them on Blask?

mhered commented 3 years ago

Hola Victor creo que quizas podr'ia ayudar con esto, con un poco de guia por tu parte

mhered commented 3 years ago

Hola Victor. Creo que tengo claro como hacerlo. Entiendo que queremos que settings.py siga funcionando para tener retrocompatibilidad, pero ¿con que precedencia?

La opcion de definir la configuration en settings.py va a seguir funcionando y tendrá precedencia cuando se importe settings y se llame a BlaskApp con parámetros:

    from blask import BlaskApp
    import settings

    if __name__ == '__main__':
        b = BlaskApp(templateDir=settings.templateDir, postDir=settings.postDir
                  , defaultLayout=settings.defaultLayout,
              staticDir=settings.staticDir, title=settings.title, errors={404:'404'})
        b.run()

Sin embargo, si se define la variable de entorno BLASK_SETTINGS y tambien hay un .env, ¿a cual de las dos opciones doy prioridad? ¿Permito que se combinen variables de las dos fuentes? (ej. usar defaultLayout de settings.py y title de .env)