Skip to content

p6m-archetypes/python-rest-service-uv-basic.archetype

Repository files navigation

Python REST Service Basic Archetype

Latest Release

A production-ready Archetect archetype for generating modular, enterprise-grade Python REST services with modern tooling and best practices.

🎯 What This Generates

This archetype creates a complete, production-ready REST service with:

  • πŸ—οΈ Modular Architecture: Separate packages for API, core logic, persistence, server, and client
  • ⚑ Modern Python Tooling: UV for dependency management, modern pyproject.toml configuration
  • 🐳 Docker-Ready: Complete containerization with Docker Compose orchestration
  • πŸ“Š Monitoring Stack: Integrated Prometheus metrics and Grafana dashboards
  • πŸ§ͺ Comprehensive Testing: Unit, integration, and REST connectivity tests
  • πŸ”„ CI/CD Pipeline: GitHub Actions workflows for testing, building, and deployment
  • πŸ“‹ Health Checks: Built-in health endpoints and service monitoring
  • πŸ”§ Development Tools: Scripts for setup, testing, and database management

πŸ“¦ Generated Service Structure

my-awesome-service/
β”œβ”€β”€ my-awesome-service-api/          # API contracts and models
β”œβ”€β”€ my-awesome-service-core/         # Business logic
β”œβ”€β”€ my-awesome-service-persistence/  # Database layer with migrations
β”œβ”€β”€ my-awesome-service-server/       # FastAPI server implementation
β”œβ”€β”€ my-awesome-service-client/       # HTTP client library
β”œβ”€β”€ my-awesome-service-integration-tests/  # End-to-end tests
β”œβ”€β”€ monitoring/                      # Grafana & Prometheus config
β”œβ”€β”€ scripts/                         # Development utilities
β”œβ”€β”€ docker-compose.yml              # Complete stack orchestration
└── Dockerfile                      # Multi-stage production build

πŸš€ Quick Start

Prerequisites

  • Archetect CLI tool
  • Git access to this repository

Generate a New Service

archetect render https://github.com/p6m-archetypes/python-rest-service-uv-basic.archetype.git#v1

Development Workflow

cd my-new-service

# 1. Sync all packages
find . -name "pyproject.toml" -exec sh -c 'cd "$(dirname "$1")" && echo "Syncing $(pwd)" && uv sync' _ {} \;

# 2. Start the complete stack
docker-compose up -d

# 3. Run integration tests
./scripts/run-tests.sh

# 4. Access your service
# - REST API: http://localhost:8000
# - Health: http://localhost:8000/health
# - OpenAPI Docs: http://localhost:8000/docs
# - Metrics: http://localhost:8000/metrics
# - Grafana: http://localhost:3000 (admin/admin)
# - Prometheus: http://localhost:9090

✨ Key Features

πŸ›οΈ Enterprise Architecture

  • Hexagonal Architecture: Clean separation of concerns
  • Modular Design: Independent, reusable packages
  • Dependency Injection: Proper service layer organization
  • Error Handling: Structured exception management

πŸ”§ Modern Python Ecosystem

  • UV Package Manager: Fast, reliable dependency management
  • Python 3.11+: Modern language features and performance
  • FastAPI: High-performance, modern async web framework
  • Pydantic: Type-safe data validation and serialization
  • SQLAlchemy 2.0: Modern ORM with async support
  • Structured Logging: JSON-structured logs with contextual information

πŸ“Š Production Monitoring

  • Prometheus Metrics: Business and technical metrics
  • Grafana Dashboards: Pre-configured service monitoring
  • Health Checks: Comprehensive service health reporting
  • OpenTelemetry: Distributed tracing and observability

πŸ§ͺ Testing Excellence

  • pytest Framework: Modern testing with async support
  • TestContainers: Isolated integration testing
  • HTTP Testing: Comprehensive REST API connectivity validation
  • Coverage Reporting: Code coverage with HTML reports
  • CI Integration: Automated testing in GitHub Actions

🚒 Production Ready

  • Multi-stage Dockerfile: Optimized container builds
  • Docker Compose: Complete development environment
  • Database Migrations: Alembic-powered schema management
  • Security: JWT authentication and rate limiting middleware
  • Performance: Connection pooling and async operations
  • OpenAPI Documentation: Auto-generated API documentation

