Set environment variables
To ensure your Flask app has everything it needs to function properly,
you need to set a few more environment variables via script.
To do so, make the following changes to the .environment file automatically generated by the Upsun CLI.
-
If you’ve added a database to your project, update the
DATABASE_URLenvironment variable to connect your app and database. To do so, locate the following line:.environmentexport DATABASE_URL="${DB_CONNECTION}://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}"And amend it to specify the type of your database. For instance, if you have a PostgreSQL database, change the line to:
.environmentexport DATABASE_URL="postgresql://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}" -
To be able to set variables that change depending on the type of environment (
production,development, orstaging), set thePLATFORM_ENVIRONMENT_TYPEenvironment variable first. To so, add the following line:.environmentexport FLASK_ENV="${PLATFORM_ENVIRONMENT_TYPE}" -
To enable the Flask debug mode on preview environments only, add the following line:
.environmentexport FLASK_DEBUG=$( [ "${PLATFORM_ENVIRONMENT_TYPE}" = "production" ] && echo 0 || echo 1)This command returns
0(disabled) on a production environment, and1(enabled) on a preview environment. -
To adjust the log level depending on the type of environment, add the following line:
.environmentexport LOG_LEVEL=$( [ "${PLATFORM_ENVIRONMENT_TYPE}" = "production" ] && echo "info" || echo "debug")This command sets the log level to
infofor your production environment, and todebugfor your preview environments. -
To adjust the cache control maximum age depending on the type of environment, add the following line:
.environmentexport SEND_FILE_MAX_AGE_DEFAULT=$( [ "${PLATFORM_ENVIRONMENT_TYPE}" = "production" ] && echo 31556926 || echo 0)This command sets the maximum age to
31556926for your production environment, and to0for your preview environments. -
Point the
SECRET_KEYenvironment variable to the Upsun-providedPLATFORM_PROJECT_ENTROPYvariable..environmentexport SECRET_KEY="${PLATFORM_PROJECT_ENTROPY}"The
SECRET_KEYenvironment variable ensures the session cookie is signed securely, and can be used for any other security-related needs by extensions or your app. -
If you want to switch from the Flask built-in server to Gunicorn later on, add the following line:
.environmentexport GUNICORN_WORKERS=1 -
To commit and push your changes, run the following command:
Terminalgit add .environment && git commit -m "Adds needed flask environmental variables"Your Flask app is now ready to be pushed to Upsun.