02 - Chief Configuration
In this module we will be setting up the different configurations we will run our API on
Last updated
Was this helpful?
In this module we will be setting up the different configurations we will run our API on
Last updated
Was this helpful?
Add the following code to your src/config.py
file
OK, what in the world did we just create. We defined the classes we will be using to configure what aspects of our application will be available based on what stage we are in. So obviously when we are creating the app and messing with our endpoints, we will want to use development. However, when we deploy our API, we should probably use production. We will see where this is called later on when we write our script to run our app. The biggest takeaway from this file, is the os.getenv()
functions. This use what we call Environment Variables. Environment Variables allow us to hide secret stuff on our machines, and use them in our code. This is EXCRUCIATINGLY IMPORTANT. Do you want to store a secret key, or even a to an online account, in your source control repository? Probably not! Environment Variables allow us to plug variables on our machines and be accessed later on. Let's create the ones that we need now
For mac/linux, use the export
command and run the following commands in your terminal
export FLASK_ENV=development
export JWT_SECRET_KEY=thisIsYourSecretKey
For Windows, use the following commands
$env:FLASK_ENV="development"
$env:JWT_SECRET_KEY="thisIsYourSecretKey"
Now we are only missing one, our database url. But, we don't have a database yet. Let's fix that. We will create a database called flask_blog
for this demo. If you are using PGAdmin, you can use the GUI Tool to create a Database, and name it flask_blog
. This will be the same as we did for Django. If you are on Mac/Linux, and using the PSQL CLI, you can use the command CREATE DATABASE flask_blog;
. Now we can head back to the command line and finish up our environment variables. Note that you will have to configure these every time you restart, shutdown, etc. However, this is a lot better than having your secret key in your remote repositories.
Mac/Linux
export DATABASE_URL=postgres://{username}:{password}@localhost/flask_blog
Windows
$env:DATABASE_URL="postgres://{username}:{password}@localhost/flask_blog
where username
and password
is your username and password. Note that if you have not set up a new user for postgres, your username will be postgres
Debugging this will be a bit tricky if it does not work the first time. This is because all postgres installations are different, and all machines are different. We will know later on when we build the API and perform migrations on the database to know for sure if its working or not