From 0c268d18c1b2081407e647a28b5f966c96ef50bc Mon Sep 17 00:00:00 2001 From: Cameron Dershem Date: Mon, 17 Jun 2019 02:08:42 -0400 Subject: [PATCH] Quick pass at url-dispatch --- examples/url-dispatch/src/cfg.rs | 21 ++++++++++----------- examples/url-dispatch/src/dhandler.rs | 21 ++++++++++++--------- examples/url-dispatch/src/main.rs | 5 ++--- examples/url-dispatch/src/minfo.rs | 12 +++++------- examples/url-dispatch/src/norm.rs | 11 +++++------ examples/url-dispatch/src/norm2.rs | 12 ++++++------ examples/url-dispatch/src/path.rs | 8 ++++---- examples/url-dispatch/src/path2.rs | 6 +++--- examples/url-dispatch/src/pbuf.rs | 10 ++++------ examples/url-dispatch/src/pred.rs | 17 +++++++++-------- examples/url-dispatch/src/pred2.rs | 15 +++++++-------- examples/url-dispatch/src/resource.rs | 13 ++++++------- examples/url-dispatch/src/scope.rs | 7 ++++--- examples/url-dispatch/src/url_ext.rs | 11 +++++------ examples/url-dispatch/src/urls.rs | 19 +++++++++++-------- 15 files changed, 93 insertions(+), 95 deletions(-) diff --git a/examples/url-dispatch/src/cfg.rs b/examples/url-dispatch/src/cfg.rs index 80f63d8..23c9490 100644 --- a/examples/url-dispatch/src/cfg.rs +++ b/examples/url-dispatch/src/cfg.rs @@ -1,15 +1,14 @@ // -use actix_web::{pred, App, HttpResponse}; +use actix_web::{guard, web, App, HttpResponse}; -fn main() { - App::new() - .resource("/path", |resource| { - resource - .route() - .filter(pred::Get()) - .filter(pred::Header("content-type", "text/plain")) - .f(|req| HttpResponse::Ok()) - }) - .finish(); +pub fn main() { + App::new().service( + web::resource("/").route( + web::route() + .guard(guard::Get()) + .guard(guard::Header("content-type", "text/plain")) + .to(|| HttpResponse::Ok()), + ), + ); } // diff --git a/examples/url-dispatch/src/dhandler.rs b/examples/url-dispatch/src/dhandler.rs index 7678d9b..46a9a75 100644 --- a/examples/url-dispatch/src/dhandler.rs +++ b/examples/url-dispatch/src/dhandler.rs @@ -1,14 +1,17 @@ -// -use actix_web::{http::Method, pred, App, HttpResponse}; +use actix_web::{guard, web, App, HttpRequest, HttpResponse, Responder}; +fn index(_req: HttpRequest) -> impl Responder { + "Welcome!" +} + +// fn main() { App::new() - .default_resource(|r| { - r.method(Method::GET).f(|req| HttpResponse::NotFound()); - r.route() - .filter(pred::Not(pred::Get())) - .f(|req| HttpResponse::MethodNotAllowed()); - }) - .finish(); + .service(web::resource("/").route(web::get().to(index))) + .default_service( + web::route() + .guard(guard::Not(guard::Get())) + .to(|| HttpResponse::MethodNotAllowed()), + ); } // diff --git a/examples/url-dispatch/src/main.rs b/examples/url-dispatch/src/main.rs index f6d5391..b1c30a2 100644 --- a/examples/url-dispatch/src/main.rs +++ b/examples/url-dispatch/src/main.rs @@ -16,14 +16,13 @@ mod urls; //
use actix_web::{web, App, HttpRequest, HttpResponse}; -fn index(req: HttpRequest) -> HttpResponse { +fn index(_req: HttpRequest) -> HttpResponse { unimplemented!() } fn main() { App::new() .route("/user/{name}", web::get().to(index)) - .route("/user/{name}", web::get().to(index)) - .finish(); + .route("/user/{name}", web::get().to(index)); } //
diff --git a/examples/url-dispatch/src/minfo.rs b/examples/url-dispatch/src/minfo.rs index 2c05a57..f26ac0a 100644 --- a/examples/url-dispatch/src/minfo.rs +++ b/examples/url-dispatch/src/minfo.rs @@ -1,15 +1,13 @@ // -use actix_web::{App, HttpRequest, Result}; +use actix_web::{web, App, HttpRequest, Result}; -fn index(req: &HttpRequest) -> Result { - let v1: u8 = req.match_info().query("v1")?; - let v2: u8 = req.match_info().query("v2")?; +fn index(req: HttpRequest) -> Result { + let v1: u8 = req.match_info().query("v1").parse().unwrap(); + let v2: u8 = req.match_info().query("v2").parse().unwrap(); Ok(format!("Values {} {}", v1, v2)) } fn main() { - App::new() - .resource(r"/a/{v1}/{v2}/", |r| r.f(index)) - .finish(); + App::new().route(r"/a/{v1}/{v2}/", web::get().to(index)); } // diff --git a/examples/url-dispatch/src/norm.rs b/examples/url-dispatch/src/norm.rs index fcdbd50..a1c8d4a 100644 --- a/examples/url-dispatch/src/norm.rs +++ b/examples/url-dispatch/src/norm.rs @@ -1,15 +1,14 @@ // -use actix_web::{http::NormalizePath, App}; +use actix_web::{middleware, web, App, HttpResponse}; fn main() { - let app = App::new() - .resource("/resource/", |r| r.f(index)) - .default_resource(|r| r.h(NormalizePath::default())) - .finish(); + App::new() + .wrap(middleware::NormalizePath) + .route("/", web::get().to(|| HttpResponse::Ok())); } // use actix_web::HttpRequest; -fn index(req: &HttpRequest) -> String { +fn index(_req: HttpRequest) -> String { unimplemented!() } diff --git a/examples/url-dispatch/src/norm2.rs b/examples/url-dispatch/src/norm2.rs index 7a4fb91..cadfd35 100644 --- a/examples/url-dispatch/src/norm2.rs +++ b/examples/url-dispatch/src/norm2.rs @@ -1,16 +1,16 @@ // -use actix_web::{http::Method, http::NormalizePath, App}; +use actix_web::{http::Method, middleware, web, App}; fn main() { - let app = App::new() - .resource("/resource/", |r| r.f(index)) - .default_resource(|r| r.method(Method::GET).h(NormalizePath::default())) - .finish(); + App::new() + .wrap(middleware::NormalizePath) + .route("/resource/", web::get().to(index)) + .default_service(web::route().method(Method::GET)); } // use actix_web::HttpRequest; -fn index(req: &HttpRequest) -> String { +fn index(_req: HttpRequest) -> String { unimplemented!() } diff --git a/examples/url-dispatch/src/path.rs b/examples/url-dispatch/src/path.rs index 7724c1e..bc726c4 100644 --- a/examples/url-dispatch/src/path.rs +++ b/examples/url-dispatch/src/path.rs @@ -1,15 +1,15 @@ // -use actix_web::{http::Method, App, Path, Result}; +use actix_web::{web, App, Result}; // extract path info using serde -fn index(info: Path<(String, u32)>) -> Result { +fn index(info: web::Path<(String, u32)>) -> Result { Ok(format!("Welcome {}! id: {}", info.0, info.1)) } fn main() { - let app = App::new().resource( + App::new().route( "/{username}/{id}/index.html", // <- define path parameters - |r| r.method(Method::GET).with(index), + web::get().to(index), ); } // diff --git a/examples/url-dispatch/src/path2.rs b/examples/url-dispatch/src/path2.rs index b433e1f..2524342 100644 --- a/examples/url-dispatch/src/path2.rs +++ b/examples/url-dispatch/src/path2.rs @@ -1,5 +1,5 @@ // -use actix_web::{http::Method, web, App, Result}; +use actix_web::{web, App, Result}; use serde::Deserialize; #[derive(Deserialize)] @@ -13,9 +13,9 @@ fn index(info: web::Path) -> Result { } fn main() { - let app = App::new().resource( + App::new().route( "/{username}/index.html", // <- define path parameters - |r| r.method(Method::GET).with(index), + web::get().to(index), ); } // diff --git a/examples/url-dispatch/src/pbuf.rs b/examples/url-dispatch/src/pbuf.rs index b18ae35..855d2e9 100644 --- a/examples/url-dispatch/src/pbuf.rs +++ b/examples/url-dispatch/src/pbuf.rs @@ -1,15 +1,13 @@ // -use actix_web::{http::Method, App, HttpRequest, Result}; +use actix_web::{web, App, HttpRequest, Result}; use std::path::PathBuf; -fn index(req: &HttpRequest) -> Result { - let path: PathBuf = req.match_info().query("tail")?; +fn index(req: HttpRequest) -> Result { + let path: PathBuf = req.match_info().query("tail").parse().unwrap(); Ok(format!("Path {:?}", path)) } fn main() { - App::new() - .resource(r"/a/{tail:.*}", |r| r.method(Method::GET).f(index)) - .finish(); + App::new().route(r"/a/{tail:.*}", web::get().to(index)); } // diff --git a/examples/url-dispatch/src/pred.rs b/examples/url-dispatch/src/pred.rs index 2fda5a5..8c75a2c 100644 --- a/examples/url-dispatch/src/pred.rs +++ b/examples/url-dispatch/src/pred.rs @@ -1,19 +1,20 @@ // -use actix_web::{http, server::Request, pred::Predicate, App, HttpResponse}; +use actix_web::{dev::RequestHead, guard::Guard, http, web, App, HttpResponse}; struct ContentTypeHeader; -impl Predicate for ContentTypeHeader { - fn check(&self, req: &Request, state: &S) -> bool { +impl Guard for ContentTypeHeader { + fn check(&self, req: &RequestHead) -> bool { req.headers().contains_key(http::header::CONTENT_TYPE) } } fn main() { - App::new().resource("/index.html", |r| { - r.route() - .filter(ContentTypeHeader) - .f(|_| HttpResponse::Ok()) - }); + App::new().route( + "", + web::route() + .guard(ContentTypeHeader) + .to(|| HttpResponse::Ok()), + ); } // diff --git a/examples/url-dispatch/src/pred2.rs b/examples/url-dispatch/src/pred2.rs index 6a9449a..e56d6c2 100644 --- a/examples/url-dispatch/src/pred2.rs +++ b/examples/url-dispatch/src/pred2.rs @@ -1,13 +1,12 @@ // -use actix_web::{pred, App, HttpResponse}; +use actix_web::{guard, web, App, HttpResponse}; fn main() { - App::new() - .resource("/index.html", |r| { - r.route() - .filter(pred::Not(pred::Get())) - .f(|req| HttpResponse::MethodNotAllowed()) - }) - .finish(); + App::new().route( + "/", + web::route() + .guard(guard::Not(guard::Get())) + .to(|| HttpResponse::MethodNotAllowed()), + ); } // diff --git a/examples/url-dispatch/src/resource.rs b/examples/url-dispatch/src/resource.rs index aebfebd..1dd5639 100644 --- a/examples/url-dispatch/src/resource.rs +++ b/examples/url-dispatch/src/resource.rs @@ -1,16 +1,15 @@ // -use actix_web::{http::Method, App, HttpRequest, HttpResponse}; +use actix_web::{http::Method, web, App, HttpRequest, HttpResponse}; -fn index(req: &HttpRequest) -> HttpResponse { +fn index(_req: HttpRequest) -> HttpResponse { unimplemented!() } fn main() { App::new() - .resource("/prefix", |r| r.f(index)) - .resource("/user/{name}", |r| { - r.method(Method::GET).f(|req| HttpResponse::Ok()) - }) - .finish(); + .service(web::resource("/prefix").route(web::get().to(index))) + .service( + web::resource("/user/{name}").route(web::get().to(|| HttpResponse::Ok())), + ); } // diff --git a/examples/url-dispatch/src/scope.rs b/examples/url-dispatch/src/scope.rs index 6180b07..5bd4906 100644 --- a/examples/url-dispatch/src/scope.rs +++ b/examples/url-dispatch/src/scope.rs @@ -1,4 +1,4 @@ -use actix_web::{App, HttpRequest, HttpResponse}; +use actix_web::{web, App, HttpRequest, HttpResponse}; // fn show_users(_req: HttpRequest) -> HttpResponse { @@ -7,7 +7,8 @@ fn show_users(_req: HttpRequest) -> HttpResponse { #[rustfmt::skip] fn main() { - App::new().service( - web::scope("/users") .route("/show", web::to(show_users))) + App::new() + .service(web::scope("/users") + .route("/show", web::get().to(show_users))); } // diff --git a/examples/url-dispatch/src/url_ext.rs b/examples/url-dispatch/src/url_ext.rs index f02f14f..6327192 100644 --- a/examples/url-dispatch/src/url_ext.rs +++ b/examples/url-dispatch/src/url_ext.rs @@ -1,16 +1,15 @@ // -use actix_web::{App, Error, HttpRequest, HttpResponse}; +use actix_web::{web, App, Error, HttpRequest, HttpResponse}; -fn index(req: &HttpRequest) -> Result { +fn index(req: HttpRequest) -> Result { let url = req.url_for("youtube", &["oHg5SJYRHA0"])?; assert_eq!(url.as_str(), "https://youtube.com/watch/oHg5SJYRHA0"); Ok(HttpResponse::Ok().into()) } fn main() { - let app = App::new() - .resource("/index.html", |r| r.f(index)) - .external_resource("youtube", "https://youtube.com/watch/{video_id}") - .finish(); + App::new() + .service(web::resource("/index.html").route(web::get().to(index))) + .external_resource("youtube", "https://youtube.com/watch/{video_id}"); } // diff --git a/examples/url-dispatch/src/urls.rs b/examples/url-dispatch/src/urls.rs index 6e67a3a..814cc9f 100644 --- a/examples/url-dispatch/src/urls.rs +++ b/examples/url-dispatch/src/urls.rs @@ -1,5 +1,7 @@ // -use actix_web::{http::header, http::Method, App, HttpRequest, HttpResponse, Result}; +use actix_web::{ + guard, http::header, http::Method, web, App, HttpRequest, HttpResponse, Result, +}; fn index(req: HttpRequest) -> Result { let url = req.url_for("foo", &["1", "2", "3"])?; // <- generate url for "foo" resource @@ -9,12 +11,13 @@ fn index(req: HttpRequest) -> Result { } fn main() { - let app = App::new() - .resource("/test/{a}/{b}/{c}", |r| { - r.name("foo"); // <- set resource name, then it could be used in `url_for` - r.method(Method::GET).f(|_| HttpResponse::Ok()); - }) - .route("/test/", Method::GET, index) - .finish(); + App::new() + .service( + web::resource("/test/{a}/{b}/{c}") + .name("foo") // <- set resource name, then it could be used in `url_for` + .guard(guard::Get()) + .to(|| HttpResponse::Ok()), + ) + .route("/test/", web::get().to(index)); } //