1
0
mirror of https://github.com/actix/actix-website synced 2024-11-24 00:41:07 +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 = "../" workspace = "../"
[dependencies] [dependencies]
actix-web = "1.0" actix-web = "2.0"
actix-rt = "1.0"

View File

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

View File

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

View File

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

View File

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

View File

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