Flask Bootstrap
This repo contains a skeleton flask project you can use to quickly bootstrap your projects. It's designed to be easily deployed to serverless environments or container clusters (see DevOps below)
Currently we have two branches:
master
Simple Flask App with Restful API documented by Swagger
flask-bootstrap-postgres
Builds on the simple app in master, adds a Postgres DB, with user registration and authorisation.
JSON parsing and validating is handled by Schematics ORM is SqlAlchemy. DB migrations handled by Flask-Migrate. Authorisation handled by Flask-HTTPAuth and PyJWT
Installing
Python 3.8+
You need to have at least Python 3.8 installed in your development environment.
Dependencies
To install the required dependencies:
pip install pipenv
pipenv install --dev
pre-commit install
Note the app uses pre-commit to ensure we automatically run the black code formatter and flake8 linter before each commit
Running Flask Bootstrap
Create a .env file
Flask-Bootstrap installs python-dotenv to manage local environment variables it is strongly advised to create a .env
file in the root of your project to store all environment variables.
Ensure you set the following (showing sample values):
AWS_ACCESS_KEY=awsaccess
AWS_SECRET_KEY=awsSecret
AWS_REGION=us-east-2 [Defaults to us-east-1]
FLASK_SECRET=secretkeyhere [Creates Entropy for tokens etc]
Running
To run ensure your virtual environment is running
pipenv shell
flask run
Tests
To run all tests
pytest tests
API Docs
API docs are generated by Flasgger all API methods should be decorated with swagger comments. API docs page can be viewed here:
DevOps
Database Migrations
Project uses Flask-Migrate for db migrations. You can generate new migrations as follows:
flask db migrate
# Migrations can be manually applied with
flask db upgrade
Note that migrations will automatically be applied when the project runs
Docker
Flask-Bootstrap can be built and run from within a Docker container:
docker-compose up
You should see the app running locally on port 8080 - http://127.0.0.1:8080
To build the production version which includes a nginx sidecar run the following:
docker-compose -f docker-compose.prod.yml up -d --build
Production build runs on port 80, so can be smoketested on http://127.0.0.1:80
Useful commands:
# Connect to the running container
docker exec -it <imageId> bash
# Rebuild the container
docker-compose build
CI
Flask-Bootstrap CI is run by Shippable. New CI scripts can be added within the devops
dir
Thanks
The work in this repo has been influenced by Miguel Grinberg, if you are learning flask check out his Mega Tutorial