Skip to content

Latest commit

 

History

History

backend

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Backend Application 🐍

This backend application template consists of:

  • Configure the TOML file for testing and linting.
  • Create the backend server with FastAPI and Uvicorn,
  • Connect the backend app with PostgreSQL server via the asynchronous SQLAlchemy with AsyncPG driver.
  • Set up Alembic for the auto-generation of database migrations.
  • Create Account (domain) class as our database entity account.
  • Create data validation for our database entity with Pydantic.
  • Create formatters for json convention and datetime.
  • Create authorization with JWT Token generator.
  • Create hashing function generator and password generator.
  • Create custom error/exception handlers for both source code and HTTP.
  • Create dependency injections for utilizing our asynchronous session and C. R. U. D. repository class.
  • Create account specific routes: signup, signin, get all, get by id, update by id, and delete by id.

P. S. This README will walk you through the local development setup. If you prefer to run your backend app with docker, see the main README.


Python, VEnv, & Requirements Installation

INFO: All related to Python will be setup IN and FROM the backend/ directory!

  • Step 1 $\rightarrow$ Open your project root directory and set up your Python via PyEnv:

    pyenv install 3.11.0
  • Step 2 $\rightarrow$ Create our virtual environment:

    pyenv virtualenv 3.11.0 YOUR_VENV_NAME
  • Step 3 $\rightarrow$ Set the newly created virtual environment as your main Python interpreter in the root directory:

    pyenv local YOUR_VENV_NAME
  • Step 4 $\rightarrow$ Install the initial project requirements with pip3:

    pip3 install -r requirements.txt

SSHhhhttt 🤫 It's a Secret!

All secret variables are configured in the .env, but in this case since it is listed in our .gitignore you should create one and save it in the root directory.

The secret variables are accessed by 2 different files:

  1. backend7src/config/settings/base.py $\rightarrow$ The BackendBaseSettings class contains all the secret variables. This allows us to set the values dynamically through out the backend application ("dynamic" because we only need to change the value in .env file without breaking any code).
  2. docker-compose.yaml $\rightarrow$ When we initialize the backend_app container, we register all the secret variables as well for our containerized backend application.

Database

INFO: No postgres server, no connection!

  • Step 1 $\rightarrow$ Run your postgres server (if you use MacOS, I suggest the installation via postgre14 homebrew):

    brew services start postgresql@14
  • Step 2 $\rightarrow$ Go to your database editor choice and create a database for this application.

  • Step 3 $\rightarrow$ Register your database credentials respectively in our .env file in the root directory.

  • Step 4 (Optional) $\rightarrow$ To stop your postgres server, run:

    brew services stop postgres@14

Alembic for Automigration

INFO: Files generated by Alembic can be found in backend/src/repository/migrations/** (if you don't see the versions/ directory, make one!)

INFO: You need to import your domain classes in backend/src/repository/base.py for alembic's auto migration!

  • Step 1 $\rightarrow$ Create the initialization of your database migration:

    alembic revision --autogenerate -m "YOUR NOTES ABOUT THE DATABASE MIGRATION HERE"
  • Step 2 $\rightarrow$ Push the registered database objects to your database:

    alembic upgrade head

Pre-Commit

INFO: Run Step 1 every time you git add a file to identify any mistakes before git commit. Otherwise, you will re-write your perfect commit message again 👿🤬🤮

  • Step 1 $\rightarrow$ Install the pre-commit hook:

    pre-commit
  • Step 2 $\rightarrow$ For good practive, let#s update the hooks:

    pre-commit autoupdate

TOML Configuration

Check the pyproject.toml as the main configuration file for the following packages:

  • Project documentation
  • Black
  • Isort
  • MyPy
  • PyTest
  • Converage

FastAPI Backend Server

INFO: Make sure that you are in the backend/ directory!

  • Stp 1 $\rightarrow$ Run the server:

    uvicorn src.main:backend_app --reload
  • Step 2 (Optional) $\rightarrow$ To stop the server click simultaneously control and C


Test with PyTest

INFO: For running the test, make sure you are in the root directory and NOT in the backend/ directory!

INFO: The testing report is automatically generated and saved in backend/coverage/**

  • Step 1: Run PyTest:

    pytest

Python Package Info Board

So what are we actually installing? You can find the main packages in the below table.

Server Database Test Linter Others
fastapi  sqlalchemy (version 2.0.0b3) pytest  black python-decouple
uvicorn  alembic pytest-asyncio  isort email-validator
pydantic  asyncpg pytest-cov  mypy python-dotenv
starlette  greenlet  pytest-xdist pre-commit passlib
asgi_lifespan - colorama loguru
httpx - - - python-jose
trio - - - pathlib
- - - - python-slugify