1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00

Allow to use any service as default service

This commit is contained in:
Nikolay Kim 2019-04-13 22:25:00 -07:00
parent 32ac159ba2
commit 5bd5651faa
7 changed files with 87 additions and 30 deletions

View File

@ -4,6 +4,8 @@
### Changed
* Allow to use any service as default service.
* Remove generic type for request payload, always use default.
* Removed `Decompress` middleware. Bytes, String, Json, Form extractors

View File

@ -100,6 +100,7 @@ rustls = { version = "^0.15", optional = true }
[dev-dependencies]
actix-http = { version = "0.1.0-alpha.5", features=["ssl", "brotli", "flate2-zlib"] }
actix-http-test = { version = "0.1.0-alpha.3", features=["ssl"] }
actix-files = { version = "0.1.0-alpha.4" }
rand = "0.6"
env_logger = "0.6"
serde_derive = "1.0"

View File

@ -36,9 +36,9 @@ fn main() -> std::io::Result<()> {
.wrap(
middleware::DefaultHeaders::new().header("X-Version-R2", "0.3"),
)
.default_resource(|r| {
r.route(web::route().to(|| HttpResponse::MethodNotAllowed()))
})
.default_service(
web::route().to(|| HttpResponse::MethodNotAllowed()),
)
.route(web::get().to_async(index_async)),
)
.service(web::resource("/test1.html").to(|| "Test\r\n"))

View File

