A production-ready Python microservice template built with gRPC, dependency injection, and best practices.
This microservice template provides:
- gRPC API - High-performance, type-safe inter-service communication
- Dependency Injection - Clean architecture with dependency-injector
- Structured Logging - JSON and console logging with structlog
- Configuration Management - Environment-based config with Pydantic
- Comprehensive Testing - 100% test coverage with pytest
- Type Safety - Full type hints and mypy validation
- Code Quality - Black, isort, and flake8 for consistent code style
- User management CRUD operations (example implementation)
- Health check endpoint
- Graceful shutdown handling
- Signal handling (SIGINT, SIGTERM)
- Clean separation of concerns
- Repository pattern ready (with example implementation)
- Modular container-based dependency injection
- Environment-based configuration
- Structured error handling
- Hot reloading in development
- Comprehensive test suite
- Code generation from proto definitions
- Docker support
- Make-based build system
- Python 3.12+
- Poetry
- Make (optional, for convenience commands)
- Install service dependencies:
make install
# or
poetry install- Run tests:
make test- Start the service:
make run
# or
poetry run microservicepy-micro-template-service/
├── src/py_micro/service/ # Main application code
│ ├── config/ # Configuration management
│ ├── containers/ # Dependency injection containers
│ ├── template_service.py # gRPC service implementation
│ └── main.py # Application entry point
├── tests/ # Test suite
│ ├── unit/ # Unit tests
│ ├── integration/ # Integration tests
│ └── conftest.py # Test configuration
├── Makefile # Build automation
├── pyproject.toml # Poetry configuration
└── README.md # This file
The service uses environment-based configuration with sensible defaults:
SERVER_HOST=0.0.0.0 # Server host (default: 0.0.0.0)
SERVER_PORT=50051 # Server port (default: 50051)
SERVER_MAX_WORKERS=10 # Max worker threads (default: 10)
SERVER_GRACE_PERIOD=30 # Shutdown grace period (default: 30)LOGGING_LEVEL=INFO # Log level (default: INFO)
LOGGING_FORMAT=json # Log format: json|console (default: json)APP_NAME=my-service # Application name
VERSION=1.0.0 # Application version
DEBUG=false # Debug mode (default: false)
ENVIRONMENT=production # Environment name (default: development)Development:
make dev-run
# or
poetry run python -m py_micro.service.mainProduction:
make run
# or
poetry run microserviceThe service provides a gRPC API. You can test it using:
- grpcurl (recommended):
# Health check
grpcurl -plaintext localhost:50051 template.v1.TemplateService/HealthCheck-
Python client (see integration tests for examples)
-
Postman with gRPC support
- Define the service in the model repository:
// In py-micro-model/proto/my_service.proto
service MyService {
rpc MyMethod(MyRequest) returns (MyResponse);
}- Implement the service:
# In src/py_micro/service/my_service.py
from py_micro.service.my_service_pb2_grpc import MyServiceServicer
class MyService(MyServiceServicer):
def MyMethod(self, request, context):
# Implementation here
pass- Register in container:
# In src/py_micro/service/containers/__init__.py
my_service = providers.Factory(MyService, ...)- Add to server:
# In src/py_micro/service/main.py
from py_micro.model.my_service_pb2_grpc import add_MyServiceServicer_to_server
add_MyServiceServicer_to_server(my_service, self._server)Run all tests:
make testRun specific test types:
make test-unit # Unit tests only
make test-integration # Integration tests onlyGenerate coverage report:
make coverage
# Open htmlcov/index.html to view detailed coverageWatch mode for development:
make dev-test-watchFormat code:
make formatRun linting:
make lintType checking:
poetry run mypy src/Build package:
make buildDocker (if Dockerfile is added):
make docker-build
make docker-run- Follow the existing code style
- Add tests for new functionality
- Update documentation
- Run the full test suite before submitting
This template is provided as-is for educational and development purposes.
For issues and questions:
- Check the troubleshooting section
- Review the test cases for usage examples
- Check the generated documentation