mirror of
https://github.com/actix/actix-website
synced 2025-01-22 16:15:56 +01:00
Update request-handlers
This commit is contained in:
parent
8594212533
commit
cc2a7a0645
@ -4,4 +4,5 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "1.0"
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
|
@ -8,17 +8,18 @@ struct AppState {
|
||||
count: Arc<AtomicUsize>,
|
||||
}
|
||||
|
||||
fn show_count(data: web::Data<AppState>) -> impl Responder {
|
||||
async fn show_count(data: web::Data<AppState>) -> impl Responder {
|
||||
format!("count: {}", data.count.load(Ordering::Relaxed))
|
||||
}
|
||||
|
||||
fn add_one(data: web::Data<AppState>) -> impl Responder {
|
||||
async fn add_one(data: web::Data<AppState>) -> impl Responder {
|
||||
data.count.fetch_add(1, Ordering::Relaxed);
|
||||
|
||||
format!("count: {}", data.count.load(Ordering::Relaxed))
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
let data = AppState {
|
||||
@ -31,9 +32,8 @@ pub fn main() {
|
||||
.route("/", web::to(show_count))
|
||||
.route("/add", web::to(add_one))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
// </arc>
|
||||
|
@ -8,18 +8,19 @@ struct AppState {
|
||||
count: Cell<i32>,
|
||||
}
|
||||
|
||||
fn show_count(data: web::Data<AppState>) -> impl Responder {
|
||||
async fn show_count(data: web::Data<AppState>) -> impl Responder {
|
||||
format!("count: {}", data.count.get())
|
||||
}
|
||||
|
||||
fn add_one(data: web::Data<AppState>) -> impl Responder {
|
||||
async fn add_one(data: web::Data<AppState>) -> impl Responder {
|
||||
let count = data.count.get();
|
||||
data.count.set(count + 1);
|
||||
|
||||
format!("count: {}", data.count.get())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
let data = AppState {
|
||||
@ -32,9 +33,8 @@ fn main() {
|
||||
.route("/", web::to(show_count))
|
||||
.route("/add", web::to(add_one))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
// </data>
|
||||
|
Loading…
x
Reference in New Issue
Block a user