1
0
mirror of https://github.com/actix/actix-website synced 2025-01-23 00:25:55 +01:00

39 lines
843 B
Rust
Raw Normal View History

2019-06-20 04:20:12 -04:00
pub mod handlers_arc;
// <data>
2022-04-07 16:22:17 +01:00
use actix_web::{web, App, HttpServer, Responder};
2019-06-15 16:37:08 -04:00
use std::cell::Cell;
2019-06-20 04:20:12 -04:00
#[derive(Clone)]
struct AppState {
count: Cell<usize>,
2019-06-20 04:20:12 -04:00
}
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.get())
}
2019-06-15 16:37:08 -04:00
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
let count = data.count.get();
data.count.set(count + 1);
2019-06-15 16:37:08 -04:00
2019-06-20 04:20:12 -04:00
format!("count: {}", data.count.get())
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: Cell::new(0),
};
HttpServer::new(move || {
App::new()
2022-02-26 03:56:24 +00:00
.app_data(web::Data::new(data.clone()))
2019-06-20 04:20:12 -04:00
.route("/", web::to(show_count))
.route("/add", web::to(add_one))
})
2022-02-26 03:56:24 +00: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
}
2019-06-20 04:20:12 -04:00
// </data>