FPBP (fastapi-postgresql-boilerplate)
is a boilerplate that can serve as a base for FastAPI with PostgreSQL.
๐ FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.6+ based on standard Python-type hints. I made and used this boilerplate for projects that Iโm working on. ๐งโ๐ป it was powerful for building data-driven applications using schema for data validation, serialization, and documentation. ๐
- ๐ Python 3.12 - A programming language that lets you work quickly and integrate systems more effectively.
- ๐ FastAPI - A modern, fast (high-performance), web framework for building APIs with Python 3.8+ based on standard Python type hints.
- ๐ณ Docker - A platform designed to help developers build, share, and run container applications.
- ๐ PostgreSQL - An open-source object-relational database system.
- ๐พ SQLAlchemy - the Python SQL toolkit and Object Relational Mapper.
- ๐ฆ Poetry - A tool for dependency management and packaging in Python.
- ๐งช Pytest - A framework that makes writing small, readable tests easy.
- ๐ฆ Ruff - A fast Python linter and code formatter written in Rust.
- ๐ซ JWT authentication, to ensure secure access to the API.
- ๐ข A Layered architecture (Controller, Service, Repository, Model).
- ๐ฆ Dependency injection for better code organization.
- ๐ Swagger and Redoc for API documentation and testing.
- โป๏ธ Schema validation, to ensure that input and output data conform to the defined schema.
- Python 3.12
- Poetry
- PostgreSQL
- Docker
Every command below except build
and run
is executed in a docker container.
# Copy service env file
$ cp .env.example .env
# Copy db env file
$ cp .db.env.example .db.env
# Build docker image
$ docker compose build
# Run the app in the background
$ docker compose up -d
# Watch logs
$ docker compose logs -f
# Execute a command in a running container
$ docker compose exec app <command>
before test or use the app, you need to migrate the database.
# init User table
$ docker compose exec app poetry run alembic upgrade head
# Run unit tests using pytest
$ docker compose exec app poetry run pytest
The Ruff is an extremely fast Python linter and code formatter written in Rust. It can replace flake8, isort, and black at once.
# Run Ruff
## Code linting
$ docker compose exec app poetry run ruff check .
## Code formatting
$ docker compose exec app poetry run ruff format .
This project is licensed under the MIT License - see the LICENSE file for details.