Git Product home page Git Product logo

fastapi_production_template's Introduction

FastAPI Example Project

Some people were searching my GitHub profile for project examples after reading the article on FastAPI best practices. Unfortunately, I didn't have useful public repositories, but only my old proof-of-concept projects.

Hence, I have decided to fix that and show how I start projects nowadays, after getting some real-world experience. This repo is kind of a template I use when starting up new FastAPI projects:

  • production-ready
    • gunicorn with dynamic workers configuration (stolen from @tiangolo)
    • Dockerfile optimized for small size and fast builds with a non-root user
    • JSON logs
    • sentry for deployed envs
  • easy local development
    • environment with configured postgres and redis
    • script to lint code with black, autoflake, isort (also stolen from @tiangolo)
    • configured pytest with async-asgi-testclient, pytest-env, pytest-asyncio
    • fully typed to comply with mypy
  • SQLAlchemy with slightly configured alembic
    • async db calls with asyncpg
    • set up sqlalchemy2-stubs
    • migrations set in easy to sort format (YYYY-MM-DD_slug)
  • pre-installed JWT authorization
    • short-lived access token
    • long-lived refresh token which is stored in http-only cookies
    • salted password storage with bcrypt
  • global pydantic model with
    • orjson
    • explicit timezone setting during JSON export
  • and some other extras like global exceptions, sqlalchemy keys naming convention, shortcut scripts for alembic, etc.

Local Development

First Build Only

  1. cp .env.example .env
  2. docker network create app_main
  3. docker-compose up -d --build

Linters

Format the code

docker compose exec app format

Migrations

  • Create an automatic migration from changes in src/database.py
docker compose exec app makemigrations *migration_name*
  • Run migrations
docker compose exec app migrate
  • Downgrade migrations
docker compose exec app downgrade -1  # or -2 or base or hash of the migration

Tests

All tests are integrational and require DB connection.

One of the choices I've made is to use default database (postgres), separated from app's app database.

  • Using default database makes it easier to run tests in CI/CD environments, since there is no need to setup additional databases
  • Tests are run with force_rollbacke=True, i.e. every transaction made is then reverted

Run tests

docker compose exec app pytest

fastapi_production_template's People

Contributors

zhanymkanov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.