lmrc-http-common
Common HTTP utilities and patterns for LMRC Stack applications.
This library provides reusable components for building Axum-based HTTP services with best practices built-in.
Features
- Error Handling: Standard HTTP error types with automatic response conversion
- Response Wrappers: Success, created, empty, and paginated response types
- Middleware: CORS, logging, request ID, and more
- Authentication (optional): JWT, password hashing, session management
- Configuration Management: Server and database configuration loading
- Health Checks: Standardized health check framework
- Validated Extractors (optional): Auto-validate requests with
validatorcrate - Server Bootstrap (optional): Quick server setup with tracing and configuration
Installation
Add to your Cargo.toml:
[]
= "0.3.11"
Feature Flags
auth(default) - Authentication utilities (JWT, bcrypt, sessions)validation(default) - Request validation helpersserver- Server bootstrap utilities (requirestracing-subscriber,dotenvy)
To enable all features:
[]
= { = "0.3.11", = ["server"] }
Quick Start
Basic Handler with Error Handling
use ;
use ;
async
Configuration Loading
use ;
async
Health Checks
use ;
use Arc;
use async_trait;
// In your router setup:
let checker = new;
let app = new
.route
.with_state;
Validated Request Extractors
use ValidatedJson;
use Deserialize;
use Validate;
async
Server Bootstrap (with server feature)
use ;
use ServerBootstrap;
async
Or use the quick start helper:
use ;
use quick_start;
async
Module Overview
error
Standard HTTP error types that automatically convert to appropriate HTTP responses:
HttpError::BadRequest(String)→ 400HttpError::Unauthorized(String)→ 401HttpError::Forbidden(String)→ 403HttpError::NotFound(String)→ 404HttpError::ValidationError(String)→ 422HttpError::InternalServer(String)→ 500
Use the app_error! macro to create custom error types:
use app_error;
app_error!
response
Standard response wrappers:
SuccessResponse<T>- Standard success response with dataCreatedResponse<T>- 201 Created with location headerEmptyResponse- 204 No ContentPaginatedResponse<T>- Paginated list with metadata
middleware
Reusable middleware layers:
add_request_id- Adds unique request ID to each requestlog_request- Logs all requests with durationcors_with_origins(Vec<String>)- CORS with specific origins
config
Configuration management:
ServerConfig- Host, port, CORS originsDatabaseConfig- Database URL, connection pool settingsConfigLoadertrait - For custom configuration types
health
Health check framework:
HealthStatus- Overall application healthHealthChecktrait - Implement for custom checksHealthChecker- Aggregates multiple health checkshealth_handler- Axum handler for health endpoint
auth (requires auth feature)
Authentication utilities:
jwt- JWT token creation and verificationpassword- Password hashing and verification (bcrypt)session- Session management
extractors (requires validation feature)
Auto-validating request extractors:
ValidatedJson<T>- JSON with automatic validationValidatedQuery<T>- Query parameters with validation
server (requires server feature)
Server bootstrap utilities:
ServerBootstrap- Fluent API for server setupquick_start- Simple one-liner server start
Examples
See the examples directory for complete working examples:
gateway- API gateway with routing and authapi-service-template- Standard REST API templateinfra-api- Infrastructure management API
Development
Running Tests
Building Documentation
License
Dual licensed under MIT OR Apache-2.0 (your choice).
Contributing
This library is part of the LMRC Stack monorepo.
See the main repository for contribution guidelines.