chavli / heroku-python-boilerplate

All the essential code you need to deploy a working Python Flask API to Heroku, with a PostgreSQL database, within minutes.
GNU General Public License v3.0
8 stars 2 forks source link
alembic backend flask heroku postgresql-database python3 sqlalchemy tutorial

(Draft) The instructions work they just need to be polished.

Introduction

You scream, I scream, we all scream for REST API's! Whether for fun or for work a REST API exposes backend resources to frontend clients. If you have even a little development experience then you can have a working API deployed in minutes!

You can even expand this example into production ready code!

Motivation and Purpose

I've noticed over the years of working on backend code that there aren't many meaningful examples of backend code that illustrates how everything works together.Of course, there are countless code pieces showing discrete parts of a functional backend and even small examples of a working "hello world" endpoint but it's still up to the developer to tie these pieces together, and configure, anything meaningful. Not everything is hopeless, however. Companies are making it easier to setup your own server [references needed] and creating the tools to easily deploy live code but the question still remains what code do you deploy?

This is the question I want to try to answer. I think this disconnect contributes to an environment that makes it hard to learn backend development and encourages a misconception that backend code is "hard" or "confusing".

I want to achieve a few things by sharing this code:

  1. create a non-trivial, but still minimal, codebase illustrating some core concepts of REST API development and tieing them together
  2. demonstrate the process of deploying code to a live environment, in this case to Heroku.
  3. save myself, and others, time when it comes to setting up a backend for hackathons and small projects.

Requirements

Optional

Included Features:

The code as-is comes with these features already built in:

Setup Instructions

Heroku Setup

You can do all this through the Heroku dashboard but it's quicker to do it all through your terminal. The following commands create your Heroku app, configure a few variables, and attach a PostgreSQL database. Replace your_app_name with anything you want. JWT_SECRET can be set to any value, this is used to create web tokens. JWT_ISS can also be set to any value and represents who is issuing the token.

heroku login
heroku apps:create your_app_name
heroku config:set JWT_SECRET="shh secret value!" --app your_app_name
heroku config:set JWT_ISS="project/team name" --app your_app_name
heroku addons:create heroku-postgresql --app your_app_name

Local Environment

Great, now lets setup your local environment for development!

Create a folder (the root folder of your project), and go into it:

git clone https://github.com/chavli/heroku-python-boilerplate.git .
git remote add heroku git@heroku.com:your_app_name.git

Once you have the code, you have to create a virtual environment and install dependencies:

virtualenv venv -p /usr/bin/python3.6
source venv/bin/activate
pip install -r requirements.txt

In the future, before working on your local copy of the code, make sure you're in the virtual environment by executing source venv/bin/activate from the root folder.

Now you need a local copy of all the environment variables your Heroku app has:

heroku config --app your_app_name --shell > .env
cp .env .bash_env

You'll have to manually prepend export to each line in .bash_env.

Initializing the Database

The last step! These commands will create 4 tables in the PostgreSQL database for tracking accounts, sessions, and various logs:

source .bash_env
alembic upgrade head

Testing Locally

Running either of these commands will start a local server on localhost:5000. If everything works then going to localhost:5000/api/hello will return {"hello": "world"}

heroku local web
- OR -
python rundebug.py

Deploying your Code

Now it's time for the main attraction! Pushing your code to Heroku and making it live! Commit any changes you made, if any, and push it directly to Heroku:

git push heroku master

In minutes your endpoints will be live on: https://your_app_name.herokuapp.com

Out-of-the-box Endpoints

Postman is the easiest way to test these endpoints.

Core Libraries

Advanced

Updating the Schema

alembic revision --autogenerate -m "update details"
alembic upgrade head