diff --git a/async_db/src/main.rs b/async_db/src/main.rs index cd66d3f..fc3d910 100644 --- a/async_db/src/main.rs +++ b/async_db/src/main.rs @@ -81,12 +81,14 @@ fn main() -> io::Result<()> { .state(pool.clone()) // enable logger // .middleware(middleware::Logger::default()) - .resource("/asyncio_weather", |r| { - r.route(web::get().to_async(asyncio_weather)) - }) - .resource("/parallel_weather", |r| { - r.route(web::get().to_async(parallel_weather)) - }) + .service( + web::resource("/asyncio_weather") + .route(web::get().to_async(asyncio_weather)), + ) + .service( + web::resource("/parallel_weather") + .route(web::get().to_async(parallel_weather)), + ) }) .bind("127.0.0.1:8080")? .start(); diff --git a/basics/src/main.rs b/basics/src/main.rs index 344ff49..ccd73b0 100644 --- a/basics/src/main.rs +++ b/basics/src/main.rs @@ -83,45 +83,42 @@ fn main() -> io::Result<()> { // cookie session middleware .middleware(CookieSession::signed(&[0; 32]).secure(false)) // register favicon - .resource("/favicon", |r| r.to(favicon)) + .service(web::resource("/favicon").to(favicon)) // register simple route, handle all methods - .resource("/welcome", |r| r.to(welcome)) + .service(web::resource("/welcome").to(welcome)) // with path parameters - .resource("/user/{name}", |r| r.route(web::get().to(with_param))) + .service(web::resource("/user/{name}").route(web::get().to(with_param))) // async handler - .resource("/async/{name}", |r| { - r.route(web::get().to_async(index_async)) - }) + .service( + web::resource("/async/{name}").route(web::get().to_async(index_async)), + ) // async handler - .resource("/async-body/{name}", |r| { - r.route(web::get().to(index_async_body)) - }) - .resource("/test", |r| { - r.to(|req: HttpRequest| match *req.method() { + .service( + web::resource("/async-body/{name}") + .route(web::get().to(index_async_body)), + ) + .service( + web::resource("/test").to(|req: HttpRequest| match *req.method() { Method::GET => HttpResponse::Ok(), Method::POST => HttpResponse::MethodNotAllowed(), _ => HttpResponse::NotFound(), - }) - }) - .resource("/error", |r| { - r.to(|| { - error::InternalError::new( - io::Error::new(io::ErrorKind::Other, "test"), - StatusCode::INTERNAL_SERVER_ERROR, - ) - }) - }) + }), + ) + .service(web::resource("/error").to(|| { + error::InternalError::new( + io::Error::new(io::ErrorKind::Other, "test"), + StatusCode::INTERNAL_SERVER_ERROR, + ) + })) // static files .service(fs::StaticFiles::new("/static", "static").show_files_listing()) // redirect - .resource("/", |r| { - r.route(web::get().to(|req: HttpRequest| { - println!("{:?}", req); - HttpResponse::Found() - .header(header::LOCATION, "static/welcome.html") - .finish() - })) - }) + .service(web::resource("/").route(web::get().to(|req: HttpRequest| { + println!("{:?}", req); + HttpResponse::Found() + .header(header::LOCATION, "static/welcome.html") + .finish() + }))) // default .default_resource(|r| { // 404 for GET request diff --git a/r2d2/src/main.rs b/r2d2/src/main.rs index 05d0784..abe564d 100644 --- a/r2d2/src/main.rs +++ b/r2d2/src/main.rs @@ -48,7 +48,7 @@ fn main() -> io::Result<()> { HttpServer::new(move || { App::new() .state(pool.clone()) // <- store db pool in app state - .resource("/{name}", |r| r.route(web::get().to_async(index))) + .route("/{name}", web::get().to_async(index)) }) .bind("127.0.0.1:8080")? .start();