Skip to content

ardydedase/cookiecutter-flask-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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

  1. Install cookiecutter.

  2. Run the following command:

    cookiecutter git@github.com:ardydedase/cookiecutter-flask-api.git
    
  3. 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:

Steps:

Skip the steps that you don't need.

  1. Run the local DynamoDB
    java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar
    
  2. Setup your virtual environment. Guide here.
  3. Install the required packages:
    pip install -r requirements.txt
    
  4. Populate the DynamoDB table.
    FLASK_ENV=development FLASK_APP=app/cli.py flask dynamo create-poem-table
    
  5. 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