Skip to content

Commit

Permalink
allow to specify multi pattern for resources
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Dec 25, 2019
1 parent 7882f54 commit f86ce03
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

* Allow to gracefully stop test server via `TestServer::stop()`

* Allow to specify multi-patterns for resources

## [2.0.0-rc] - 2019-12-20

Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ rustls = ["actix-tls/rustls", "awc/rustls", "rust-tls"]
actix-codec = "0.2.0"
actix-service = "1.0.1"
actix-utils = "1.0.4"
actix-router = "0.2.0"
actix-router = "0.2.1"
actix-rt = "1.0.0"
actix-server = "1.0.0"
actix-testing = "1.0.0"
Expand Down Expand Up @@ -115,4 +115,4 @@ actix-identity = { path = "actix-identity" }
actix-session = { path = "actix-session" }
actix-files = { path = "actix-files" }
actix-multipart = { path = "actix-multipart" }
awc = { path = "awc" }
awc = { path = "awc" }
2 changes: 1 addition & 1 deletion actix-cors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ where
res
}
}
.boxed_local(),
.boxed_local(),
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion actix-framed/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ where
service
})
}
.boxed_local()
.boxed_local()
}
}

Expand Down
2 changes: 1 addition & 1 deletion actix-framed/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,6 @@ where
}
Ok(())
}
.boxed_local()
.boxed_local()
}
}
2 changes: 1 addition & 1 deletion actix-identity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ where
Err(err) => Ok(req.error_response(err)),
}
}
.boxed_local()
.boxed_local()
}
}

Expand Down
2 changes: 1 addition & 1 deletion actix-session/src/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ where
}
})
}
.boxed_local()
.boxed_local()
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,13 @@ pub mod dev {
pub use actix_server::Server;
pub use actix_service::{Service, Transform};

pub(crate) fn insert_slash(path: &str) -> String {
let mut path = path.to_owned();
if !path.is_empty() && !path.starts_with('/') {
path.insert(0, '/');
};
path
pub(crate) fn insert_slash(mut patterns: Vec<String>) -> Vec<String> {
for path in &mut patterns {
if !path.is_empty() && !path.starts_with('/') {
path.insert(0, '/');
};
}
patterns
}

use crate::http::header::ContentEncoding;
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/defaultheaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ where
}
Ok(res)
}
.boxed_local()
.boxed_local()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/middleware/errhandlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ where
Ok(res)
}
}
.boxed_local()
.boxed_local()
}
}

Expand Down
28 changes: 23 additions & 5 deletions src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::rc::Rc;
use std::task::{Context, Poll};

