Skip to content

A boilerplate for building fast and reliable web applications with FastAPI and PostgreSQL.

License

Notifications You must be signed in to change notification settings

beerjoa/fastapi-postgresql-boilerplate

Repository files navigation

Nest Logo

FPBP

hits GitHub GitHub issues GitHub last commit GitHub top language

Python FastAPI Docker PostgreSQL SQLAlchemy Poetry Pytest Ruff

Overview

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. 📝

Technology Stacks

  • 🐍 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.

Development Features

  • 🚫 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.

Requirements

  • Python 3.12
  • Poetry
  • PostgreSQL
  • Docker

Getting Started

Every command below except build and run is executed in a docker container.

Set Environment Variables

# Copy service env file
$ cp .env.example .env
# Copy db env file
$ cp .db.env.example .db.env

Build and run the app with Docker Compose

# 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>

Migrate database

before test or use the app, you need to migrate the database.

# init User table
$ docker compose exec app poetry run alembic upgrade head

Test

# Run unit tests using pytest
$ docker compose exec app poetry run pytest

Lint and format

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 .

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A boilerplate for building fast and reliable web applications with FastAPI and PostgreSQL.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published