Cookiecutter for Flask API
Overview
Reusable template to quickly run your Flask API with or without a Database. It supports DynamoDB for now.
Software design
Software design is based on Clean Architecture from this post. Goal of the design is to make use of repositories and models so that it can conveniently switch data sources.
Supported options
- Flask API only (without repositories and models)
- Flask API with DynamoDB
Getting Started
-
Install cookiecutter.
-
Run the following command:
cookiecutter git@github.com:ardydedase/cookiecutter-flask-api.git
-
Follow the instructions in the command line.
Development
Run with Docker
Pre-requisites:
- Install Docker
Run the following command:
docker-compose up
After running docker compose, you might see the following:
pynamodb.exceptions.TableError: Unable to describe table: Could not connect to the endpoint URL: "http://db:8000/"
This is because Flask is trying to connect to DynamoDB while the table is not yet available. This can be ignored because Flask will retry to connect after failure.
Local endpoints:
Run locally
Pre-requisites:
- Download DynamoDB Local
- Python 3.6
- Install virtualenv
Steps:
Skip the steps that you don't need.
- Run the local DynamoDB
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar
- Setup your virtual environment. Guide here.
- Install the required packages:
pip install -r requirements.txt
- Populate the DynamoDB table.
FLASK_ENV=development FLASK_APP=app/cli.py flask dynamo create-poem-table
- Run Flask locally:
FLASK_ENV=development FLASK_APP=app/main.py FLASK_DEBUG=1 flask run
Testing locally
Checkout this repo and Run the following:
cookiecutter cookiecutter-flask-dynamodb
Production build
Run with gunicorn.
FLASK_ENV=production gunicorn AWS_ACCESS_KEY_ID="your-key-here" --build-arg AWS_SECRET_ACCESS_KEY="your-key-here" --bind 0.0.0.0:5000 wsgi
Build with docker for production.
docker build . -f Dockerfile.prod --build-arg FLASK_ENV="production" --build-arg AWS_ACCESS_KEY_ID="your-key-here" --build-arg AWS_SECRET_ACCESS_KEY="your-key-here" -t production_image_build