From 51860a4f2ad2403d102d2bbbc8a2b185f0f73e3e Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 6 Mar 2019 15:51:56 -0800 Subject: [PATCH] update web api --- async_db/src/main.rs | 14 ++++++----- basics/src/main.rs | 55 +++++++++++++++++++++----------------------- r2d2/src/main.rs | 2 +- 3 files changed, 35 insertions(+), 36 deletions(-) diff --git a/async_db/src/main.rs b/async_db/src/main.rs index cd66d3f0..fc3d910d 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 344ff49c..ccd73b05 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 05d07849..abe564d1 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();