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

40 lines
867 B
Rust
Raw Normal View History

2019-06-15 22:37:08 +02:00
// <arc>
2019-06-28 19:31:30 +02:00
use actix_web::{web, Responder};
2019-06-15 22:37:08 +02:00
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
2019-06-20 10:20:12 +02:00
#[derive(Clone)]
struct AppState {
count: Arc<AtomicUsize>,
}
fn show_count(data: web::Data<AppState>) -> impl Responder {
format!("count: {}", data.count.load(Ordering::Relaxed))
}
2019-06-15 22:37:08 +02:00
2019-06-20 10:20:12 +02:00
fn add_one(data: web::Data<AppState>) -> impl Responder {
data.count.fetch_add(1, Ordering::Relaxed);
2019-06-15 22:37:08 +02:00
2019-06-20 10:20:12 +02:00
format!("count: {}", data.count.load(Ordering::Relaxed))
2019-06-15 22:37:08 +02:00
}
pub fn main() {
2019-06-28 19:31:30 +02:00
use actix_web::{App, HttpServer};
2019-06-20 10:20:12 +02:00
let data = AppState {
count: Arc::new(AtomicUsize::new(0)),
};
2019-06-15 22:37:08 +02:00
2019-06-20 10:20:12 +02:00
HttpServer::new(move || {
App::new()
.data(data.clone())
.route("/", web::to(show_count))
.route("/add", web::to(add_one))
2019-06-15 22:37:08 +02:00
})
.bind("127.0.0.1:8088")
.unwrap()
2019-06-20 10:20:12 +02:00
.run()
.unwrap();
2019-06-15 22:37:08 +02:00
}
// </arc>