π Python Microservice Template
// 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"
}
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FastAPI Microservice β
β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Health β β Items β β Users β β
β β Router β β Router β β Router β β
β β (/health) β β (/items) β β (/users) β β
β ββββββββ¬βββββββ ββββββββ¬βββββββ ββββββββ¬βββββββ β
β β β β β
β ββββββββββββββββββββΌβββββββββββββββββββ β
β βΌ β
β βββββββββββββββ β
β β Pydantic β β
β β Models β β
β β(Validation) β β
β ββββββββ¬βββββββ β
β βΌ β
β βββββββββββββββ β
β β Database β β
β β (In-Memory) β β
β βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
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
Technologie
Verwendung
FastAPI
Web Framework
Pydantic
Datenvalidierung & Serialisierung
Uvicorn
ASGI Server
Pytest
Testing Framework
Docker
Containerisierung
Black
Code Formatting
MyPy
Static Type Checking
# 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
# 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
Methode
Endpoint
Beschreibung
GET
/health
Health Check
GET
/health/ready
Readiness Probe
GET
/health/live
Liveness Probe
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
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
# 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
β
Non-root User im Docker Container
β
Multi-stage Build (kleines Image)
β
Health Checks in Dockerfile
β
Input Validation mit Pydantic
β
HTTP Exception Handling
.
βββ 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