Skip to content
This repository was archived by the owner on Dec 15, 2025. It is now read-only.

Commit 7776184

Browse files
committed
apply cargo fmt
1 parent 59ffb4c commit 7776184

File tree

8 files changed

+127
-88
lines changed

8 files changed

+127
-88
lines changed

src/app/api.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use serde_json::Value;
1010
use std::collections::HashMap;
1111
use std::sync::Arc;
1212
use tokio::sync::Mutex;
13+
use tower_http::cors::{AllowHeaders, AllowMethods};
1314
use tower_http::cors::{Any, CorsLayer};
14-
use tower_http::cors::{AllowMethods, AllowHeaders};
1515

1616
#[derive(Clone, Debug, Deserialize, Serialize)]
1717
#[serde(rename_all = "lowercase")]
@@ -83,7 +83,8 @@ impl Api {
8383
let middleware = tower::ServiceBuilder::new().layer(cors);
8484

8585
// Create the JSON-RPC server with CORS support
86-
let server_rpc = jsonrpsee::server::ServerBuilder::default().set_http_middleware(middleware)
86+
let server_rpc = jsonrpsee::server::ServerBuilder::default()
87+
.set_http_middleware(middleware)
8788
.build(socket_addr)
8889
.await?;
8990

src/app/mod.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ use reth_ipc::client::IpcClientBuilder;
88
use rpc::ZinitLoggingApiClient;
99
use rpc::ZinitServiceApiClient;
1010
use rpc::ZinitSystemApiClient;
11-
use std::net::ToSocketAddrs;
1211
use serde_yaml as encoder;
12+
use std::net::ToSocketAddrs;
1313
use std::path::{Path, PathBuf};
1414
use tokio::fs;
15+
use tokio::signal;
1516
use tokio::time;
1617
use tokio_stream::wrappers::ReceiverStream;
1718
use tokio_stream::Stream;
18-
use tokio::signal;
1919

2020
fn logger(level: log::LevelFilter) -> Result<()> {
2121
let logger = fern::Dispatch::new()
@@ -175,7 +175,11 @@ pub async fn kill(socket: &str, name: String, signal: String) -> Result<()> {
175175
Ok(())
176176
}
177177

178-
pub async fn logs(socket: &str, filter: Option<String>, follow: bool) -> Result<impl Stream<Item = String> + Unpin> {
178+
pub async fn logs(
179+
socket: &str,
180+
filter: Option<String>,
181+
follow: bool,
182+
) -> Result<impl Stream<Item = String> + Unpin> {
179183
let client = IpcClientBuilder::default().build(socket.into()).await?;
180184
if let Some(ref filter) = filter {
181185
client.status(filter.clone()).await?;
@@ -204,7 +208,7 @@ pub async fn logs(socket: &str, filter: Option<String>, follow: bool) -> Result<
204208
Some(Ok(log)) => {
205209
if tx.send(log).await.is_err() {
206210
let _ = logs_sub.unsubscribe().await;
207-
return
211+
return;
208212
}
209213
}
210214
Some(Err(e)) => {
@@ -222,7 +226,8 @@ pub async fn logs(socket: &str, filter: Option<String>, follow: bool) -> Result<
222226
/// Start an HTTP/RPC proxy server for the Zinit API at the specified address
223227
pub async fn proxy(sock: &str, address: String) -> Result<()> {
224228
// Parse the socket address
225-
let _socket_addr = address.to_socket_addrs()
229+
let _socket_addr = address
230+
.to_socket_addrs()
226231
.context("Failed to parse socket address")?
227232
.next()
228233
.context("No valid socket address found")?;
@@ -232,20 +237,20 @@ pub async fn proxy(sock: &str, address: String) -> Result<()> {
232237

233238
// Connect to the existing Zinit daemon through the Unix socket
234239
let client = IpcClientBuilder::default().build(sock.into()).await?;
235-
240+
236241
// Issue an RPC call to start the HTTP server on the specified address
237242
let result = client.start_http_server(address.clone()).await?;
238-
243+
239244
println!("{}", result);
240245
println!("Press Ctrl+C to stop");
241-
246+
242247
// Wait for Ctrl+C to shutdown
243248
signal::ctrl_c().await?;
244-
249+
245250
// Shutdown the HTTP server
246251
client.stop_http_server().await?;
247-
252+
248253
println!("HTTP/RPC server stopped");
249-
254+
250255
Ok(())
251-
}
256+
}

src/app/rpc.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::zinit::config;
21
use crate::app::api::Status;
2+
use crate::zinit::config;
33
use async_trait::async_trait;
44
use jsonrpsee::core::{RpcResult, SubscriptionResult};
55
use jsonrpsee::proc_macros::rpc;
@@ -12,7 +12,6 @@ use tokio_stream::StreamExt;
1212

1313
use super::api::Api;
1414

15-
1615
// Custom error codes for Zinit
1716
const SERVICE_NOT_FOUND: i32 = -32000;
1817
const SERVICE_IS_UP: i32 = -32002;
@@ -286,11 +285,11 @@ pub trait ZinitSystemApi {
286285
/// Initiate system reboot process.
287286
#[method(name = "reboot")]
288287
async fn reboot(&self) -> RpcResult<()>;
289-
288+
290289
/// Start an HTTP/RPC server at the specified address
291290
#[method(name = "start_http_server")]
292291
async fn start_http_server(&self, address: String) -> RpcResult<String>;
293-
292+
294293
/// Stop the HTTP/RPC server if running
295294
#[method(name = "stop_http_server")]
296295
async fn stop_http_server(&self) -> RpcResult<()>;
@@ -311,20 +310,20 @@ impl ZinitSystemApiServer for Api {
311310
.await
312311
.map_err(|_| ErrorObjectOwned::from(ErrorCode::InternalError))
313312
}
314-
313+
315314
async fn start_http_server(&self, address: String) -> RpcResult<String> {
316315
// Call the method from the API implementation
317316
match crate::app::api::Api::start_http_server(self, address).await {
318317
Ok(result) => Ok(result),
319-
Err(_) => Err(ErrorObjectOwned::from(ErrorCode::InternalError))
318+
Err(_) => Err(ErrorObjectOwned::from(ErrorCode::InternalError)),
320319
}
321320
}
322-
321+
323322
async fn stop_http_server(&self) -> RpcResult<()> {
324323
// Call the method from the API implementation
325324
match crate::app::api::Api::stop_http_server(self).await {
326325
Ok(_) => Ok(()),
327-
Err(_) => Err(ErrorObjectOwned::from(ErrorCode::InternalError))
326+
Err(_) => Err(ErrorObjectOwned::from(ErrorCode::InternalError)),
328327
}
329328
}
330329
}
@@ -370,7 +369,7 @@ impl ZinitLoggingApiServer for Api {
370369
) -> SubscriptionResult {
371370
let sink = sink.accept().await?;
372371
let filter = name.map(|n| format!("{n}:"));
373-
let mut stream =
372+
let mut stream =
374373
tokio_stream::wrappers::ReceiverStream::new(self.zinit.logs(false, true).await)
375374
.filter_map(|l| {
376375
if let Some(ref filter) = filter {
@@ -383,10 +382,7 @@ impl ZinitLoggingApiServer for Api {
383382
Some(l.to_string())
384383
}
385384
});
386-
while let Some(log) = stream
387-
.next()
388-
.await
389-
{
385+
while let Some(log) = stream.next().await {
390386
if sink.send(log.into()).await.is_err() {
391387
break;
392388
}

src/bin/testapp.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#[tokio::main]
2-
async fn main () {
2+
async fn main() {
33
println!("hello from testapp");
44
}
55

6-
76
// extern crate zinit;
87

98
// use anyhow::Result;

zinit-client/examples/basic_usage.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,51 @@ use zinit_client::Client;
88
async fn main() -> Result<()> {
99
// Create a client using Unix socket transport
1010
// let unix_client = Client::unix_socket("/var/run/zinit.sock");
11-
11+
1212
// Or create a client using HTTP transport
1313
let http_client = Client::http("http://localhost:8080");
14-
14+
1515
// Choose which client to use for this example
1616
let client = http_client;
17-
17+
1818
// Service name for our example
1919
let service_name = "test1234";
20-
20+
2121
// Step 1: List existing services
2222
println!("Listing all services before creating our test service:");
2323
let services = client.list().await?;
24-
24+
2525
// Print all services
2626
for (name, state) in &services {
2727
println!("- {}: {}", name, state);
2828
}
29-
29+
3030
// Step 2: Create a new service
3131
println!("\n--- Creating service '{}' ---", service_name);
32-
32+
3333
// Create the service configuration
3434
let service_config = json!({
3535
"exec": "echo 'hello from test service'",
3636
"oneshot": false,
3737
"log": "stdout"
38-
}).as_object().unwrap().clone();
39-
38+
})
39+
.as_object()
40+
.unwrap()
41+
.clone();
42+
4043
// Create the service
4144
let result = client.create_service(service_name, service_config).await?;
4245
println!("Create result: {}", result);
43-
46+
4447
// Step 3: Monitor the service
4548
println!("\n--- Monitoring service '{}' ---", service_name);
4649
client.monitor(service_name).await?;
4750
println!("Service is now being monitored");
48-
51+
4952
// Wait a moment for the service to start
5053
println!("Waiting for service to start...");
5154
time::sleep(Duration::from_secs(2)).await;
52-
55+
5356
// Step 4: Get the status of the service
5457
println!("\n--- Getting status for '{}' ---", service_name);
5558
let status = client.status(service_name).await?;
@@ -61,39 +64,39 @@ async fn main() -> Result<()> {
6164
for (dep, state) in &status.after {
6265
println!(" - {}: {}", dep, state);
6366
}
64-
67+
6568
// Step 5: Stop the service
6669
println!("\n--- Stopping service '{}' ---", service_name);
6770
client.stop(service_name).await?;
6871
println!("Service stopped");
69-
72+
7073
// Wait a moment for the service to stop
7174
time::sleep(Duration::from_secs(1)).await;
72-
75+
7376
// Check status after stopping
7477
println!("\n--- Status after stopping ---");
7578
let status = client.status(service_name).await?;
7679
println!("State: {}", status.state);
7780
println!("Target: {}", status.target);
78-
81+
7982
// Step 6: Delete the service
8083
println!("\n--- Deleting service '{}' ---", service_name);
8184
let delete_result = client.delete_service(service_name).await?;
8285
println!("Delete result: {}", delete_result);
83-
86+
8487
// Step 7: Verify the service is gone
8588
println!("\n--- Listing services after deletion ---");
8689
let services_after = client.list().await?;
8790
for (name, state) in &services_after {
8891
println!("- {}: {}", name, state);
8992
}
90-
93+
9194
// Check if our service was deleted
9295
if !services_after.contains_key(service_name) {
9396
println!("\nService '{}' was successfully deleted", service_name);
9497
} else {
9598
println!("\nWarning: Service '{}' still exists", service_name);
9699
}
97-
100+
98101
Ok(())
99-
}
102+
}

zinit-client/examples/http_client.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use zinit_client::Client;
55
async fn main() -> Result<()> {
66
// Create a client using HTTP transport
77
let client = Client::http("http://localhost:8080");
8-
8+
99
println!("Connecting to Zinit via HTTP...");
10-
10+
1111
// List all services
1212
println!("Listing all services:");
1313
match client.list().await {
@@ -18,7 +18,7 @@ async fn main() -> Result<()> {
1818
for (name, state) in &services {
1919
println!("- {}: {}", name, state);
2020
}
21-
21+
2222
// Try to get the first service for a status example
2323
if let Some(service_name) = services.keys().next() {
2424
println!("\nGetting status for {}:", service_name);
@@ -29,17 +29,17 @@ async fn main() -> Result<()> {
2929
println!("State: {}", status.state);
3030
println!("Target: {}", status.target);
3131
println!("Dependencies: {}", status.after.len());
32-
},
32+
}
3333
Err(e) => println!("Error getting status: {}", e),
3434
}
3535
}
3636
}
37-
},
37+
}
3838
Err(e) => {
3939
println!("Error connecting to Zinit HTTP proxy: {}", e);
4040
println!("Make sure the Zinit HTTP proxy is running on http://localhost:8080");
4141
}
4242
}
43-
43+
4444
Ok(())
45-
}
45+
}

zinit-client/src/lib.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ impl Client {
112112
}
113113

114114
/// Send a JSON-RPC request and return the result
115-
async fn jsonrpc_request(&self, method: &str, params: Option<Value>) -> Result<Value, ClientError> {
115+
async fn jsonrpc_request(
116+
&self,
117+
method: &str,
118+
params: Option<Value>,
119+
) -> Result<Value, ClientError> {
116120
// Get a unique ID for this request
117121
let id = self.next_id.fetch_add(1, Ordering::SeqCst);
118122

@@ -124,7 +128,9 @@ impl Client {
124128
};
125129

126130
match &self.transport {
127-
Transport::UnixSocket(socket_path) => self.unix_socket_request(&request, socket_path).await,
131+
Transport::UnixSocket(socket_path) => {
132+
self.unix_socket_request(&request, socket_path).await
133+
}
128134
Transport::Http(url) => self.http_request(&request, url).await,
129135
}
130136
}
@@ -173,8 +179,8 @@ impl Client {
173179
}
174180

175181
// Convert to string and trim the trailing newline
176-
let data = String::from_utf8(buffer)
177-
.map_err(|e| ClientError::InvalidResponse(e.to_string()))?;
182+
let data =
183+
String::from_utf8(buffer).map_err(|e| ClientError::InvalidResponse(e.to_string()))?;
178184
let data = data.trim_end();
179185

180186
// Parse the JSON-RPC response
@@ -315,7 +321,8 @@ impl Client {
315321
"name": name.as_ref()
316322
});
317323

318-
self.jsonrpc_request("service.monitor", Some(params)).await?;
324+
self.jsonrpc_request("service.monitor", Some(params))
325+
.await?;
319326
Ok(())
320327
}
321328

@@ -386,4 +393,4 @@ impl Client {
386393
#[cfg(test)]
387394
mod tests {
388395
// Tests would go here
389-
}
396+
}

0 commit comments

Comments
 (0)