1
0
mirror of https://github.com/actix/actix-website synced 2024-11-26 01:32:42 +01:00
actix-website/examples/request-handlers/src/handlers_arc.rs

35 lines
816 B
Rust
Raw Normal View History

2019-06-15 22:37:08 +02:00
// <arc>
use actix_web::{dev::Handler, server, App, HttpRequest, HttpResponse};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
struct MyHandler(Arc<AtomicUsize>);
impl<S> Handler<S> for MyHandler {
type Result = HttpResponse;
/// Handle request
fn handle(&self, _req: &HttpRequest<S>) -> Self::Result {
self.0.fetch_add(1, Ordering::Relaxed);
HttpResponse::Ok().into()
}
}
pub fn main() {
let sys = actix::System::new("example");
let inc = Arc::new(AtomicUsize::new(0));
server::new(move || {
let cloned = inc.clone();
App::new().resource("/", move |r| r.h(MyHandler(cloned)))
})
.bind("127.0.0.1:8088")
.unwrap()
.start();
println!("Started http server: 127.0.0.1:8088");
let _ = sys.run();
}
// </arc>