2019-06-20 10:20:12 +02:00
|
|
|
pub mod handlers_arc;
|
|
|
|
// <data>
|
2019-06-28 19:31:30 +02:00
|
|
|
use actix_web::{web, Responder};
|
2019-06-15 22:37:08 +02:00
|
|
|
use std::cell::Cell;
|
|
|
|
|
2019-06-20 10:20:12 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct AppState {
|
2021-04-06 01:41:26 +02:00
|
|
|
count: Cell<usize>,
|
2019-06-20 10:20:12 +02:00
|
|
|
}
|
|
|
|
|
2019-12-28 18:08:25 +01:00
|
|
|
async fn show_count(data: web::Data<AppState>) -> impl Responder {
|
2019-06-20 10:20:12 +02:00
|
|
|
format!("count: {}", data.count.get())
|
|
|
|
}
|
2019-06-15 22:37:08 +02:00
|
|
|
|
2019-12-28 18:08:25 +01:00
|
|
|
async fn add_one(data: web::Data<AppState>) -> impl Responder {
|
2019-06-20 10:20:12 +02:00
|
|
|
let count = data.count.get();
|
|
|
|
data.count.set(count + 1);
|
2019-06-15 22:37:08 +02:00
|
|
|
|
2019-06-20 10:20:12 +02:00
|
|
|
format!("count: {}", data.count.get())
|
2019-06-15 22:37:08 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:21:54 +02:00
|
|
|
#[actix_web::main]
|
2019-12-28 18:08:25 +01:00
|
|
|
async fn main() -> std::io::Result<()> {
|
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: Cell::new(0),
|
|
|
|
};
|
|
|
|
|
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
2020-04-06 03:05:07 +02:00
|
|
|
.data(data.clone())
|
2019-06-20 10:20:12 +02:00
|
|
|
.route("/", web::to(show_count))
|
|
|
|
.route("/add", web::to(add_one))
|
|
|
|
})
|
2020-09-12 17:21:54 +02:00
|
|
|
.bind("127.0.0.1:8080")?
|
2019-06-20 10:20:12 +02:00
|
|
|
.run()
|
2019-12-28 18:08:25 +01:00
|
|
|
.await
|
2019-06-15 22:37:08 +02:00
|
|
|
}
|
2019-06-20 10:20:12 +02:00
|
|
|
// </data>
|