Skip to content

High-performance FastAPI backend boilerplate for real-world production, with mongo and pytest. Suitable for microservices.

License

Notifications You must be signed in to change notification settings

klee1611/cookiecutter-fastapi-mongo

Repository files navigation

Backend FastAPI and MongoDB - Project Template

Python3.10 MongoDB Lint CI Tests PRs Welcome

This is a backend boilerplate designed for high-performance RESTful APIs using FastAPI and Motor, an asynchronous driver for MongoDB. It includes a health API for service health checking, basic CRUD operations for a sample resource, Swagger API documentation, and Docker support. Additionally, this boilerplate uses Cookiecutter, allowing for easy creation of new backend services.

api document screenshot

Features

A new backend project created with this boilerplate provides:

  • Asynchronous high-performance RESTful APIs built upon FastAPI framework.
  • Asynchronous CRUD operations for a sample resource built upon Motor driver for MongoDB, providing high performance and efficiency.
  • API documentation with Swagger UI.
  • API testing with pytest and pytest-asyncio.
  • Dockerfile for containerization and docker-compose support.
  • Easy creation of new backend services with cookiecutter.
  • Easy package menagement with Poetry.
  • Health API for service health checking.
  • Easy configuration with environment variables.
  • Easy testing, develop running, docker build, docker-compose up and down with Makefile.
  • Proper logging with ID masking.

Prerequisites

Getting Started

Create A New Service

With Cookiecutter,
you don't have to fork this project to create a new service.

Go to the directory where you want to place your new backend project,
and create the new project with:

cookiecutter gh:klee1611/cookiecutter-fastapi-mongo

After the project folder is created,
all packages required will be installed automatically.

Edit Environment Variables

Edit the .env file within the project folder.

Run Tests

make test

Build Docker Image

make docker-build

Docker-compose

make docker-compose-up
make docker-compose-down

Run Service Locally

make dev

This will create a MongoDB container as well.

Check Swagger API Document

Go to http://localhost:8888/docs.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you.