API Framework
Macro’s backend uses Axum 0.7 as the web framework for building HTTP APIs. Key features:- Type-safe request/response handling
- Extractors for dependency injection
- Middleware support
- OpenAPI documentation generation
- Async/await with Tokio runtime
axum_rpc Pattern
The codebase uses a custom RPC pattern built on top of Axum, located in:- Consistent RPC-style APIs across services
- Automatic client generation
- Type-safe service communication
- OpenAPI schema generation
Handler Patterns
Using Extension for Dependencies
Important: This codebase usesExtension instead of State for handlers.
Error Handling
Useanyhow::bail! for error returns:
“Preferanyhow::bail!("error message")overErr(anyhow::anyhow!("error message"))”
Tracing Instrumentation
Always includeerr in the tracing::instrument attribute:
“Includeerrwhen adding thetracing::instrumentattribute to functions. Never includelevel = "info".”
Error Logging Pattern
Use.inspect_err() instead of if let Err(e):
OpenAPI Documentation
Services useutoipa for OpenAPI schema generation:
Service Communication
Internal Service Clients
Most services provide a client crate for inter-service communication:Using Service Clients
Async Processing with SQS
For long-running or background operations, use SQS queues:SQS Worker Pattern
Workers process messages from SQS queues:Lambda Integration
Services trigger Lambda functions for:- Event-driven processing (S3 uploads, scheduled tasks)
- Async operations (text extraction, document conversion)
- Scalable workloads
Request/Response Models
Model Organization
Models are organized into domain-specific crates:model- Core shared modelsmodels_email- Email-related typesmodels_search- Search request/response typesmodels_pagination- Pagination utilitiesmodels_permissions- Permission modelsmodels_opensearch- OpenSearch document types
Common Model Pattern
Authentication & Authorization
JWT Token Validation
Permission Checking
Middleware
Common middleware inmacro_middleware:
- Authentication
- Request tracing
- CORS handling (via
macro_cors) - Error handling
- Request/response logging