Hoot Back-End Setup

Setup

Open your Terminal application and navigate to your ~/code/ga/lectures directory:

cd ~/code/ga/lectures

Cloning the auth boilerplate

This lecture uses the Flask JWT Auth Template as starter code. The template includes code to authenticate users with JWT tokens.

Navigate to the Flask JWT Auth Template and clone the repository to your machine:

git clone https://git.generalassemb.ly/modular-curriculum-all-courses/flask-jwt-auth-template.git

Once we have the repository on our machines, we can change the name of the directory to flask-api-hoot-back-end:

mv flask-jwt-auth-template flask-api-hoot-back-end

Next, cd into your renamed directory:

cd flask-api-hoot-back-end

Finally, remove the existing .git information from this template:

rm -rf .git

💡 Removing the .git info is important as this is just a starter template provided by GA. You do not need the existing git history for this project.

Open the project’s folder in your code editor:

code .

GitHub setup

To add this project to GitHub, initialize a new Git repository:

git init
git add .
git commit -m "Initial commit"

Make a new repository on GitHub named flask-api-hoot-back-end.

Link your local project to your remote GitHub repo:

git remote add origin https://github.com/<github-username>/flask-api-hoot-back-end.git
git push origin main

🚨 Do not copy the above command. It will not work. Your GitHub username will replace <github-username> (including the < and >) in the URL above.

Install dependencies

Next, you will want to install all of the packages listed in the Pipfile:

pipenv install

Activate the virtual environment:

pipenv shell

Create your .env

Run the following command in your terminal:

touch .env

Add the following secret keys to your .env file:

JWT_SECRET=<your-personal-secret-string>
POSTGRES_USERNAME=<your-username>
POSTGRES_PASSWORD=<your-password>
POSTGRES_DATABASE="flask_hoot"

💡 Notice the POSTGRES_DATABASE variable included here.

Run the application

Add the following route to app.py:

# Index route:
@app.route('/')
def index():
  return "Hello, world!"

Start the application with the following command:

python app.py

In your browser, send a GET request to http://127.0.0.1:5000/ to hit the index route.

To deactivate the virtual environment when you’re done, simply type:

exit

Creating the SQL Database for this project

Let’s take a moment to setup our initial database.

We’ll need to execute SQL commands to create a flask_hoot database and users table.

Activate the psql shell with the following command:

psql

Create the database:

CREATE DATABASE flask_hoot;

Connect to the database:

\c flask_hoot

And add the users table:

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    password VARCHAR(255) NOT NULL
);

😎 Remember, you can exit the psql shell by running \q in your terminal.

Next, we’ll modify the connection code in app.py to connect to the flask_hoot database. We can accomplish this with the use of our POSTGRES_DATABASE environment variable.

Update app.py with the following:

def get_db_connection():
    connection = psycopg2.connect(
        host='localhost',
        database=os.getenv('POSTGRES_DATABASE'),
        user=os.getenv('POSTGRES_USERNAME'),
        password=os.getenv('POSTGRES_PASSWORD')
    )
    return connection