🎯 Use Cases

This archetype is ideal for:

  1. Public-Facing APIs: External APIs requiring wide compatibility

    • OpenAPI/Swagger documentation for easy integration
    • HTTP/JSON standard supported by all programming languages
    • Third-party integrations and partner APIs
    • Can be consumed directly by frontend applications
  2. API Gateways & Aggregation: Services that federate internal microservices

    • Aggregate multiple gRPC or REST services behind a unified REST API
    • Backend-for-Frontend (BFF) pattern for web and mobile apps
    • Transform internal service responses into client-optimized formats
    • Handle authentication, rate limiting, and CORS for frontend clients
  3. CRUD Applications: Standard Create, Read, Update, Delete operations

    • Resource-based REST endpoints with HTTP verbs
    • Simple data models with straightforward persistence
    • Admin interfaces and internal tools
  4. Python-First Architectures: When Python's ecosystem is the primary choice

    • FastAPI for high-performance async HTTP
    • Integration with Python data science and ML libraries
    • Modern Python 3.11+ async/await support

Architectural Note: REST services offer flexibility:

  • Consume gRPC services internally, expose REST externally (gateway pattern)
  • Stand-alone services with direct database access (monolithic microservice)
  • Pure HTTP architecture where all services communicate via REST

πŸ“‹ Validation & Quality

This archetype includes a comprehensive validation suite that ensures generated services meet production standards:

  • βœ… 0 manual fixes required - Services work immediately after generation
  • βœ… <2 minutes from generation to running service - Fast development cycle
  • βœ… 100% integration test pass rate - Reliable, tested code
  • βœ… Template validation - No hardcoded values remain

Run the validation suite:

./validate_archetype.sh

πŸ› οΈ Recent Improvements

This archetype has been extensively updated and tested:

Fixed Issues βœ…

  1. Package Configuration Modernization - Updated all pyproject.toml files to modern standards
  2. FastAPI Version Consistency - Pinned FastAPI and dependencies to stable versions
  3. Python Package Structure - Fixed namespace organization and import paths
  4. Docker Configuration - Updated container setup for reliable operation
  5. Monitoring Infrastructure - Added complete Prometheus/Grafana stack
  6. Integration Testing - Fixed pytest hooks and REST connectivity tests
  7. CI/CD Pipelines - Updated GitHub Actions with proper template variables
  8. Database Configuration - Replaced hardcoded database references
  9. Build System - Cleaned up package references and dependencies
  10. Template Validation - Created comprehensive validation tools
  11. Test Structure Alignment - Aligned test patterns with gRPC archetype for consistency
  12. Port Configuration - Standardized on port 8000 for REST services

Verification βœ…

  • Comprehensive test suite validates all success criteria
  • Reference implementation comparison ensures correctness
  • Integration testing covers all service components
  • Monitoring validation confirms observability stack works
  • CI/CD testing verifies automation pipelines

πŸ“š Documentation

Generated services include comprehensive documentation:

  • README.md - Complete setup and usage instructions
  • OpenAPI Documentation - Interactive API documentation at /docs
  • Development Guide - Local development workflow
  • Deployment Guide - Production deployment instructions
  • Monitoring Guide - Observability and alerting setup

πŸ”§ API Features

REST Endpoints

  • CRUD Operations: Complete Create, Read, Update, Delete functionality
  • Pagination: Built-in pagination support for list endpoints
  • Filtering: Query parameter filtering capabilities
  • Validation: Automatic request/response validation with Pydantic
  • Error Handling: Consistent error responses with proper HTTP status codes

Documentation

  • OpenAPI 3.0: Auto-generated OpenAPI/Swagger specifications
  • Interactive Docs: Swagger UI at /docs and ReDoc at /redoc
  • Schema Validation: Runtime request/response validation
  • Type Safety: Full type hints throughout the codebase

🀝 Contributing

This archetype is actively maintained and improved. For issues or enhancements:

  1. Check existing issues in the repository
  2. Create detailed bug reports or feature requests
  3. Follow the contribution guidelines
  4. Test changes with the validation suite

πŸ“„ License

This archetype is released under the MIT License. Generated services inherit this license but can be changed as needed for your organization.


Ready to build production-grade REST services? Generate your first service with the command above and have a fully functional microservice running in under 2 minutes! πŸš€

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 8