apollo-rust-client
A robust Rust client for the Apollo Configuration Centre, with support for WebAssembly for browser and Node.js environments.
Features
- Multiple Configuration Formats: Support for Properties, JSON, YAML, Text formats with planned XML support
- Automatic Format Detection: Based on namespace file extensions
- Type-Safe Configuration Management: Compile-time guarantees and runtime type conversion
- Cross-Platform Support: Native Rust and WebAssembly targets
- TLS Support: Switchable TLS implementations (native-tls by default, rustls via feature flag)
- Real-Time Updates: Background polling with configurable intervals and event listeners
- Comprehensive Caching: Multi-level caching with file persistence (native) and memory-only (WASM)
- Async/Await Support: Full asynchronous API for non-blocking operations
- Error Handling: Detailed error diagnostics with comprehensive error types
- Grayscale Release Support: IP and label-based configuration targeting
- Flexible Configuration: Direct instantiation or environment variable configuration
- Memory Management: Automatic cleanup with explicit control for WASM environments
Installation
Rust
Add the following to your Cargo.toml:
[]
= "0.6.3"
If you prefer to use rustls instead of the system's native TLS (useful for minimal images like Alpine):
[]
= { = "0.6.3", = false, = ["rustls"] }
Note on TLS Support:
- Non-WASM Targets:
native-tls(enabled by default) andrustlsare mutually exclusive. You must disable default features if you want to userustls. - WASM Targets: Only
native-tls(which uses the browser's fetch API) is supported. Enabling therustlsfeature on WASM targets will result in a compile error.
Alternatively, you can use cargo add:
WebAssembly (npm)
Quick Start
Rust Usage
use Client;
use ClientConfig;
async
Configuration via Environment Variables
use ClientConfig;
// Initialize from environment variables
let client_config = from_env?;
Environment Variables:
APP_ID: Your application ID (required)APOLLO_CONFIG_SERVICE: The Apollo config server URL (required)IDC: The cluster name (optional, defaults to "default")APOLLO_ACCESS_KEY_SECRET: The secret key for authentication (optional)APOLLO_LABEL: Comma-separated list of labels for grayscale rules (optional)APOLLO_CACHE_DIR: Directory to store local cache (optional)APOLLO_CACHE_TTL: Time-to-live for cache in seconds (optional, defaults to 600, native targets only)APOLLO_ALLOW_INSECURE_HTTPS: Whether to allow insecure HTTPS connections (optional, defaults to false)
JavaScript/WebAssembly Usage
import from "@qqiao/apollo-rust-client";
.;
Configuration Formats
The library automatically detects configuration formats based on namespace names:
Properties Format (Default)
- Namespace:
"application","config.properties" - Format: Key-value pairs
- Example:
JSON Format
- Namespace:
"config.json","settings.json" - Format: Structured JSON data
- Example:
{"database": {"host": "localhost", "port": 5432}}
Text Format
- Namespace:
"readme.txt","content.txt" - Format: Plain text content
- Example: Raw text content
YAML Format
- Namespace:
"config.yaml","config.yml" - Format: Structured YAML data
- Example: Complex configuration structures with nested objects
if let Yaml = namespace
Planned Formats
- XML:
"config.xml"
Configuration Options
ClientConfig Fields
app_id: Your application ID in Apollo (required)config_server: The Apollo config server URL (required)cluster: The cluster name (required, typically "default")secret: Optional secret key for authenticationcache_dir: Directory for local cache files (native only)- Default:
/opt/data/{app_id}/config-cache - WASM: Always
None(memory-only caching)
- Default:
label: Label for grayscale releases (optional)ip: IP address for grayscale releases (optional)
Error Handling
The library provides comprehensive error handling:
use Error;
match client.namespace.await
Memory Management (WASM)
For WebAssembly environments, explicit memory management is required:
// Always call free() on WASM objects when done
cache.;
client.;
clientConfig.;
This prevents memory leaks by releasing Rust-allocated memory on the WebAssembly heap.
Advanced Usage
Event Listeners
// Rust - Add event listener
client.add_listener.await;
// JavaScript - Add event listener
await cache.;
Grayscale Releases
let client_config = ClientConfig ;
Custom JSON Deserialization
use ;
if let Json = namespace
API Changes in v0.6.0
- Typed Namespaces: Support for multiple configuration formats with automatic detection
- Event Listeners: Real-time configuration change notifications
- Enhanced Error Handling: Comprehensive error types and better error reporting
- WASM Improvements: Better memory management and JavaScript interop
- Background Polling: Configurable automatic configuration refresh
Documentation
For comprehensive documentation, visit our wiki:
- Installation Guide - Setup instructions
- Rust Usage - Native Rust examples
- JavaScript Usage - WASM usage in JavaScript
- Configuration - Configuration options
- Features - Feature overview
- Error Handling - Error handling guide
- Design Overview - Architecture documentation
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the Apache License Version 2.0 - see the LICENSE file for details.