Skip to content

Commit

Permalink
feat: add a unit socket
Browse files Browse the repository at this point in the history
  • Loading branch information
raghav-rama committed Apr 22, 2024
1 parent 5964994 commit 3bf0e0b
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/bin/socket_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use reqwest::blocking::Client;
use reqwest::Url;
use std::os::unix::net::UnixStream;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a Unix stream connected to the Docker socket
let socket_path = "/var/run/docker.sock";
let stream = match UnixStream::connect(socket_path) {
Ok(sock) => sock,
Err(e) => {
println!("Couldn't connect: {e:?}");
return Ok(());
}
};

// Emulate this
// curl --unix-socket /var/run/docker.sock -X GET http:/v1.45/containers/json

// Create a reqwest client with the Unix stream as the connector
let client = Client::builder().build().unwrap();

// Make the GET request to the specified endpoint
let url = Url::parse("http://localhost/v1.45/containers/json")?;
let response = client.get(url).send()?;

// Check the response status and print the response body
if response.status().is_success() {
let body = response.text()?;
println!("Response Body: {}", body);
} else {
println!("Request failed with status: {}", response.status());
}

Ok(())
}

0 comments on commit 3bf0e0b

Please sign in to comment.