Skip to content

A production-ready Python backend boilerplate with FastAPI, async database support, caching, middleware, and structured API responses. Designed for scalable, maintainable, and modular backend services to jumpstart new projects effortlessly.

License

Notifications You must be signed in to change notification settings

kitty-ilnazik/backend-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

21 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

# 🌸 Backend Template

An elegant, modular, and production-ready boilerplate for building Python backend services with FastAPI.

anime-banner

Stars Forks Issues License


🐾 Overview (English)

This Backend Template provides a solid foundation for building scalable, maintainable, and production-ready Python backend services. It is designed to help developers start new projects quickly without worrying about boilerplate setup. The template emphasizes modularity, clear project structure, and best practices, including async database operations, caching, middleware support, and structured API responses. With this template, you can focus on building your application logic while the foundation handles configuration, logging, database initialization, and API versioning. It is suitable for building RESTful APIs, microservices, or any backend service that requires reliability, scalability, and maintainability.


🌍 Translations

🌐 Language 🏷 Code πŸ”— Link
Russian ru README.ru.md
Ukrainian uk README.uk.md
Tatar tt README.tt.md
Uzbek uz README.uz.md
Kazakh kk README.kk.md
English en README.md

πŸ› οΈ Tools and Libraries

  • ⚑ fastapi β€” modern, fast (high-performance) web framework for building APIs with Python.
  • πŸ”₯ uvicorn β€” lightning-fast ASGI server for running FastAPI apps.
  • 🧩 pydantic-settings β€” settings management using Pydantic.
  • πŸ’Ύ sqlalchemy + aiosqlite β€” async ORM and DB driver.
  • πŸ” redis β€” a client for Redis, used for caching.
  • 🧱 alembic β€” a database migration tool.
  • ✨ ruff β€” an extremely fast Python linter and formatter.
  • πŸš€ uv β€” an extremely fast package manager and bundler for Python.

πŸ“ Project Structure

// Directory tree (3 levels, limited to 200 entries)
β”œβ”€β”€ .gitignore
β”œβ”€β”€ .python-version
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ LICENSE
β”œβ”€β”€ README.md
β”œβ”€β”€ example.alembic.ini
β”œβ”€β”€ pyproject.toml
β”œβ”€β”€ ruff.toml
β”œβ”€β”€ tests\
β”œβ”€β”€ src\
β”‚   β”œβ”€β”€ .env.example
β”‚   β”œβ”€β”€ config.py
β”‚   β”œβ”€β”€ app.py
β”‚   β”œβ”€β”€ run.py
β”‚   β”œβ”€β”€ api\
β”‚   β”‚   β”œβ”€β”€ v1\
β”‚   β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”‚   β”œβ”€β”€ admin.py
β”‚   β”‚   β”‚   └── user.py
β”‚   β”‚   β”œβ”€β”€ __init.py__
β”‚   β”‚   └── common.py
β”‚   β”œβ”€β”€ database\
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ base.py
β”‚   β”‚   β”œβ”€β”€ models\
β”‚   β”‚   └── repositories\
β”‚   β”œβ”€β”€ middlewares\
β”‚   β”‚   └── rate_limit.py
β”‚   β”œβ”€β”€ schemas\
β”‚   β”‚   └── __init__.py
β”‚   β”œβ”€β”€ services\
β”‚   β”‚   └── redis_service.py
β”‚   └── utils\
β”‚       β”œβ”€β”€ api_structure.py
β”‚       β”œβ”€β”€ command_runner.py
β”‚       β”œβ”€β”€ endpoints.py
β”‚       β”œβ”€β”€ exceptions.py
β”‚       β”œβ”€β”€ logger.py
β”‚       β”œβ”€β”€ migration_database.py
β”‚       └── responses.py
└── uv.lock

πŸ“„ Description of Important Files

πŸ“‚ Folder 🧠 Description
src/api/ Contains API routes and versioned modules.
src/api/v1/ API version 1 endpoints (admin, user, etc.).
src/database/ Database initialization, ORM models, and repositories.
src/database/models/ SQLAlchemy ORM models.
src/database/repositories/ CRUD repositories for working with the database.
src/middlewares/ Middleware for rate limiting and request processing.
src/schemas/ Pydantic schemas for request/response validation.
src/services/ Application services (e.g., Redis).
src/utils/ Utilities: logging, exceptions, responses, etc.
tests/ Unit and integration tests.

