Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct DoCache {
- droplets: String, // Vec<Droplet>,
- }
- fn do_caching_loop(mut do_cache: Arc<RwLock<DoCache>>) {
- let auth_token = "SECRET";
- let domgr = DoManager::with_token(&auth_token);
- loop {
- info!("Getting Droplets");
- let result: String = domgr.droplets().retrieve_json().unwrap();
- let mut modify_data = do_cache.write().unwrap();
- (*modify_data).droplets = result.clone();
- std::thread::sleep(std::time::Duration::from_millis(50000));
- }
- }
- struct State {
- do_cache: Arc<RwLock<DoCache>>,
- threads: Vec<std::thread::JoinHandle<()>>,
- }
- impl State {
- fn getDroplets(&self) -> String {
- info!("Get Droplets A");
- let cache = self.do_cache.read();
- let mut do_cache = cache.unwrap();
- (*do_cache).droplets.clone()
- }
- }
- fn main() {
- init();
- let mut do_cache = Arc::new(RwLock::new(DoCache { droplets: String::from("") })); // vec![] }));
- let mut state = State {
- do_cache: do_cache.clone(),
- threads: Vec::new(),
- };
- info!("Spawning DO Cache");
- state.threads.push(std::thread::spawn(move || {
- do_caching_loop(do_cache.clone());
- }));
- info!("Serving");
- Iron::new(move |_: &mut Request| Ok(Response::with((status::Ok, state.getDroplets()))))
- .http("localhost:80")
- .unwrap();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement