A comprehensive collection of Rust infrastructure modules designed for building robust blockchain and distributed systems. This workspace provides foundational components that handle common infrastructure concerns including configuration management, caching, database operations, web services, and more.
The infra-rs workspace follows a layered architecture approach where each module serves specific infrastructure concerns:
βββββββββββββββββββ¬ββββββββββββββββββ¬ββββββββββββββββββ
β Application β Web Services β CLI Tools β
βββββββββββββββββββΌββββββββββββββββββΌββββββββββββββββββ€
β web-infra β cfg-nacos β cli-infra β
βββββββββββββββββββΌββββββββββββββββββΌββββββββββββββββββ€
β sql-infra β cache-infra β rksdb-infra β
βββββββββββββββββββ΄ββββββββββββββββββ΄ββββββββββββββββββ€
β base-infra (Foundation) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
The cornerstone module providing essential utilities and patterns used across all other modules.
Key Features:
- Error Handling: Comprehensive error management with structured error codes and macros
- Configuration: Figment-based configuration management with environment variable support
- Logging: Structured logging with tracing support
- Runtime Management: Tokio and Rayon runtime abstractions
- Codecs: Binary serialization support (bincode)
- Utilities: Time, UUID, string, and vector utilities
- Validation: Input validation framework
Dependencies:
base-infra = { workspace = true, features = ["tokio-pool", "bincode"] }High-performance caching solution with schema-based type safety and multiple codec support.
Key Features:
- Type-Safe Caching: Schema-based caching with compile-time type safety
- Multiple Codecs: Support for bincode and BCS serialization
- TTL Support: Time-to-live functionality for cache entries
- Async/Sync: Both synchronous and asynchronous cache operations
- Memory Management: Intelligent memory management with Moka
Usage Example:
use cache_infra::{Cacheable, init_cache};
// Initialize cache system
init_cache();
// Define schema and use caching
let cache = AsyncMemCache::new();
cache.store(key, value).await;
let result = cache.load(&key).await;Advanced RocksDB integration with schema-based operations and TTL support.
Key Features:
- Schema System: Type-safe database operations with schema definitions
- TTL Support: Built-in time-to-live functionality with automatic cleanup
- Batch Operations: Efficient batch read/write operations
- Column Families: Advanced column family management
- Compression: LZ4 compression support
- Async Cleanup: Background TTL cleanup scheduling
Usage Example:
use rksdb_infra::{OpenRocksDB, schemadb::ttl::*};
// Create database with TTL support
let db = MyDB::new(path, "mydb", &config, false, true)?;
// Store data with TTL (expires in 10 minutes)
let expire_at = timestamp_after_minutes(10);
db.put_with_ttl::<MySchema>(&key, &value, expire_at)?;
// Read with automatic TTL checking
let result = db.get_check_ttl::<MySchema>(&key)?;Comprehensive SQL database layer built on SeaORM with PostgreSQL and SQLite support.
Key Features:
- SeaORM Integration: Full SeaORM compatibility with extensions
- Multi-Database: PostgreSQL and SQLite support
- Big Number Support: Ethereum uint types (U64, U128, U256) mapping
- Connection Pooling: Advanced connection pool management
- Pagination: Built-in pagination utilities
- Type Safety: Compile-time type safety for database operations
PostgreSQL Big Number Support:
use sql_infra::sea_ext::uint_types::{DbU64, DbU128, DbU256};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "transactions")]
pub struct Model {
#[sea_orm(column_type = "BigInteger")]
pub gas_used: DbU64,
#[sea_orm(column_type = "Decimal(Some((78, 0)))")]
pub wei_amount: DbU256,
}Axum-based web framework foundation with error handling and tracing integration.
Key Features:
- Axum Integration: Built on Axum web framework
- Error Handling: Structured error responses with HTTP status codes
- Request Tracing: OpenTelemetry compatible request tracing
- Pagination: Built-in pagination support for APIs
- Type Safety: Strong typing for HTTP handlers
Nacos service discovery and configuration management client.
Key Features:
- Service Discovery: Automatic service registration and discovery
- Configuration Management: Dynamic configuration updates
- Health Checking: Built-in health check mechanisms
- Load Balancing: Client-side load balancing support
- Async Operations: Full async/await support
Command-line interface development utilities built on Clap.
Key Features:
- Clap Integration: Modern CLI argument parsing
- Error Integration: Seamless error handling with base-infra
- Logging Support: CLI-friendly logging configuration
In your Cargo.toml:
[dependencies]
base-infra = { workspace = true, features = ["tokio-pool"] }
cache-infra = { workspace = true }
sql-infra = { workspace = true, features = ["pgsql"] }
web-infra = { workspace = true }use base_infra::{config::ConfigExt, logger};
use sql_infra::{DatabaseTrait, cfgs::PgSqlConfig};
use cache_infra::init_cache;
#[tokio::main]
async fn main() -> base_infra::result::AppResult<()> {
// Initialize logging
let _guard = logger::init_logger()?;
// Load configuration
let config = AppConfig::load("config.yml".into())?;
// Initialize cache
init_cache();
// Setup database
let db = GlobalDatabase::setup(&config.database).await?;
// Start your application
Ok(())
}use base_infra::{result::AppResult, map_err, gen_impl_code_enum};
// Define custom error codes
gen_impl_code_enum! {
MyServiceErr {
ValidationFailed = ("SVC001", "Input validation failed"),
DatabaseError = ("SVC002", "Database operation failed"),
}
}
// Use in service methods
pub async fn process_data(&self, input: &str) -> AppResult<ProcessResult> {
let validated = self.validate_input(input)
.map_err(map_err!(&MyServiceErr::ValidationFailed))?;
let result = self.database.save(&validated).await
.map_err(map_err!(&MyServiceErr::DatabaseError))?;
Ok(result)
}- Connection Pooling: All database modules support configurable connection pooling
- Async by Default: Built with async/await patterns for high concurrency
- Zero-Copy: Minimal data copying with efficient serialization
- Resource Management: Proper resource cleanup and lifecycle management
- Error Handling: Comprehensive error handling with structured error codes
- Graceful Shutdown: Support for graceful service shutdown
- Health Checks: Built-in health checking for all external dependencies
- Retry Logic: Configurable retry mechanisms for transient failures
- Structured Logging: OpenTelemetry compatible structured logging
- Request Tracing: Full request tracing across service boundaries
- Metrics: Built-in metrics collection for performance monitoring
- Error Tracking: Detailed error context for debugging
All modules follow workspace dependency management. Dependencies are centrally managed in the root Cargo.toml:
[workspace.dependencies]
tokio = { version = "1.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
anyhow = "1.0"
# ... other dependenciesModules reference dependencies using:
[dependencies]
tokio.workspace = true
serde.workspace = trueEach module provides optional feature flags for fine-grained control:
- base-infra:
tokio-pool,rayon-pool,bincode,http - sql-infra:
pgsql,sqlite - rksdb-infra: Inherits from
base-infrafeatures
- API Documentation: Run
cargo doc --openfor detailed API docs - Examples: Each module includes examples in
examples/directory - Tests: Comprehensive test coverage with
cargo test
- Follow the existing code patterns and error handling conventions
- Add comprehensive unit tests for new functionality
- Update documentation for public APIs
- Ensure all modules compile with their default features
- Follow the workspace dependency management rules
This project is licensed under the terms specified in the workspace root.
Note: This infrastructure is designed for production use in blockchain and distributed systems. Each module is battle-tested and follows Rust best practices for performance, safety, and maintainability.