Skip to content

Instantly share code, notes, and snippets.

@chrissharkey
Created May 19, 2015 16:58
Show Gist options
  • Save chrissharkey/f2bb96ca2bce01b56415 to your computer and use it in GitHub Desktop.
Save chrissharkey/f2bb96ca2bce01b56415 to your computer and use it in GitHub Desktop.
extern crate hyper;
use std::io::prelude::*;
use hyper::Client;
use hyper::header::Connection;
use hyper::header::Basic;
use hyper::header::Authorization;
use std::sync::{Arc, Mutex};
use std::thread;
use std::sync::mpsc;
#[no_mangle]
pub extern fn couchquickview() {
let couch_url = "https://chrissharkey.cloudant.com/bislr_development/_all_docs?startkey=\"contacts_\"&endkey=\"contactt_\"&limit=1000&include_docs=true";
let auth = Basic { username: "chrissharkey".to_string(), password: Some("REDACTED".to_string()) };
let mut client = Client::new();
let mut res = client.get(couch_url)
.header(Authorization(auth))
.header(Connection::close())
.send().unwrap();
let mut body = String::new();
res.read_to_string(&mut body).unwrap();
let lines: Vec<&str> = body.split("\n").collect();
println!("LINE 1: {}", lines.len());
let data = Arc::new(Mutex::new(0u32));
let (tx, rx) = mpsc::channel();
for l in 0..lines.len() {
let (data, tx) = (data.clone(), tx.clone());
thread::spawn(move || {
let mut data = data.lock().unwrap();
*data += 1;
println!("LINE: {}", lines[l].clone());
tx.send(());
});
}
for s in 0..lines.len() {
rx.recv();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment