1
0
mirror of https://github.com/actix/actix-website synced 2024-11-23 16:31:08 +01:00

Update application examples to v2

This commit is contained in:
Yuki Okushi 2019-12-29 01:15:22 +09:00
parent f8e3430d1a
commit 67c0360864
7 changed files with 32 additions and 29 deletions

View File

@ -5,4 +5,5 @@ edition = "2018"
workspace = "../"
[dependencies]
actix-web = "1.0"
actix-web = "2.0"
actix-rt = "1.0"

View File

@ -1,12 +1,13 @@
// <setup>
use actix_web::{web, App, Responder};
fn index() -> impl Responder {
async fn index() -> impl Responder {
"Hello world!"
}
#[rustfmt::skip]
pub fn main() {
#[actix_rt::main]
async fn main() {
App::new().service(
web::scope("/app")
.route("/index.html", web::get().to(index)));

View File

@ -5,7 +5,8 @@ struct State1;
struct State2;
#[rustfmt::skip]
pub fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(
@ -17,9 +18,8 @@ pub fn main() {
.data(State2)
.route("/", web::to(|| HttpResponse::Ok())))
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </combine>

View File

@ -19,16 +19,16 @@ fn config(cfg: &mut web::ServiceConfig) {
);
}
pub fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.configure(config)
.service(web::scope("/api").configure(scoped_config))
.route("/", web::get().to(|| HttpResponse::Ok().body("/")))
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </config>

View File

@ -1,12 +1,13 @@
use actix_web::{web, App, HttpRequest, Responder};
fn show_users(_req: HttpRequest) -> impl Responder {
unimplemented!()
async fn show_users(_req: HttpRequest) -> impl Responder {
"unimplemented!"
}
#[rustfmt::skip]
// <scope>
pub fn main() {
#[actix_rt::main]
async fn main() {
App::new()
.service(
web::scope("/users")

View File

@ -7,7 +7,7 @@ struct AppState {
app_name: String,
}
fn index(data: web::Data<AppState>) -> String {
async fn index(data: web::Data<AppState>) -> String {
let app_name = &data.app_name; // <- get app_name
format!("Hello {}!", app_name) // <- response with app_name
@ -19,7 +19,7 @@ struct AppStateWithCounter {
counter: Mutex<i32>, // <- Mutex is necessary to mutate safely across threads
}
fn _index(data: web::Data<AppStateWithCounter>) -> String {
async fn _index(data: web::Data<AppStateWithCounter>) -> String {
let mut counter = data.counter.lock().unwrap(); // <- get counter's MutexGuard
*counter += 1; // <- access counter inside MutexGuard
@ -28,25 +28,26 @@ fn _index(data: web::Data<AppStateWithCounter>) -> String {
// </setup_mutable>
// <make_app_mutable>
fn _main() {
#[actix_rt::main]
async fn _main() -> std::io::Result<()> {
let counter = web::Data::new(AppStateWithCounter {
counter: Mutex::new(0),
});
HttpServer::new(move || { // move counter into the closure
App::new()
.register_data(counter.clone()) // <- register the created data
.app_data(counter.clone()) // <- register the created data
.route("/", web::get().to(_index))
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </make_app_mutable>
// <start_app>
pub fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.data(AppState {
@ -54,9 +55,8 @@ pub fn main() {
})
.route("/", web::get().to(index))
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </start_app>

View File

@ -1,7 +1,8 @@
use actix_web::{guard, web, App, HttpResponse, HttpServer};
// <vh>
pub fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(
@ -16,9 +17,8 @@ pub fn main() {
)
.route("/", web::to(|| HttpResponse::Ok()))
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </vh>