mirror of
https://github.com/actix/actix-website
synced 2025-06-27 07:29:02 +02:00
update examples to v4 (#258)
This commit is contained in:
@ -5,5 +5,4 @@ edition = "2018"
|
||||
workspace = "../"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-service = "1"
|
||||
actix-web = "4"
|
||||
|
@ -17,7 +17,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.route("/index.html", web::get().to(index)),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -12,16 +12,16 @@ async fn main() -> std::io::Result<()> {
|
||||
App::new()
|
||||
.service(
|
||||
web::scope("/app1")
|
||||
.data(State1)
|
||||
.route("/", web::to(|| HttpResponse::Ok())),
|
||||
.app_data(web::Data::new(State1))
|
||||
.route("/", web::to(HttpResponse::Ok)),
|
||||
)
|
||||
.service(
|
||||
web::scope("/app2")
|
||||
.data(State2)
|
||||
.route("/", web::to(|| HttpResponse::Ok())),
|
||||
.app_data(web::Data::new(State2))
|
||||
.route("/", web::to(HttpResponse::Ok)),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -7,8 +7,8 @@ use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
fn scoped_config(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(
|
||||
web::resource("/test")
|
||||
.route(web::get().to(|| HttpResponse::Ok().body("test")))
|
||||
.route(web::head().to(|| HttpResponse::MethodNotAllowed())),
|
||||
.route(web::get().to(|| async { HttpResponse::Ok().body("test") }))
|
||||
.route(web::head().to(HttpResponse::MethodNotAllowed)),
|
||||
);
|
||||
}
|
||||
|
||||
@ -16,8 +16,8 @@ fn scoped_config(cfg: &mut web::ServiceConfig) {
|
||||
fn config(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(
|
||||
web::resource("/app")
|
||||
.route(web::get().to(|| HttpResponse::Ok().body("app")))
|
||||
.route(web::head().to(|| HttpResponse::MethodNotAllowed())),
|
||||
.route(web::get().to(|| async { HttpResponse::Ok().body("app") }))
|
||||
.route(web::head().to(HttpResponse::MethodNotAllowed)),
|
||||
);
|
||||
}
|
||||
|
||||
@ -27,9 +27,12 @@ async fn main() -> std::io::Result<()> {
|
||||
App::new()
|
||||
.configure(config)
|
||||
.service(web::scope("/api").configure(scoped_config))
|
||||
.route("/", web::get().to(|| HttpResponse::Ok().body("/")))
|
||||
.route(
|
||||
"/",
|
||||
web::get().to(|| async { HttpResponse::Ok().body("/") }),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -12,15 +12,11 @@ pub mod vh;
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.service(
|
||||
web::scope("/app1").route("/", web::to(|| HttpResponse::Ok())),
|
||||
)
|
||||
.service(
|
||||
web::scope("/app2").route("/", web::to(|| HttpResponse::Ok())),
|
||||
)
|
||||
.route("/", web::to(|| HttpResponse::Ok()))
|
||||
.service(web::scope("/app1").route("/", web::to(HttpResponse::Ok)))
|
||||
.service(web::scope("/app2").route("/", web::to(HttpResponse::Ok)))
|
||||
.route("/", web::to(HttpResponse::Ok))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.app_data(counter.clone()) // <- register the created data
|
||||
.route("/", web::get().to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -19,12 +19,12 @@ async fn index(data: web::Data<AppState>) -> String {
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.data(AppState {
|
||||
.app_data(web::Data::new(AppState {
|
||||
app_name: String::from("Actix-web"),
|
||||
})
|
||||
}))
|
||||
.service(index)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -8,16 +8,16 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(
|
||||
web::scope("/")
|
||||
.guard(guard::Header("Host", "www.rust-lang.org"))
|
||||
.route("", web::to(|| HttpResponse::Ok().body("www"))),
|
||||
.route("", web::to(|| async { HttpResponse::Ok().body("www") })),
|
||||
)
|
||||
.service(
|
||||
web::scope("/")
|
||||
.guard(guard::Header("Host", "users.rust-lang.org"))
|
||||
.route("", web::to(|| HttpResponse::Ok().body("user"))),
|
||||
.route("", web::to(|| async { HttpResponse::Ok().body("user") })),
|
||||
)
|
||||
.route("/", web::to(|| HttpResponse::Ok()))
|
||||
.route("/", web::to(HttpResponse::Ok))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
Reference in New Issue
Block a user