Python FastAPI Boilerplate with PostgreSQL
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.11 - 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.10+
- 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.