1
0
mirror of https://github.com/actix/actix-website synced 2025-02-08 22:36:07 +01:00

40 lines
889 B
Rust
Raw Normal View History

2019-06-15 16:37:08 -04:00
// <arc>
2020-09-12 16:21:54 +01:00
use actix_web::{get, web, App, HttpServer, Responder};
2019-06-15 16:37:08 -04:00
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
2019-06-20 04:20:12 -04:00
#[derive(Clone)]
struct AppState {
count: Arc<AtomicUsize>,
}
2020-09-12 16:21:54 +01:00
#[get("/")]
2019-12-29 02:08:25 +09:00
async fn show_count(data: web::Data<AppState>) -> impl Responder {
2019-06-20 04:20:12 -04:00
format!("count: {}", data.count.load(Ordering::Relaxed))
}
2019-06-15 16:37:08 -04:00
2020-09-12 16:21:54 +01:00
#[get("/add")]
2019-12-29 02:08:25 +09:00
async fn add_one(data: web::Data<AppState>) -> impl Responder {
2019-06-20 04:20:12 -04:00
data.count.fetch_add(1, Ordering::Relaxed);
2019-06-15 16:37:08 -04:00
2019-06-20 04:20:12 -04:00
format!("count: {}", data.count.load(Ordering::Relaxed))
2019-06-15 16:37:08 -04:00
}
2020-09-12 16:21:54 +01:00
#[actix_web::main]
2019-12-29 02:08:25 +09:00
async fn main() -> std::io::Result<()> {
2019-06-20 04:20:12 -04:00
let data = AppState {
count: Arc::new(AtomicUsize::new(0)),
};
2019-06-15 16:37:08 -04:00
2019-06-20 04:20:12 -04:00
HttpServer::new(move || {
App::new()
.data(data.clone())
2020-09-12 16:21:54 +01:00
.service(show_count)
.service(add_one)
2019-06-15 16:37:08 -04:00
})
2020-09-12 16:21:54 +01:00
.bind("127.0.0.1:8080")?
2019-06-20 04:20:12 -04:00
.run()
2019-12-29 02:08:25 +09:00
.await
2019-06-15 16:37:08 -04:00
}
// </arc>