πŸš€ Setup and Running

Installing Redis

Redis is used for caching and rate limiting.

For Windows:

  1. Download the .msi installer from the Microsoft's repository: https://github.com/microsoftarchive/redis/releases
  2. Run the .msi file and follow the installer instructions.

For Linux:

  • Arch Linux:
    sudo pacman -S redis
    sudo systemctl start redis
    sudo systemctl enable redis
  • Debian/Ubuntu:
    sudo apt update
    sudo apt install redis-server
    sudo systemctl enable redis-server
    sudo systemctl start redis-server
  • Fedora:
    sudo dnf install redis
    sudo systemctl start redis
    sudo systemctl enable redis

For macOS:

brew install redis
brew services start redis

Running without Docker

Using uv (recommended)

  1. Install uv:

    curl -LsSf https://astral.sh/uv/install.sh | sh

    Or for Windows:

    irm https://astral.sh/uv/install.ps1 | iex
  2. Clone the repository and navigate to the folder:

    git clone https://github.com/Kitty-Ilnazik/backend-template.git
    cd backend-template
  3. Configure environment variables: Copy the example .env file and fill it with your data:

    cp src/.env.example src/.env

    Open src/.env and enter values for REDIS_URL and DB_URL.

  4. Install dependencies and run:

    uv run start

Without uv

  1. Clone the repository and navigate to the folder:

    git clone https://github.com/Kitty-Ilnazik/backend-template.git
    cd backend-template
  2. Configure environment variables: Copy the example .env file and fill it with your data:

    cp src/.env.example src/.env

    Open src/.env and enter values for REDIS_URL and DB_URL.

  3. Create and activate a virtual environment:

    • Windows:
      python -m venv .venv
      .venv/Scripts/activate
    • macOS/Linux:
      python3 -m venv .venv
      . .venv/bin/activate
  4. Install dependencies:

    pip install -r requirements.txt
  5. Run the bot:

    python src/run.py

Running with Docker

  1. Install Docker:

    • Arch Linux:
      sudo pacman -S docker
      sudo systemctl start docker
      sudo systemctl enable docker
      sudo usermod -aG docker $USER # Add user to docker group
      newgrp docker # Apply group changes
    • Debian/Ubuntu:
      sudo apt update
      sudo apt install docker.io
      sudo systemctl start docker
      sudo systemctl enable docker
      sudo usermod -aG docker $USER
      newgrp docker
    • Fedora:
      sudo dnf install docker
      sudo systemctl start docker
      sudo systemctl enable docker
      sudo usermod -aG docker $USER
      newgrp docker
    • Windows/macOS: Install Docker Desktop from the official Docker website.
  2. Build and run the Docker image:

    docker build -t backend-template .
    • docker build: Command to build a Docker image.
    • -t backend-template: Assigns the tag (name) backend-template to the created image.
    • .: Indicates that the Dockerfile is in the current directory.
    docker run -d --name my-backend --env-file src/.env backend-template
    • docker run: Command to run a Docker container.
    • -d: Runs the container in detached mode.
    • --name my-backend: Assigns the name my-backend to the running container.
    • --env-file src/.env: Instructs Docker to use environment variables from src/.env inside the container.
    • backend-template: The name of the Docker image to run.

πŸ—„ Using DB Migrations (Alembic)

  1. Alembic Configuration: Copy the example Alembic configuration file:

    cp example.alembic.ini alembic.ini

    Open alembic.ini and change the path to the .env file in the [alembic] section to src/.env.

  2. Create and apply a migration:

    uv run migrate commit "Initial migration"

🧹 Using Ruff

Ruff is used for code formatting and error checking.

uv run ruff check .
uv run ruff format .

About

A production-ready Python backend boilerplate with FastAPI, async database support, caching, middleware, and structured API responses. Designed for scalable, maintainable, and modular backend services to jumpstart new projects effortlessly.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Contributors 2

  •  
  •