1
0
mirror of https://github.com/actix/actix-website synced 2025-02-02 12:19:04 +01:00

Update request-handlers

This commit is contained in:
Yuki Okushi 2019-12-29 02:08:25 +09:00
parent 8594212533
commit cc2a7a0645
3 changed files with 14 additions and 13 deletions

View File

@ -4,4 +4,5 @@ version = "1.0.0"
edition = "2018" edition = "2018"
[dependencies] [dependencies]
actix-web = "1.0" actix-web = "2.0"
actix-rt = "1.0"

View File

@ -8,17 +8,18 @@ struct AppState {
count: Arc<AtomicUsize>, 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)) 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); data.count.fetch_add(1, Ordering::Relaxed);
format!("count: {}", data.count.load(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}; use actix_web::{App, HttpServer};
let data = AppState { let data = AppState {
@ -31,9 +32,8 @@ pub fn main() {
.route("/", web::to(show_count)) .route("/", web::to(show_count))
.route("/add", web::to(add_one)) .route("/add", web::to(add_one))
}) })
.bind("127.0.0.1:8088") .bind("127.0.0.1:8088")?
.unwrap()
.run() .run()
.unwrap(); .await
} }
// </arc> // </arc>

View File

@ -8,18 +8,19 @@ struct AppState {
count: Cell<i32>, 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()) 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(); let count = data.count.get();
data.count.set(count + 1); data.count.set(count + 1);
format!("count: {}", data.count.get()) format!("count: {}", data.count.get())
} }
fn main() { #[actix_rt::main]
async fn main() -> std::io::Result<()> {
use actix_web::{App, HttpServer}; use actix_web::{App, HttpServer};
let data = AppState { let data = AppState {
@ -32,9 +33,8 @@ fn main() {
.route("/", web::to(show_count)) .route("/", web::to(show_count))
.route("/add", web::to(add_one)) .route("/add", web::to(add_one))
}) })
.bind("127.0.0.1:8088") .bind("127.0.0.1:8088")?
.unwrap()
.run() .run()
.unwrap(); .await
} }
// </data> // </data>