mirror of
https://github.com/actix/actix-website
synced 2025-06-27 15:39:02 +02:00
v3 (#188)
This commit is contained in:
@ -4,5 +4,4 @@ version = "1.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
actix-web = "3"
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <arc>
|
||||
use actix_web::{web, Responder};
|
||||
use actix_web::{get, web, App, HttpServer, Responder};
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
@ -8,20 +8,20 @@ struct AppState {
|
||||
count: Arc<AtomicUsize>,
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
async fn show_count(data: web::Data<AppState>) -> impl Responder {
|
||||
format!("count: {}", data.count.load(Ordering::Relaxed))
|
||||
}
|
||||
|
||||
#[get("/add")]
|
||||
async fn add_one(data: web::Data<AppState>) -> impl Responder {
|
||||
data.count.fetch_add(1, Ordering::Relaxed);
|
||||
|
||||
format!("count: {}", data.count.load(Ordering::Relaxed))
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
let data = AppState {
|
||||
count: Arc::new(AtomicUsize::new(0)),
|
||||
};
|
||||
@ -29,10 +29,10 @@ async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.data(data.clone())
|
||||
.route("/", web::to(show_count))
|
||||
.route("/add", web::to(add_one))
|
||||
.service(show_count)
|
||||
.service(add_one)
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ async fn add_one(data: web::Data<AppState>) -> impl Responder {
|
||||
format!("count: {}", data.count.get())
|
||||
}
|
||||
|
||||
#[actix_rt::main]
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
@ -33,7 +33,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.route("/", web::to(show_count))
|
||||
.route("/add", web::to(add_one))
|
||||
})
|
||||
.bind("127.0.0.1:8088")?
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
Reference in New Issue
Block a user