Pragma Template is a template for creating a new service using axum and diesel. It follows modern Rust practices, emphasizing reliability, safety, and maintainability.
- Modern Architecture: Built with a modular crate-based design
- High Performance: Leverages Rust's performance and safe concurrency
- Database Integration: Uses PostgreSQL with Diesel ORM and connection pooling
- API Documentation: Auto-generated OpenAPI documentation via Utoipa
- Observability: Integrated with OpenTelemetry for tracing and monitoring
- Containerization: Docker ready with multi-stage builds for minimal image size
- Development Tools: Docker Compose for local development environment
- CI/CD: GitHub Actions workflows for automated testing and deployments
The project is organized into a workspace with multiple crates:
pragma-axum-diesel-template/
├── bin/ # Binary crates
│ └── pragma-bin/ # Main executable
├── crates/ # Library crates
│ ├── pragma-api/ # API logic and routes
│ └── pragma-db/ # Database models and interactions
| └── migrations/ # Database migrations
└── src/ # Workspace shared code
This structure allows for clear separation of concerns and easier maintenance.
- Rust 1.86.0 or newer
- Docker and Docker Compose (for development environment)
- PostgreSQL (for production or standalone use)
-
Clone the repository:
git clone https://github.com/yourusername/pragma-axum-diesel-template.git cd pragma-axum-diesel-template
-
Copy the example environment file:
cp .env.example .env
-
Update
.env
with your configuration values
The project includes a Docker Compose file for setting up a local development environment:
# Start the development environment
docker-compose -f compose.dev.yaml up -d
# Check the services
docker-compose -f compose.dev.yaml ps
This will start:
- PostgreSQL database
- Grafana LGTM stack for observability (Loki, Grafana, Tempo, Mimir)
# Build the project
cargo build
# Run tests
cargo nextest run
# Run with cargo
cargo run --bin pragma-bin
# Build the Docker image
docker build -t pragma-bin .
# Run the container
docker run -p 3000:3000 --env-file .env pragma-bin
The service is configured via environment variables:
DATABASE_URL
: PostgreSQL connection stringDATABASE_MAX_CONN
: Maximum database connections in the poolAPI_PORT
: Port for the API serverOTEL_COLLECTOR_ENDPOINT
: OpenTelemetry collector endpoint for tracing
When the service is running, API documentation is available at:
http://localhost:{API_PORT}/v1/docs
The documentation is automatically generated from the API code using Utoipa.
Database migrations are managed with Diesel:
# Install Diesel CLI (if not already installed)
cargo install diesel_cli --no-default-features --features postgres
# Run migrations
diesel migration run
# Create a new migration
diesel migration generate name_of_migration
# Run all tests
cargo test
# Run specific tests
cargo test -p pragma-api
# Format and lint code
make format
To complete this template you can:
-
Models and Business Logic
- Implement models in
crates/pragma-db/src/models/
- Add business logic in
crates/pragma-api/src/handlers/
- Create database migrations
- Implement models in
-
API Endpoints
- Implement endpoints in
crates/pragma-api/src/handlers/
- Implement endpoints in
-
Testing and Documentation
- Write comprehensive tests for all endpoints
- Complete API documentation
- Add usage examples
- Security: Always follow security best practices
- Testing: Maintain high test coverage using codecov and nextes.st
- Documentation: Keep API documentation up to date
- Error Handling: Implement proper error handling and user feedback
- Rate Limiting: Consider implementing rate limiting to prevent abuse
- Audit Logging: Add detailed audit logging for security events
- Fork the repository
- Create your feature branch:
git checkout -b feature/amazing-feature
- Commit your changes:
git commit -m 'Add some amazing feature'
- Push to the branch:
git push origin feature/amazing-feature
- Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.