Skip to content

Instantly share code, notes, and snippets.

@shakyShane
Created September 26, 2018 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shakyShane/8815fc8043f394d6b33359cc7e7fdfdb to your computer and use it in GitHub Desktop.
Save shakyShane/8815fc8043f394d6b33359cc7e7fdfdb to your computer and use it in GitHub Desktop.
struct AppState {
config: Arc<Mutext<Vec<String>>>
}
///
/// these handlers can be running on any number of threads
///
fn handler(req: &HttpRequest<AppState>) -> Box<Future<Item = HttpResponse, Error = ()>> {
// clone the Arc here so it can be safely moved into the closure
let arc = req.state().config.clone();
req.payload()
.concat2()
.from_err()
.and_then(move |body| {
// now we can access the mutex here to lock it
// & write to it
let m = arc.lock().unwrap();
m.config.push(body.to_string());
// return json response
HttpResponse::Ok()
.content_type("application/json")
.body("yo!")
})
.responder()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment