Created
September 26, 2018 20:21
-
-
Save shakyShane/8815fc8043f394d6b33359cc7e7fdfdb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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