- Clean Architecture: Separated layers (Controller, Service, Repository, Model, Adapters)
- Strict Model Validation: Models enforce required parameters and throw errors for missing fields
- Database Integration: PostgreSQL with SQLAlchemy ORM
- Observability: Structured logging with Prometheus metrics
- Error Handling: Centralized error handling middleware
- Health Checks: Database connectivity and application health monitoring
- API Documentation: Automatic OpenAPI/Swagger documentation
┌─────────────────┐
│ Controllers │ ← HTTP layer (FastAPI)
└─────────────────┘
│
┌─────────────────┐
│ Services │ ← Business logic
└─────────────────┘
│
┌─────────────────┐
│ Repositories │ ← Data access layer
└─────────────────┘
│
┌─────────────────┐
│ Adapters │ ← Infrastructure (DB, Logging)
└─────────────────┘
│
┌─────────────────┐
│ Models │ ← Domain entities with validation
└─────────────────┘
- Clone and setup:
git clone https://github.com/BotecoDaFerramenta/pythoncrudbp.git
cd pythoncrudbp
cp .env.example .env- Run with Docker Compose:
docker-compose up --build- Access the application:
- API: http://localhost:8000
- API Docs: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
- Metrics: http://localhost:8000/metrics
- Prometheus: http://localhost:9090
- Grafana: http://localhost:3000 (admin/admin)
POST /api/v1/users/- Create userGET /api/v1/users/{id}- Get user by IDGET /api/v1/users/- Get all usersPUT /api/v1/users/{id}- Update userDELETE /api/v1/users/{id}- Delete user
GET /health- Health checkGET /metrics- Prometheus metricsGET /- Root endpoint
The application enforces strict model validation. Models will raise ValidationError if:
- Required fields are missing from constructor arguments
- Required fields are set to
None - Field values don't meet validation criteria
Example:
# This will raise ValidationError
user = User(name="John") # Missing required 'email' field
# This will also raise ValidationError
user = User(name="John", email=None) # Required field cannot be None
# This works correctly
user = User(name="John", email="[email protected]")# Run tests
pytest tests/
# Run with coverage
pytest tests/ --cov=app --cov-report=html- Install dependencies:
pip install -r requirements.txt- Run database migrations:
# Using docker-compose
docker-compose exec app alembic upgrade head- Run application locally:
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000The application includes comprehensive observability:
- Structured Logging: JSON formatted logs with correlation IDs
- Metrics: Prometheus metrics for requests, errors, and performance
- Health Checks: Database connectivity and application status
- Error Tracking: Centralized error handling with detailed context
| Variable | Description | Default |
|---|---|---|
DATABASE_URL |
PostgreSQL connection string | postgresql://user:password@localhost:5432/myapp |
LOG_LEVEL |
Logging level | INFO |
ENVIRONMENT |
Environment name | development |
APP_NAME |
Application name | MyPythonApp |
DEBUG |
Debug mode | false |
- Follow the established architecture patterns
- Ensure all models have proper validation
- Add tests for new functionality
- Update documentation as needed
- Use structured logging for observability
MIT License