March 7, 2019

Dockerized Flask APIs for busy people

Flask is an excellent choice for micro-services and API. Flask API Starter kit makes it very easy to setup a full featured project in minutes.

Dockerized Flask APIs for busy people

I work with many different tools but when I need to do something fast and well I usually resort to Python. I think I did my first app based on Zope back in 1995, when it first came out, and since then I've built many production systems with large user base on a combination of Django, WebPy (one of my favourite) and Flask.

I'm working on a ticket-tracking API that abstracts away the specifics of different ticket systems, and for this project I picked Flask since it fits well what i need:

  • a simple, well tested and documented api
  • a nice development environment
  • easy move to production

Flask fits the bill well since is quite minimal, you can pair it with any db and ORM mapping, and works fine also on a smaller cloud instance.

Time to code. Experience thought me that this is the crucial time for setting up a good project structure to build on.  Flask doesn't have any cli tool to get you started, you can code a full app in a single file if you want. But I'm going to share this project with other people, not all experts, and I like to have some advice from flask-experts on how to setup my project. This is what I usually do: look for a starter project.

I didn't even start searching for one, that I found out this little gem as search result: Flask API Starter Kit by Github user @antkahn.

This starter will make your life so easy!

  • Docker-compose handles all the dependencies an deployment, postgres db included
  • A Makefile makes super easy to start the app in dev mode or production, run tests and coverage reports, setup your db, install dependencies

If you do your TDD, you will spend most of the time running make test in a Visual Studio Code terminal.

The file structure itself it's pretty intuitive, heavily based on Blueprints, and adding new resources is very fast. The templates uses FlaskRestfulAPI extension to provide an easy routing. The flasgger module provides Swagger API documentation, served under /apidocs, all generated from .yml files in the project.

Check out the project on Github.

In the meanwhile I'll keep working on the issue tracker manager, stay tuned.