use actix_http::{Error, Extensions, Response};
use actix_router::IntoPattern;
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
use actix_service::{
apply, apply_fn_factory, IntoServiceFactory, Service, ServiceFactory, Transform,
Expand Down Expand Up @@ -48,7 +49,7 @@ type HttpNewService = BoxServiceFactory<(), ServiceRequest, ServiceResponse, Err
/// Default behavior could be overriden with `default_resource()` method.
pub struct Resource<T = ResourceEndpoint> {
endpoint: T,
rdef: String,
rdef: Vec<String>,
name: Option<String>,
routes: Vec<Route>,
data: Option<Extensions>,
Expand All @@ -58,12 +59,12 @@ pub struct Resource<T = ResourceEndpoint> {
}

impl Resource {
pub fn new(path: &str) -> Resource {
pub fn new<T: IntoPattern>(path: T) -> Resource {
let fref = Rc::new(RefCell::new(None));

Resource {
routes: Vec::new(),
rdef: path.to_string(),
rdef: path.patterns(),
name: None,
endpoint: ResourceEndpoint::new(fref.clone()),
factory_ref: fref,
Expand Down Expand Up @@ -381,9 +382,9 @@ where
Some(std::mem::replace(&mut self.guards, Vec::new()))
};
let mut rdef = if config.is_root() || !self.rdef.is_empty() {
ResourceDef::new(&insert_slash(&self.rdef))
ResourceDef::new(insert_slash(self.rdef.clone()))
} else {
ResourceDef::new(&self.rdef)
ResourceDef::new(self.rdef.clone())
};
if let Some(ref name) = self.name {
*rdef.name_mut() = name.clone();
Expand Down Expand Up @@ -660,6 +661,23 @@ mod tests {
assert_eq!(resp.status(), StatusCode::OK);
}

#[actix_rt::test]
async fn test_pattern() {
let mut srv =
init_service(App::new().service(web::resource(["/test", "/test2"]).to(|| {
async {
Ok::<_, Error>(HttpResponse::Ok())
}
})))
.await;
let req = TestRequest::with_uri("/test").to_request();
let resp = call_service(&mut srv, req).await;
assert_eq!(resp.status(), StatusCode::OK);
let req = TestRequest::with_uri("/test2").to_request();
let resp = call_service(&mut srv, req).await;
assert_eq!(resp.status(), StatusCode::OK);
}

#[actix_rt::test]
async fn test_default_resource() {
let mut srv = init_service(
Expand Down
14 changes: 7 additions & 7 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use actix_http::{
Error, Extensions, HttpMessage, Payload, PayloadStream, RequestHead, Response,
ResponseHead,
};
use actix_router::{Path, Resource, ResourceDef, Url};
use actix_router::{IntoPattern, Path, Resource, ResourceDef, Url};
use actix_service::{IntoServiceFactory, ServiceFactory};

use crate::config::{AppConfig, AppService};
Expand Down Expand Up @@ -422,16 +422,16 @@ impl<B: MessageBody> fmt::Debug for ServiceResponse<B> {
}

pub struct WebService {
rdef: String,
rdef: Vec<String>,
name: Option<String>,
guards: Vec<Box<dyn Guard>>,
}

impl WebService {
/// Create new `WebService` instance.
pub fn new(path: &str) -> Self {
pub fn new<T: IntoPattern>(path: T) -> Self {
WebService {
rdef: path.to_string(),
rdef: path.patterns(),
name: None,
guards: Vec::new(),
}
Expand Down Expand Up @@ -491,7 +491,7 @@ impl WebService {

struct WebServiceImpl<T> {
srv: T,
rdef: String,
rdef: Vec<String>,
name: Option<String>,
guards: Vec<Box<dyn Guard>>,
}
Expand All @@ -514,9 +514,9 @@ where
};

let mut rdef = if config.is_root() || !self.rdef.is_empty() {
ResourceDef::new(&insert_slash(&self.rdef))
ResourceDef::new(insert_slash(self.rdef))
} else {
ResourceDef::new(&self.rdef)
ResourceDef::new(self.rdef)
};
if let Some(ref name) = self.name {
*rdef.name_mut() = name.clone();
Expand Down
2 changes: 1 addition & 1 deletion src/types/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ where
.map_err(|_| UrlencodedError::Parse)
}
}
.boxed_local(),
.boxed_local(),
);
self.poll(cx)
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ where
}
Ok(serde_json::from_slice::<U>(&body)?)
}
.boxed_local(),
.boxed_local(),
);

self.poll(cx)
Expand Down
4 changes: 2 additions & 2 deletions src/types/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl FromRequest for String {
.ok_or_else(|| ErrorBadRequest("Can not decode body"))?)
}
}
.boxed_local(),
.boxed_local(),
)
}
}
Expand Down Expand Up @@ -391,7 +391,7 @@ impl Future for HttpMessageBody {
}
Ok(body.freeze())
}
.boxed_local(),
.boxed_local(),
);
self.poll(cx)
}
Expand Down
5 changes: 3 additions & 2 deletions src/web.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Essentials helper functions and types for application registration.
use actix_http::http::Method;
use actix_router::IntoPattern;
use futures::Future;

pub use actix_http::Response as HttpResponse;
Expand Down Expand Up @@ -50,7 +51,7 @@ pub use crate::types::*;
/// .route(web::head().to(|| HttpResponse::MethodNotAllowed()))
/// );
/// ```
pub fn resource(path: &str) -> Resource {
pub fn resource<T: IntoPattern>(path: T) -> Resource {
Resource::new(path)
}

Expand Down Expand Up @@ -249,7 +250,7 @@ where
/// .finish(my_service)
/// );
/// ```
pub fn service(path: &str) -> WebService {
pub fn service<T: IntoPattern>(path: T) -> WebService {
WebService::new(path)
}

Expand Down

0 comments on commit f86ce03

Please sign in to comment.