Skip to content

vvandriichuk/fastapi_db_template_02_2024

Repository files navigation

FastAPI + SQL Template from 02.2024

Build Status Build Status Build Status

This my current vision of the good FastAPI structure.

Short description of this structure and changes:

v.2.6.0 (from 16.04.2024) - Memory Leak Detector:

  • Added Memory Leak Detector

v.2.5.1 (from 15.03.2024) - AI Assistent for Code Review:

  • Added AI Assistent for Code Review
  • Separation check action to two - check code style and check pytest

v.2.5.0 (from 13.03.2024) - Metrics Collector for Open Telemetry:

  • Added Metrics Collector
  • Some small changes in the code

v.2.4.0 (from 13.03.2024) - Slack Integration to Logger:

  • Added Slack Integrations for notifications
  • Some small changes in the code

v.2.3.2 (from 09.03.2024) - Tracer Sample Rate Control for Open Telemetry:

  • Added Tracer Sample Rate Control if we want to reduce the load on the server in a production environment

v.2.3.1 (from 09.03.2024) - Integrating Loguru and Open Telemetry:

  • Added loguru as an option for logging and selector between loguru and standard logging

v.2.3.0 (from 06.03.2024) - Logging and Tracer Manager and Open Telemetry:

  • Added Open Telemetry to project
  • Added logger' and tracer' managers

v.2.2.0 (from 29.02.2024) - Locust Tests:

  • Added locust tests

v.2.1.0 (from 29.02.2024) - Additional checker and GitHub actions:

  • Added mypy, ruff checkers
  • Added actions for GitHub

v.2.0.0 (from 21.02.2024) - Unit of Work:

  • Added transaction manager to handle the database transactions

v.1.0.0 (from 15.02.2024) - Onion Architecture:

  • Take beyond the logic from the API endpoints and put it in the middle layer
  • Added SQLAlchemy models and linked them with Pydantic schemas through to_read_model
  • Changed orm_mode for schemas class Config to from_attributes (for Pydantic v2)
  • Added Service layer to handle the logic of the API endpoints
  • Added Repository layer to handle the logic of the database (based on dependency inversion principle from SOLID principles)

TODO:

  • Add tests
  • Add abstract class for choosing DB
  • Add PostgreSQL database
  • Add logger
  1. For autogenerated migration files for updating DB, run (NB! you need to add paths to models to the migrations/env file if you want to autogenerate migration successfully (as this moment I don't know why)):
alembic revision --autogenerate -m "description_of_changes"

  1. For running the migration, run:
alembic upgrade head
  1. For running check of the code with Ruff, run:
ruff check app
  1. For running the formatting of the code with the Ruff, run:
ruff format app
  1. For checking the code with Mypy, run:
mypy app
  1. For running pytest, use commands from directory:
pytest app/tests -v
  1. Loading testing, use:
locust -f app/tests/locust/users.py --host http://localhost:8001

  1. For running this project locally from root dir, run (you can use any port that you want):
uvicorn app.main:app --reload --host 0.0.0.0 --port 8001
  1. Before running the project in docker create docker network:
docker network create test-fastapi-sql-network
  1. Run the project in docker:
docker-compose up --build
  1. If you want to work with collected data locally, use TRACE_INSECURE=True and TRACE_USE_CREDENTIALS=False. If you want to use external Aspecto service, use TRACE_INSECURE=False and TRACE_USE_CREDENTIALS=True

  2. In Elasticseach please go to Dev Tools for finding count of metrics. For example

GET /_cat/indices?v


GET /.ds-metrics-*/_mapping


GET /.ds-metrics-*/_search
{
  "query": {
    "exists": {
      "field": "labels.Counter"
    }
  }
}
  1. Bandit is a tool designed to find common security issues in Python code.
bandit -r app

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages