# Zinit Rust Client Examples
This directory contains examples demonstrating how to use the Rust client for the Zinit process supervisor.
## Prerequisites
You need a running Zinit server. If you don't have one, you can start it with:
```bash
zinit
```
Or use the default Unix socket at `/run/zinit/zinit.sock`.
## Examples
### `main.rs` - Basic Usage
Demonstrates fundamental operations:
- Connecting to the zinit server
- Listing services
- Creating a new service
- Checking service status
- Starting and stopping services
- Viewing service logs
Run with:
```bash
cargo run --example main --manifest-path packages/clients/Cargo.toml
```
### `advanced.rs` - Advanced Features
Demonstrates more complex features:
- Services with environment variables
- Health checks (TCP, HTTP)
- Service dependencies
- Lifecycle configuration (restart policies, timeouts)
- Conflicting services
- Viewing dependency tree
Run with:
```bash
cargo run --example advanced --manifest-path packages/clients/Cargo.toml
```
## API Overview
The Rust client provides:
```rust
use herolib_clients::zinit::ZinitHandle;
// Create a handle to communicate with zinit
let z = ZinitHandle::new()?;
// System operations
z.ping()?;
z.list()?;
z.tree()?;
// Service operations
z.service_new(&config)?;
z.status("service_name")?;
z.start("service_name")?;
z.stop("service_name")?;
z.restart("service_name")?;
z.logs("service_name", None)?;
// Xinit proxy operations
z.xinet_new(&config)?;
z.xinet_list()?;
z.xinet_remove("proxy_name")?;
```
## Error Handling
All methods return `Result<T>` where errors represent:
- Connection failures
- Service not found
- Invalid configuration
- Server errors
Example:
```rust
match z.start("my_service") {
Ok(_) => println!("Service started"),
Err(e) => eprintln!("Failed to start service: {}", e),
}
```
## Service Configuration
Services are defined using `ServiceDef` which includes:
```rust
ServiceDef {
name: String, // Service name
exec: String, // Command to execute
dir: Option<String>, // Working directory
oneshot: Option<bool>, // One-time execution
env: HashMap<String, String>, // Environment variables
critical: Option<bool>, // Critical service flag
class: Option<String>, // Service class (system/user/etc)
dependencies: Option<DependencyDef>, // Dependencies
lifecycle: Option<LifecycleDef>, // Restart policies
health: Option<HealthDef>, // Health checks
logging: Option<LoggingDef>, // Log configuration
xinet: Option<XinetConfig>, // Xinit proxy config
// ... other fields
}
```
## See Also
- [Rhai Examples](../rhai/zinit/) - Dynamic scripting examples
- [Zinit Documentation](../../../../docs/reference/)
- [OpenRPC Specification](../../../../docs/reference/openrpc.json)