# kode-bridge
[](https://www.rust-lang.org/)
[](http://www.apache.org/licenses/LICENSE-2.0)
[](https://crates.io/crates/kode-bridge)
**[δΈζ](./README_CN.md) | English**
**kode-bridge** is a modern Rust library designed for cross-platform (macOS, Linux, Windows) IPC HTTP communication. With a unified API, you can easily send HTTP requests via Unix Domain Sockets or Windows Named Pipes, just as simple as using a regular HTTP client.
## β¨ Features
- **π True Cross-Platform**: Automatically detects the platform and uses optimal IPC methods
- **Unix/Linux/macOS**: Unix Domain Sockets
- **Windows**: Named Pipes
- **π Zero Configuration**: Unified `IpcHttpClient` API, no platform-specific code required
- **π¦ Auto Serialization**: Built-in JSON request and response handling
- **β‘ High Performance**: Optimized connection management strategies for different platforms
- **π§ Easy Integration**: Based on [interprocess](https://github.com/kotauskas/interprocess) and Tokio async runtime
- **π Complete Support**: Includes examples, benchmarks, and comprehensive documentation
## π Quick Start
### Add Dependencies
```toml
[dependencies]
kode-bridge = "0.1"
tokio = { version = "1", features = ["full"] }
serde_json = "1.0"
```
### Basic Usage
```rust
use kode_bridge::IpcHttpClient;
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Automatically detect platform and use appropriate IPC path
#[cfg(unix)]
let client = IpcHttpClient::new("/tmp/my_service.sock")?;
#[cfg(windows)]
let client = IpcHttpClient::new(r"\\.\pipe\my_service")?;
// Send GET request
let response = client.request("GET", "/api/version", None).await?;
println!("Status: {}", response.status);
println!("Response: {}", response.body);
// Send POST request
let data = json!({"user": "alice", "action": "login"});
let response = client.request("POST", "/api/auth", Some(&data)).await?;
println!("Auth result: {}", response.json()?);
Ok(())
}
```
### Using Environment Variables
Create a `.env` file:
```env
# Unix systems
CUSTOM_SOCK=/tmp/my_app.sock
# Windows systems (each backslash needs to be escaped by doubling)
CUSTOM_PIPE=\\\\.\\pipe\\\my_app
```
Then in your code:
```rust
use dotenv::dotenv;
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok();
#[cfg(unix)]
let path = env::var("CUSTOM_SOCK").unwrap_or("/tmp/default.sock".to_string());
#[cfg(windows)]
let path = env::var("CUSTOM_PIPE").unwrap_or(r"\\.\pipe\default".to_string());
let client = IpcHttpClient::new(&path)?;
let response = client.request("GET", "/status", None).await?;
Ok(())
}
```
## π Examples
Run built-in examples:
```bash
# Basic request example
cargo run --example request
# Large data request example
cargo run --example request_large
# Using custom IPC path
CUSTOM_SOCK=/tmp/my.sock cargo run --example request # Unix
CUSTOM_PIPE=\\\\.\\pipe\\my_pipe cargo run --example request # Windows
```
## π₯ Performance Benchmarks
Run performance benchmarks:
```bash
# Run all benchmarks
cargo bench
# View benchmark reports
open target/criterion/report/index.html
```
Benchmarks automatically:
- Detect the running platform
- Use appropriate environment variables (`CUSTOM_SOCK` or `CUSTOM_PIPE`)
- Apply platform-specific performance optimization strategies
## ποΈ Architecture Design
```
βββββββββββββββββββββββββββββββββββββββββββ
β IpcHttpClient β
β (Unified Cross-Platform API) β
βββββββββββββββββββββββββββββββββββββββββββ€
β http_client.rs β
β (HTTP Protocol Handler) β
βββββββββββββββββββββββββββββββββββββββββββ€
β interprocess β
β (Cross-Platform IPC Transport) β
βββββββββββββββββββ¬ββββββββββββββββββββββββ€
β Unix Sockets β Windows Pipes β
β (Unix/Linux) β (Windows) β
βββββββββββββββββββ΄ββββββββββββββββββββββββ
```
### Core Components
- **`IpcHttpClient`**: Unified client interface that automatically adapts to different platforms
- **`http_client`**: Platform-agnostic HTTP protocol handling with chunked transfer encoding support
- **Smart Platform Detection**: Compile-time automatic selection of optimal IPC implementation
## π― Use Cases
- **Local Service Communication**: Communicate with local processes like Clash, Mihomo, proxy services, etc.
- **Microservice Architecture**: High-performance inter-process HTTP communication
- **System Integration**: Replace traditional REST API local calls
- **Performance-Critical Applications**: Scenarios requiring low-latency local communication
## π οΈ Development
### Build Project
```bash
git clone https://github.com/KodeBarinn/kode-bridge.git
cd kode-bridge
cargo build
```
### Run Tests
```bash
cargo test
```
### Generate Documentation
```bash
cargo doc --open
```
## π Resources
- [Platform Guide](./PLATFORM_GUIDE.md) - Detailed cross-platform usage guide
- [Examples](./examples/) - Complete example code
- [Benchmarks](./benches/) - Performance benchmarks
## π€ Contributing
We welcome Issues and Pull Requests!
## π License
This project is licensed under the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).
See the [Licence](./Licence) file for details.