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:
- π³ Dockerized
- π Asynchronous PostgreSQL
- π FastAPI
When the Docker is started, these are the URL addresses:
- Backend Application (API docs)
$\rightarrow$ http://localhost:8001/docs - Database editor (Adminer)
$\rightarrow$ http//localhost:8081
The backend API without Docker can be found in http://localhost:8000/docs.
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). Read this blog from Packt if you want to educate yourself further about the topic Asynchronous, Synchronous, Concurrency, and Parallelism.
- Docker is a technology that packages an application into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime.
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:
- Pre-Commit CI
$\rightarrow$ Continuous integration for our Pre-Commit hook that fixes and updates our hook versions. - CodeCov
$\rightarrow$ A platform that analyzes 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 platform provides a complete symbol for entity relationships (not like many other platforms!). - SQLAlchemy 2.0
$\rightarrow$ The go-to database interface library for Python. The 2.0 is the most recent update where it provides an asynchronous setup.
For the backend application:
- 3 settings classes (development, staging, production) with the super class in
backend/src/config/settings/base.py. - Event logger in
backend/src/config/events.py. - The
Accountobject table model inbackend/src/models/db/account.py. - The
Accountobject schema model inbackend/src/models/schemas/account.py. - PostgreSQL database connection with asynchronous SQLAlchemy 2.0 in
backend/src/repository/database.py. - A custom SQLAlchemy Base class in
backend/src/repository/table.py - PostgreSQL database connection with asynchronous SQLAlchemy 2.0 in
backend/src/repository/database.py. - Database-related events e.g. database table registration by app startup in
backend/src/repository/events.py. - C. R. U. D. methods for
Accountobject inbackend/src/repository/crud/account.py. - Table classes registration file in
backend/src/repository/base.py. - Alembic setup for auto-generating asynchronous database migrations in
backend/src/repository/migration/**. - Alembic main configuration file in
backend/alembic.ini. - Dependency injection for database session and repository in
backend/src/api/**. - API endpoints for
Accountsignup and signin inbackend/src/api/routes/authentication.py. - API endpoints for
Accountget all, get 1, update, and delete inbackend/src/api/routes/account.py. - API endpoints registration file in
backend/src/api/endpoints. - Hashing and JWT generators and simple verification functions in
backend/src/securities/**. - Helper functions, string messages, and error handling in
backend/src/utilities/**. - A comprehensive FastAPI application initialization in
backend/src/main.py.
For testing, I have prepared the following simple code to kick-start your test-driven development:
- A simple replication of the backend application for testing purposes and the asynchronous test client in
backend/tests/conftest.py. - 2 simple test functions to test the backend application initialization in
tests/unit_tests/test_src.py.
For the DevOps:
- A simple
buildjob to test the compilation of the source code for the backend application in.github/workflows/ci-backend.yaml. - A simple linting job called
code-stylewith black, isort, flake8, and mypy in.github/workflows/ci-backend.yaml. - An automated testing with
PyTestand an automated test reporting withCodecovin in.github/workflows/ci-backend.yaml. - A source code responsibility distribution file in
.github/CODEOWNERS(Please change the username to your own). - A
YAMLfile for an automated semantic commit message in.github/workflows/ci-backend.yaml. - An automated test report monitoring via
Codecovincodecov.yaml - A CI for automatically updating all linter version in the pre-commit
YAMLfile in.pre-commit-config.YAML.
For containerization:
- A
Dockerconfiguration that utilizes the latest Python image inbackend/Dockerfile. - A script that ensure the backend application will restart when postgres image hasn't started yet in
backend/entrypoint.sh. - Setting up
Postgresimage for our database server,Adminerfor our database editor, andbackend_appfor our backend application's container indocker-compose.yaml.
For the team development environment:
- A pre-commit hooks for
Black,Isort, andMyPyto ensure the conventional commit message before pushing an updated code into the remote repository in.pre-commit-config.YAML. - All secret variables are listed in
.env.example. You need to copy these variables and set the values respectively to your need and save them in a new.envin the root directory.
This backend application is setup with Docker. Nevertheless, you can see the full local setup without Docker in backend/README.md.
-
Before setting up the backend app, please create a new directory called
coveragefor the testing report purpose:cd backend && mkdir coverage
-
Backend app setup:
# Install dependencies sh init.sh # Test run your backend server uvicorn src.main:backend_app --reload
-
Testing with
PyTest: Make sure that you are in thebackend/directory.# For testing without Docker pytest # For testing within Docker docker exec backend_app pytest
-
Docker setup:
# Make sure you are in the ROOT project directory chmod +x backend/entrypoint.sh docker-compose build docker-compose up # Every time you write a new code, update your container with: docker-compose up -d --build
-
(IMPORTANT) Database setup:
# (Docker) 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 # (Local) Generate revision for the database auto-migrations alembic revision --autogenerate -m "YOUR MIGRATION TITLE" alembic upgrade head # to register the database classes
.github/ βββ workflows/ βββ ci-backend.yaml # A CI file for the backend app that consists of `build`, `code-style`, and `test` βββ CODEOWNERS # A configuration file to distribute code responsibility βββ semantic.yaml # A configuration file for ensuring an automated semantic commit message backend/ βββ coverage/ βββ src/ βββ api/ βββ dependencies/ # Dependency injections βββ session.py βββrepository.py βββ routes/ # Endpoints βββ account.py # Account routes βββ authentication.py # Signup and Signin routes βββ endpoints.py # Endpoint registration βββ config/ βββ settings/ βββ base.py # Base settings / settings parent class βββ development.py # Development settings βββ environments.py # Enum with PROD, DEV, STAGE environment βββ production.py # Production settings βββ staging.py # Test settings βββ events.py # Registration of global events βββ manager.py # Manage get settings βββ models/ βββ db/ βββ account.py # Account class for database entity βββ schemas/ βββ account.py # Account classes for data validation objects βββ base.py # Base class for data validation objects βββ repository/ βββ crud/ βββ account.py # C. R. U. D. operations for Account entity βββ base.py # Base class for C. R. U. D. operations βββ migrations/ βββ versions/ βββ env.py # Generated via alembic for automigration βββ script.py.mako # Generated via alembic βββ base.py # Entry point for alembic automigration βββ database.py # Database class with engine and session βββ events.py # Registration of database events βββ table.py # Custom SQLAlchemy Base class βββ security/ βββ hashing/ βββ hash.py # Hash functions with passlib βββ password.py # Password generator with hash functions βββ authorizations/ βββ jwt.py # Generate JWT tokens with python-jose βββ verifications/ βββ credentials.py # Check for attributes' availability βββ utilities/ βββ exceptions/ βββ http/ βββ http_exc_400.py # Custom 400 error handling functions βββ http_exc_401.py # Custom 401 error handling functions βββ http_exc_403.py # Custom 403 error handling functions βββ http_exc_404.py # Custom 404 error handling functions βββ database.py # Custom `Exception` class βββ password.py # Custom `Exception` class βββ formatters/ βββ datetime_formatter.py # Reformat datetime into the ISO form βββ field_formatter.py # Reformat snake_case to camelCase βββ messages/ βββ http/ βββ http_exc_details.py# Custom message for HTTP exceptions βββ main.py # Our main backend server app βββ tests/ βββ end_to_end_tests/ # End-to-end tests βββ integration_tests/ # Integration tests βββ security_tests/ # Security-related tests βββ unit_tests/ # Unit tests βββ test_src.py # Testing the src directory's version βββ conftest.py # The fixture codes and other base test codes βββ Dockerfile # Docker configuration file for backend application βββ README.md # Documentation for backend app βββ entrypoint.sh # A script to restart backend app container if postgres is not started βββ alembic.ini # Automatic database migration configuration βββ pyproject.toml # Linter and test main configuration file βββ requirements.txt # Packages installed for backend app .dockerignore # A file that list files to be excluded in Docker container .gitignore # A file that list files to be excluded in GitHub repository .pre-commit-config.yaml # A file with Python linter hooks to ensure conventional commit when committing LICENSE.md # A license to use this template repository (delete this file after using this repository) README.md # The main documentation file for this template repository codecov.yaml # The configuration file for automated testing CI with codecov.io docker-compose.yaml # The main configuration file for setting up a multi-container DockerYou can delete these 3 files (or change its content based on your need):
LICENSE.mdREADME.mdbackend/README.md
Enjoy your development and may your technology be forever useful to everyone πππ§¬