Skip to content

danialahmed2207/python-microservice-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐍 Python Microservice Template

FastAPI Docker Python Tests

πŸ“Έ API Preview

// GET /
{
  "name": "Python Microservice Template",
  "version": "1.0.0",
  "author": "Danial Ahmed",
  "docs": "/docs",
  "health": "/health"
}

// GET /health
{
  "status": "healthy",
  "timestamp": "2026-05-09T10:30:00",
  "service": "python-microservice-template",
  "version": "1.0.0"
}

πŸ—οΈ Architektur

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  FastAPI Microservice                    β”‚
β”‚                                                          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚   Health    β”‚    β”‚    Items    β”‚    β”‚    Users    β”‚ β”‚
β”‚  β”‚   Router    β”‚    β”‚   Router    β”‚    β”‚   Router    β”‚ β”‚
β”‚  β”‚  (/health)  β”‚    β”‚  (/items)   β”‚    β”‚  (/users)   β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚         β”‚                  β”‚                  β”‚         β”‚
β”‚         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β”‚
β”‚                            β–Ό                            β”‚
β”‚                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                      β”‚
β”‚                    β”‚   Pydantic  β”‚                      β”‚
β”‚                    β”‚   Models    β”‚                      β”‚
β”‚                    β”‚(Validation) β”‚                      β”‚
β”‚                    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜                      β”‚
β”‚                           β–Ό                             β”‚
β”‚                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                      β”‚
β”‚                    β”‚  Database   β”‚                      β”‚
β”‚                    β”‚ (In-Memory) β”‚                      β”‚
β”‚                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

✨ Features

  • βœ… FastAPI – Hochperformante, moderne Python API
  • βœ… Pydantic – Automatische Datenvalidierung
  • βœ… CRUD Operations – VollstΓ€ndige REST API fΓΌr Items & Users
  • βœ… Health Checks – Kubernetes-ready Probes (/health, /ready, /live)
  • βœ… Docker – Multi-stage Dockerfile mit Security-Best-Practices
  • βœ… Tests – 100% Testabdeckung mit pytest
  • βœ… Code Quality – Black, flake8, mypy konfiguriert
  • βœ… Auto-Docs – Interaktive Swagger UI unter /docs

πŸ› οΈ Tech Stack

Technologie Verwendung
FastAPI Web Framework
Pydantic Datenvalidierung & Serialisierung
Uvicorn ASGI Server
Pytest Testing Framework
Docker Containerisierung
Black Code Formatting
MyPy Static Type Checking

πŸš€ Quick Start

Mit Docker (empfohlen)

# Repository klonen
git clone https://github.com/danialahmed2207/python-microservice-template.git
cd python-microservice-template

# Container bauen & starten
docker compose up -d

# API testen
curl http://localhost:8000/
curl http://localhost:8000/health

Lokal (Development)

# Virtual Environment erstellen
python3 -m venv venv
source venv/bin/activate

# Dependencies installieren
pip install -r requirements.txt

# Server starten
uvicorn app.main:app --reload

# Tests ausfΓΌhren
pytest

πŸ“‘ API Endpoints

Health

Methode Endpoint Beschreibung
GET /health Health Check
GET /health/ready Readiness Probe
GET /health/live Liveness Probe

Items

Methode Endpoint Beschreibung
GET /items/ Alle Items listen
GET /items/{id} Einzelnes Item
POST /items/ Item erstellen
PUT /items/{id} Item aktualisieren
DELETE /items/{id} Item lΓΆschen

Users

Methode Endpoint Beschreibung
GET /users/ Alle User listen
GET /users/{id} Einzelnen User
POST /users/ User erstellen
PUT /users/{id} User aktualisieren
DELETE /users/{id} User lΓΆschen

Dokumentation


πŸ§ͺ Tests

# Alle Tests ausfΓΌhren
pytest

# Mit Coverage
pytest --cov=app --cov-report=term-missing

# Nur Health Tests
pytest tests/test_health.py

# Nur Item Tests
pytest tests/test_items.py -v

πŸ”’ Security Features

  • βœ… Non-root User im Docker Container
  • βœ… Multi-stage Build (kleines Image)
  • βœ… Health Checks in Dockerfile
  • βœ… Input Validation mit Pydantic
  • βœ… HTTP Exception Handling

πŸ“ Projektstruktur

.
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ main.py              # FastAPI App Entry Point
β”‚   β”œβ”€β”€ database.py          # In-Memory DB
β”‚   β”œβ”€β”€ models.py            # Pydantic Models
β”‚   └── routers/
β”‚       β”œβ”€β”€ __init__.py
β”‚       β”œβ”€β”€ health.py        # Health Endpoints
β”‚       β”œβ”€β”€ items.py         # Item CRUD
β”‚       └── users.py         # User CRUD
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ test_health.py
β”‚   β”œβ”€β”€ test_items.py
β”‚   └── test_users.py
β”œβ”€β”€ Dockerfile               # Multi-stage Docker
β”œβ”€β”€ docker-compose.yml       # Docker Compose
β”œβ”€β”€ requirements.txt         # Python Dependencies
β”œβ”€β”€ pyproject.toml           # Project Config
└── README.md

🎯 Was ich gelernt habe

  • FastAPI Routing und Dependency Injection
  • Pydantic Modelle und Datenvalidierung
  • Async/Await in Python APIs
  • Docker Multi-stage Builds
  • Kubernetes Health Probes
  • pytest Best Practices
  • API Design Patterns (REST)

Made with 🐍 by Danial Ahmed | Backend Developer

About

Production-ready FastAPI microservice with Docker and tests

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors