Skip to content

A backend project template with FastAPI, PostgreSQL with asynchronous SQLAlchemy 2.0, Alembic for asynchronous database migration, and Docker.

License

Notifications You must be signed in to change notification settings

elvishabib/FastAPI-Backend-Template

Repository files navigation

FastAPI Backend Application Template

This is a template repository aimed to kick start your project with a setup from a real-world application! This template utilizes the following tech-stack:

When the Docker is started, these are the URL addresses:

  • Backend Application (API docs) $\rightarrow$ http://localhost:8001/docs
  • Database editor (Adminer) $\rightarrow$ http//lcoalhost:8081

Why the above Tech-Stack?

Well, the easy answer is Asynchronousity and Speed!

  • FastAPI is crowned as the fastest web framework for Python and thus we use it for our backend development.
  • The database of my choice is the asynchronous version of PostgreSQL (via SQLAlchemy 2.0). Why asynchronous? Well.. Speed! Read this blog from Packt if you want to educate yourself further about the topic Asynchronous, Synchronous, Concurrency, and Parallelism.
  • And Docker is the technology that will hold your team together because your app will live in a container where the gravity of your personal machine specs don't matter anymore!

Other Technologies

The above listed technologies are just the main ones. There are other technologies utilized in this project template to ensure that your application is robust and provides the best-possible development environment for your team! These technologies are:

  • TOML $\rightarrow$ The one-for-all configuration file. This makes it simpler to setup our project.
  • Pyenv $\rightarrow$ The simplest way to manage our Python versions.
  • Pyenv-VirtualEnv $\rightarrow$ The plugin for Pyenv to manage the virtual environment for our packages.
  • Pre-Commit $\rightarrow$ Git hook scripts to identify issues and quality of your code before pushing it to GitHub. These hooks are implemented for the following linting pakcages:
    • Black (Python) $\rightarrow$ Manage your code style with auto formatting and parallel continuous integration runner for Python.
    • Isort (Python) $\rightarrow$ Sort your import for clarity. Also for Python.
    • MyPy (Python) $\rightarrow$ A static type checker for Python that helps you write a cleaner code.
  • Pre-Commit CI $\rightarrow$ Continuous integration for our Pre-Commit hook that fixes and updates our hook versions.
  • CodeCov $\rightarrow$ A platform that analyze the result of your automated tests.
  • PyTest $\rightarrow$ The testing framework for Python code.
  • DBDiagram $\rightarrow$ A platform that lets your design your database by writing SQL and converting it into ERD. This paltform provides a complete symbol for entity relationships (not like many other platforms!).
  • GitHub Actions $\rightarrow$ The platform to setup our CI/CD by GitHub.
  • SQLAlchemy 2.0 $\rightarrow$ The go-to database interface library for Python. The 2.0 is the most recent update where it provides asynchronous setup.
  • CODEOWNERS $\rightarrow$ A file for distributing the responsibilities in our project to each team/team mate.

The choice for my project development worklow is usually the Trunk-Based Development, hence the name trunk for the main branch repository instead of master or main.

Using this Template

Not a beginner? Great, here is the step to setup this template repository:

  1. Backend setup: The backend server runs on http://localhost:8000

    cd backend mdkir coverage pyenv virtualenv 3.11.0 any_venv_name pyenv local any_venv_name pip3 install -r requirements.txt # Test run your backend server uvicorn src.main:backend_app --reload # Testing pytest # Might throw an error without docker becasue of the imports
  2. Pre-Commit setup:

    # Make sure you are in the ROOT project directory pre-commit install pre-commit update
  3. .env setup: If you are not used to VIM or Linux CLI, then ignore the echo command and do it manually. All the secret variables for this template is located in .env.example.

    If you want to have another names for the secret variables, don't forget to change them also in:

    • backend/src/config/base.py
    • docker-compose.yaml
    # Make sure you are in the ROOT project directory touch .env echo "SECRET_VARIABLE=SECRET_VARIABLE_VALUE" >> .env
  4. CODEOWNERS setup: Go to .github/ and open CODEOWNERS file. This file is to assign the code to specific team member so you can distribute the weights of the project clearly.

  5. Docker setup:

     # Make sure you are in the ROOT project directory chmod +x backend/entrypoint.sh docker-compose build docker-compose up # Everytime you write a new code, update your container with: docker-compose up -d --build
  6. (IMPORTANT) Finishing setup:

     # Generate revision for the database auto-migrations docker exec backend_app alembic revision --autogenerate -m "YOUR MIGRATION TITLE" docker exec backend_app alembic upgrade head # to register the database classes # For testing within Docker container docker exec backend_app pytest # backend
  7. Go to https://about.codecov.io/, and sign up with your github to get the CODECOV_TOKEN

  8. Go to your GitHub and register all the secret variables (look in .env.example) in your repository (settings $\rightarrow$ (scroll down a bit) Secrets $\rightarrow$ Actions $\rightarrow$ New repository secret)

IMPORTANT: Without the secrets registered in Codecov and GitHub, your CI will fail and life will be horrible 🤮🤬 IMPORTANT: Remember to always run the container update every once in a while. Without the arguments -d --build, your Docker dashboard will be full of junk containers!

What's Next?

Read morea bout how the backend/ application is set up here

Final Step

You can delete these 3 files (or change its content based on your need):

  • LICENSE.md
  • README.md
  • backend/README.md

Enjoy your development and may your technology be forever useful to everyone 😉🚀🧬


About

A backend project template with FastAPI, PostgreSQL with asynchronous SQLAlchemy 2.0, Alembic for asynchronous database migration, and Docker.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 96.5%
  • Dockerfile 2.0%
  • Other 1.5%