Note
Before you start, check out the Upsun demo app and the main Getting started guide. They provide all the core concepts and common commands you need to know before using the materials below.1. Leverage environment variables
The CLI generated a.environment file during the Getting started guide.
Notice it has already created some environmental variables for you to connect to your database service.
.environment
-
Set the
FLASK_APPenvironment variable to specify how to load the app.The above example uses the file.environmentautoapp.pyas the main entry. Adjust according to your situation. -
Set the
FLASK_ENVenvironment variable to enable behaviors based on the environment type. Upsun provides information about what type of environment the app is running in via thePLATFORM_ENVIRONMENT_TYPEenvironment variable. Its values can beproduction,development, orstaging. Use this information to set the value forFLASK_ENV.Several other environmental variables need to change based on the environment type. Leverage the information in.environmentPLATFORM_ENVIRONMENT_TYPEfor these other variables too. -
Set the
FLASK_DEBUGenvironment variable to1(enabled) if you’re not running in production..environment -
Do the same for
LOG_LEVEL..environment -
Set the
SEND_FILE_MAX_AGE_DEFAULTto0(disabled) if you’re not in production, but a higher value if you are..environment -
Optional: You may also need to set a
SECRET_KEYenvironment variable. It’s used for securely signing the session cookie and can be used for any other security-related needs by extensions or your app. It usually is a long random string. Set theSECRET_KEYenvironment variable to leverage thePLATFORM_PROJECT_ENTROPYvariable provided by Upsun:Your.environment.environmentfile should now look similar to the following:.environment
2. Configure static assets
You need to add some writable disk space to hold the static assets thatflask-static-digest generates and npm builds.
To do so, define the ./<APP_NAME>:/static directory as a mount.
In your app configuration, locate the section dedicated to mounts and update it as follows:
Replace <APP_NAME>: with the app_name you defined when creating your Flask project.
3. Install dependencies and builds
Instruct Upsun to automatically runnpm install in addition to installing your Python dependencies
when building the application container.
To do so, customize your build hook.
In your app configuration, locate the section dedicated to it and update it as follows:
The Upsun CLI automatically added pip install -r requirements.txt to your build hook configuration when you
configured your Upsun project.
If you want pip to be upgraded first to the latest version, add pip install --upgrade pip to the above line.
Then, add npm install:
Note that if your project uses a different package manager, Upsun also supports several other options.
4. Configure the deploy phase
Instruct Upsun to automatically runnpm run build when building the application container.
To do so, customize your deploy hook.
In your app configuration, locate the section dedicated to it:
Add npm run build:
5. Configure the web server
Configure the web server running in front of your app to define how your app handles dynamic requests. To do so, in your app configuration, locate the section dedicated to the web server: To add a basic Flask server, replace the default information added by the Upsun CLI withflask run -p $PORT.
Also, change the socket_family value from unix to tcp:
You can now commit all of the above changes and push to Upsun.
Terminal
requirements.txt file.
6. Handle database migrations
Prepare database migrations
If you have a new Flask project that uses Flask-migrate, or an existing app but need to set up the initial migrations, you can do so using the database service you created earlier. To do so, follow these steps.-
Set up a virtual environment where your project can run:
Terminal
-
Just like in your build hook, update pip and install the requirements:
Terminal
-
Set up a way for your local instance of Flask to communicate with your database service:
This opens an SSH tunnel to all the services for the app. You can use this connection to allow your local instance to communicate with live services as if they too were local.Terminal
To do so, you need to configure more environment variables. -
Reopen your
.environmentfile. Note the use of the$PLATFORM_RELATIONSHIPSenvironment variable to retrieve information about services and their credentials.
The tunnel you created gives you access to that same data, allowing you to generate a localPLATFORM_RELATIONSHIPSenvironment variable containing the same information. Set the following environment variable:Since you now have this environmental variable set locally, you can reuse your.environment.environmentfile for Upsun to recreate many of the other environmental variables you need to run locally. -
Set the following environment variables as they aren’t set via
PLATFORM_RELATIONSHIPS:.environment -
Source your
.environmentfile to finish setting up all the environmental variables in your current bash:You now have everything you need for Flask-Migrate to be able to connect to the database and generate your migration files.Terminal
Generate database migrations
-
Initiate the migrations directory and prepare for the
migratecommand:Terminal -
Generate your migrations:
Terminal
-
Commit your generated migrations:
Terminal
-
Instruct Upsun to run the Flask-migrate upgrade command when deploying
so any migration changes are automatically applied.
To do so, re-open your.upsun/config.yamlfile. Locate thedeployhook where you addednpm run buildpreviously and update it as follows: -
Commit all your changes and push to Upsun.
Terminal