docs(examples): update state example to async/await
This commit is contained in:
committed by
Sean McArthur
parent
af78fd3672
commit
22bd31c68f
56
examples/state.rs
Normal file
56
examples/state.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
#![feature(async_await)]
|
||||
#![deny(warnings)]
|
||||
|
||||
use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
|
||||
|
||||
use hyper::{Body, Error, Response, Server};
|
||||
use hyper::service::{make_service_fn, service_fn};
|
||||
|
||||
#[hyper::rt::main]
|
||||
async fn main() {
|
||||
pretty_env_logger::init();
|
||||
|
||||
let addr = ([127, 0, 0, 1], 3000).into();
|
||||
|
||||
// For the most basic of state, we just share a counter, that increments
|
||||
// with each request, and we send its value back in the response.
|
||||
let counter = Arc::new(AtomicUsize::new(0));
|
||||
|
||||
// The closure inside `make_service_fn` is run for each connection,
|
||||
// creating a 'service' to handle requests for that specific connection.
|
||||
let make_service = make_service_fn(move |_| {
|
||||
// While the state was moved into the make_service closure,
|
||||
// we need to clone it here because this closure is called
|
||||
// once for every connection.
|
||||
//
|
||||
// Each connection could send multiple requests, so
|
||||
// the `Service` needs a clone to handle later requests.
|
||||
let counter = counter.clone();
|
||||
|
||||
async move {
|
||||
// This is the `Service` that will handle the connection.
|
||||
// `service_fn` is a helper to convert a function that
|
||||
// returns a Response into a `Service`.
|
||||
Ok::<_, Error>(service_fn(move |_req| {
|
||||
// Get the current count, and also increment by 1, in a single
|
||||
// atomic operation.
|
||||
let count = counter.fetch_add(1, Ordering::AcqRel);
|
||||
async move {
|
||||
Ok::<_, Error>(
|
||||
Response::new(Body::from(format!("Request #{}", count)))
|
||||
)
|
||||
}
|
||||
}))
|
||||
}
|
||||
});
|
||||
|
||||
let server = Server::bind(&addr)
|
||||
.serve(make_service);
|
||||
|
||||
println!("Listening on http://{}", addr);
|
||||
|
||||
if let Err(e) = server.await {
|
||||
eprintln!("server error: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
#![deny(warnings)]
|
||||
extern crate hyper;
|
||||
extern crate pretty_env_logger;
|
||||
extern crate serde_json;
|
||||
|
||||
use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
|
||||
|
||||
use hyper::{Body, Response, Server};
|
||||
use hyper::service::service_fn_ok;
|
||||
use hyper::rt::{self, Future};
|
||||
|
||||
fn main() {
|
||||
pretty_env_logger::init();
|
||||
|
||||
let addr = ([127, 0, 0, 1], 3000).into();
|
||||
|
||||
// For the most basic of state, we just share a counter, that increments
|
||||
// with each request, and we send its value back in the response.
|
||||
let counter = Arc::new(AtomicUsize::new(0));
|
||||
|
||||
// new_service is run for each connection, creating a 'service'
|
||||
// to handle requests for that specific connection.
|
||||
let new_service = move || {
|
||||
// While the state was moved into the new_service closure,
|
||||
// we need to clone it here because this closure is called
|
||||
// once for every connection.
|
||||
//
|
||||
// Each connection could send multiple requests, so
|
||||
// the `Service` needs a clone to handle later requests.
|
||||
let counter = counter.clone();
|
||||
|
||||
// This is the `Service` that will handle the connection.
|
||||
// `service_fn_ok` is a helper to convert a function that
|
||||
// returns a Response into a `Service`.
|
||||
//
|
||||
// If you wanted to return a `Future` of a `Response`, such as because
|
||||
// you wish to load data from a database or do other things, you
|
||||
// could change this to `service_fn` instead.
|
||||
service_fn_ok(move |_req| {
|
||||
// Get the current count, and also increment by 1, in a single
|
||||
// atomic operation.
|
||||
let count = counter.fetch_add(1, Ordering::AcqRel);
|
||||
Response::new(Body::from(format!("Request #{}", count)))
|
||||
})
|
||||
};
|
||||
|
||||
let server = Server::bind(&addr)
|
||||
.serve(new_service)
|
||||
.map_err(|e| eprintln!("server error: {}", e));
|
||||
|
||||
println!("Listening on http://{}", addr);
|
||||
|
||||
rt::run(server);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user