Cookiecutter Flask OpenAPI boilerplate
π» A full-featured Flask + API + OAS3 + JWT + SwaggerUI + ORM + Migrations + Great and Scalable structure project template
π It's using latest Flask 1.2
Flask 2 is coming soon
π
It uses PostgreSQL everywhere, so Docker is necessary (SQLite with no Docker dependency is on the roadmap)
π
β
Why using this boilerplate - Focus on the business and creating value
- Faster project setup
- Standard project structure organization (easy to scale)
- Better QA
π
What's Included (Features) - API Design first using OpenAPI & Connexion
- API documentation using swagger UI
- Login using JWT
- Every layer is separated in context/domain
- Service layer for better tests and reuse
- Using Flask Factory to integrate with extensions
- Migrations using Alembic
- ORM using SQLAlchemy
- Optimized development and production settings
- Comes with user model ready to go, signup & signin
- Procfile for deploying to Heroku
- Customizable PostgreSQL version
- Tests using pytest
- Unit tests for the API layer
- Unit tests for the service layer
Development
- Code linter
- Code formatter (Black+iSort)
- Using .env file
- Docker support using docker-compose for development
- Docker using multistage (Production Ready Dockerfile)
- Postgres in development (using docker-compose)
- CI using Github Actions
Structure
This project is organized in:
- Layers
π§ , which might not change in the project life cycle - Modules
π¦ , for domain contexts, which might scale in terms of new features - Configuration
βοΈ separated based on the extensions - The
π Business modules
Hackernews-Clone
.
βββ hackernews
β βββ app.py π Entrypoint (create_app)
β βββ exceptions.py
β βββ π§
ext π Settings
β β βββ βοΈ configuration.py
β β βββ βοΈ api.py
β β βββ βοΈ database.py
β β ...
β βββ π§
api π API Routes
β β βββ π¦ auth.py
β β βββ π¦ news.py
β β βββ π¦ openapi.yaml π API Contract
β β ...
β βββ π§
services π Business rules
β β βββ π¦ auth.py π
β β βββ π¦ news.py π
β β βββ π¦ token.py π
β β ...
β βββ π§
models π ORM
β βββ π¦ news.py
β βββ π¦ users.py
β ...
βββ βοΈ migrations π Database versions
β βββ alembic.ini
β βββ env.py
β βββ script.py.mako
β βββ versions
βββ tests
β βββ conftest.py
β βββ api π Endpoint tests, input, output and validation
β βββ database π Database connection tests
β βββ services π Business rules tests
βββ requirements.txt
βββ pytest.ini
βββ uwsgi.ini π Application server settings
βββ wsgi.py π WSGI Deploy file (Gunicorn/uWSGI)
Requirements
-
Python 3.8 or 3.9 (Help us test in other versions)
-
Docker to run Postgres locally
π
Contribute Any help is more than welcome...
-
π It could be an Issue -
π» It could be using it and give a feedback -
π It could be a github star -
π€ It could be a Question -
π€ If you dislike this project, feel free to tell us what is wrong with it
Get Started (Usage)
Let's pretend you want to create a Flask project called "hackernewsclone". Rather than start from scratch by a app.py
and add each library, Flask extesion and various other configurations that always get forgotten until the worst possible moment, get this cookiecutter template do all the work.
First, get Cookiecutter. Trust me, it's awesome::
$ pip install "cookiecutter>=1.7.0"
Now run it against this repo::
$ cookiecutter https://github.com/huogerac/cookiecutter-flask-openapi/
You'll be prompted for some values. Provide them...
project_name [Hackernews Clone]:
project_slug [hackernews_clone]: hackernewsclone
main_model [News]:
main_model_lower [news]:
description [The Ultimate Flask Template]:
author_name [Roger Camargo]:
email [[email protected]]:
version [0.1.0]:
Select python_version:
1 - 3.8.10
2 - 3.9.5
Choose from 1, 2 [1]:
Select postgresql_version:
1 - 13.3-alpine
2 - 13.5
3 - 14.1
Choose from 1, 2, 3 [1]:
use_dockerfile [yes]:
use_github_actions_CI [yes]:
deploy_to_heroku [yes]: no
keep_vscode_settings [yes]:
[INFO]: - Removing Procfile (Heroku deploy)
[SUCCESS]: π Huruuuu, All done! β¨ π° β¨
What's next?
1) π³ Running using docker
cd hackernewsclone
docker-compose up --build
2) π Running using virtualenv
cd hackernewsclone
make virtualenv
source .venv/bin/activate
make all
Then
access π http://localhost:5000/api
[INFO]: β οΈ You must have Docker installed, at least to run the postgres database
Articles
Thanks and Inspirations
-
This template is based on cookiecutter-django. Thanks PyDanny and the Cookiecutter maintainers
-
This Flask structure is based on "Arquitetura Definitiva para o Projeto Web Com Python e Flask". Thanks Bruno Rocha and all yours awesome Flask contents