Skip to content

yeezy-na-izi/FastAPI-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

14 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

FastAPI Template

Useful and multifunction FastApi template

FastApi CodeFactor CodeStyle PythonVersions

Features

  • aiogram 3 as a main library
  • pyrogram as a database library
  • poetry as a dependency manager
  • 🎨 OpenAPI docs on api/docs and api/redoc
  • πŸ›  Docker support with docker-compose

Usage

Create project

  • πŸ“Œ Create and clone repo from this template
  • πŸ”‘ Rename example.toml to config.toml and change api settings

Install dependencies

  • 🐍 Install poetry with command pip install poetry
  • πŸ“Ž Install dependencies with command poetry install

Run project

  • πŸš€ Run project via python -m app

Docker

You can start the project with docker using this command:

docker-compose -f deploy/docker-compose.yml --project-directory . up --build

But you have to rebuild image every time you modify poetry.lock or pyproject.toml with this command:

docker-compose -f deploy/docker-compose.yml --project-directory . build

Project structure

$ tree "app"
app
β”œβ”€β”€ __init__.py
β”œβ”€β”€ __main__.py  # Startup script. Starts uvicorn.
β”œβ”€β”€ application.py  # FastAPI application configuration.
β”œβ”€β”€ settings.py  # Main configuration settings for project.
β”œβ”€β”€ api  # Package with all handlers.
β”‚Β Β  β”œβ”€β”€ __init__.py
β”‚Β Β  └── router.py  # Main router.
β”œβ”€β”€ db  # module contains db models
β”‚Β Β  └── __init__.py
β”œβ”€β”€ events
β”‚Β Β  β”œβ”€β”€ __init__.py
β”‚Β Β  β”œβ”€β”€ shutdown.py  # Shutdown event.
β”‚Β Β  └── startup.py  # Startup event.
β”œβ”€β”€ middlewares  # Package contains middlewares.
β”‚Β Β  β”œβ”€β”€ __init__.py
β”‚Β Β  └── main.py  # Main middleware.
└── static  # Static content.
    └── docs
        β”œβ”€β”€ redoc.standalone.js
        β”œβ”€β”€ swagger-ui-bundle.js
        └── swagger-ui.css

Pre-commit checker

To install pre-commit simply run inside the shell:

pre-commit install

pre-commit is very useful to check your code before publishing it. It's configured using .pre-commit-config.yaml file.

By default, it runs:

  • black (formats your code);
  • flake8 (spots possible bugs);

You can read more about pre-commit here: https://pre-commit.com/