Python - 301 - Flask
  • 00 - Appealing API's
  • 01- A Smooth Start
  • 02 - Chief Configuration
  • 03 - Example Endpoint and an Adornable App
  • 04 - Modeling the Models
  • 05 - Scheming Schemas
  • 06 - Making Migrations
  • 07 - Uniformed Users
  • 08 - Alarming Authentication
  • 09 - Account Actions
  • 10 - Postman Prevalence and Examining Endpoints
  • 11 - Blog Post Blogging
Powered by GitBook
On this page

Was this helpful?

02 - Chief Configuration

In this module we will be setting up the different configurations we will run our API on

Previous01- A Smooth StartNext03 - Example Endpoint and an Adornable App

Last updated 6 years ago

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