@ -1,4 +1,5 @@
use std::cell::RefCell;
use std::fmt;
use std::marker::PhantomData;
use std::rc::Rc;
@ -207,20 +208,56 @@ where
self
}
/// Default resource to be used if no matching resource could be found.
pub fn default_resource<F, U>(mut self, f: F) -> Self
/// Default service to be used if no matching resource could be found.
///
/// It is possible to use services like `Resource`, `Route`.
///
/// ```rust
/// use actix_web::{web, App, HttpResponse};
///
/// fn index() -> &'static str {
/// "Welcome!"
/// }
///
/// fn main() {
/// let app = App::new()
/// .service(
/// web::resource("/index.html").route(web::get().to(index)))
/// .default_service(
/// web::route().to(|| HttpResponse::NotFound()));
/// }
/// ```
///
/// It is also possible to use static files as default service.
///
/// ```rust
/// use actix_files::Files;
/// use actix_web::{web, App, HttpResponse};
///
/// fn main() {
/// let app = App::new()
/// .service(
/// web::resource("/index.html").to(|| HttpResponse::Ok()))
/// .default_service(
/// Files::new("", "./static")
/// );
/// }
/// ```
pub fn default_service<F, U>(mut self, f: F) -> Self
where
F: FnOnce(Resource) -> Resource<U>,
F: IntoNewService<U>,
U: NewService<
Request = ServiceRequest,
Response = ServiceResponse,
Error = Error,
InitError = (),
> + 'static,
U::InitError: fmt::Debug,
{
// create and configure default resource
self.default = Some(Rc::new(boxed::new_service(
f(Resource::new("")).into_new_service().map_init_err(|_| ()),
f.into_new_service().map_init_err(|e| {
log::error!("Can not construct default service: {:?}", e)
}),
)));
self
@ -420,10 +457,14 @@ mod tests {
.service(web::resource("/test").to(|| HttpResponse::Ok()))
.service(
web::resource("/test2")
.default_resource(|r| r.to(|| HttpResponse::Created()))
.default_service(|r: ServiceRequest| {
r.into_response(HttpResponse::Created())
})
.route(web::get().to(|| HttpResponse::Ok())),
)
.default_resource(|r| r.to(|| HttpResponse::MethodNotAllowed())),
.default_service(|r: ServiceRequest| {
r.into_response(HttpResponse::MethodNotAllowed())
}),
);
let req = TestRequest::with_uri("/blah").to_request();

View File

@ -1,4 +1,5 @@
use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
use actix_http::{Error, Response};
@ -313,22 +314,24 @@ where
self.wrap(mw)
}
/// Default resource to be used if no matching route could be found.
/// Default service to be used if no matching route could be found.
/// By default *405* response get returned. Resource does not use
/// default handler from `App` or `Scope`.
pub fn default_resource<F, R, U>(mut self, f: F) -> Self
pub fn default_service<F, U>(mut self, f: F) -> Self
where
F: FnOnce(Resource) -> R,
R: IntoNewService<U>,
F: IntoNewService<U>,
U: NewService<
Request = ServiceRequest,
Response = ServiceResponse,
Error = Error,
> + 'static,
U::InitError: fmt::Debug,
{
// create and configure default resource
self.default = Rc::new(RefCell::new(Some(Rc::new(boxed::new_service(
f(Resource::new("")).into_new_service().map_init_err(|_| ()),
f.into_new_service().map_init_err(|e| {
log::error!("Can not construct default service: {:?}", e)
}),
)))));
self
@ -626,7 +629,9 @@ mod tests {
.service(
web::resource("/test").route(web::get().to(|| HttpResponse::Ok())),
)
.default_resource(|r| r.to(|| HttpResponse::BadRequest())),
.default_service(|r: ServiceRequest| {
r.into_response(HttpResponse::BadRequest())
}),
);
let req = TestRequest::with_uri("/test").to_request();
let resp = call_success(&mut srv, req);
@ -642,7 +647,9 @@ mod tests {
App::new().service(
web::resource("/test")
.route(web::get().to(|| HttpResponse::Ok()))
.default_resource(|r| r.to(|| HttpResponse::BadRequest())),
.default_service(|r: ServiceRequest| {
r.into_response(HttpResponse::BadRequest())
}),
),
);

View File

@ -1,4 +1,5 @@
use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
use actix_http::Response;
@ -180,22 +181,24 @@ where
)
}
/// Default resource to be used if no matching route could be found.
/// Default service to be used if no matching route could be found.
///
/// If default resource is not registered, app's default resource is being used.
pub fn default_resource<F, U>(mut self, f: F) -> Self
pub fn default_service<F, U>(mut self, f: F) -> Self
where
F: FnOnce(Resource) -> Resource<U>,
F: IntoNewService<U>,
U: NewService<
Request = ServiceRequest,
Response = ServiceResponse,
Error = Error,
InitError = (),
> + 'static,
U::InitError: fmt::Debug,
{
// create and configure default resource
self.default = Rc::new(RefCell::new(Some(Rc::new(boxed::new_service(
f(Resource::new("")).into_new_service().map_init_err(|_| ()),
f.into_new_service().map_init_err(|e| {
log::error!("Can not construct default service: {:?}", e)
}),
)))));
self
@ -843,7 +846,9 @@ mod tests {
App::new().service(
web::scope("/app")
.service(web::resource("/path1").to(|| HttpResponse::Ok()))
.default_resource(|r| r.to(|| HttpResponse::BadRequest())),
.default_service(|r: ServiceRequest| {
r.into_response(HttpResponse::BadRequest())
}),
),
);
@ -860,12 +865,13 @@ mod tests {
fn test_default_resource_propagation() {
let mut srv = init_service(
App::new()
.service(
web::scope("/app1")
.default_resource(|r| r.to(|| HttpResponse::BadRequest())),
)
.service(web::scope("/app1").default_service(
web::resource("").to(|| HttpResponse::BadRequest()),
))
.service(web::scope("/app2"))
.default_resource(|r| r.to(|| HttpResponse::MethodNotAllowed())),
.default_service(|r: ServiceRequest| {
r.into_response(HttpResponse::MethodNotAllowed())
}),
);
let req = TestRequest::with_uri("/non-exist").to_request();

View File

@ -63,8 +63,8 @@ impl ServiceRequest {
/// Create service response
#[inline]
pub fn into_response<B>(self, res: Response<B>) -> ServiceResponse<B> {
ServiceResponse::new(self.req, res)
pub fn into_response<B, R: Into<Response<B>>>(self, res: R) -> ServiceResponse<B> {
ServiceResponse::new(self.req, res.into())
}
/// Create service response for error