mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-27 17:22:57 +01:00
Allow to use any service as default service
This commit is contained in:
parent
32ac159ba2
commit
5bd5651faa
@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
* Allow to use any service as default service.
|
||||||
|
|
||||||
* Remove generic type for request payload, always use default.
|
* Remove generic type for request payload, always use default.
|
||||||
|
|
||||||
* Removed `Decompress` middleware. Bytes, String, Json, Form extractors
|
* Removed `Decompress` middleware. Bytes, String, Json, Form extractors
|
||||||
|
@ -100,6 +100,7 @@ rustls = { version = "^0.15", optional = true }
|
|||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-http = { version = "0.1.0-alpha.5", features=["ssl", "brotli", "flate2-zlib"] }
|
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-http-test = { version = "0.1.0-alpha.3", features=["ssl"] }
|
||||||
|
actix-files = { version = "0.1.0-alpha.4" }
|
||||||
rand = "0.6"
|
rand = "0.6"
|
||||||
env_logger = "0.6"
|
env_logger = "0.6"
|
||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
|
@ -36,9 +36,9 @@ fn main() -> std::io::Result<()> {
|
|||||||
.wrap(
|
.wrap(
|
||||||
middleware::DefaultHeaders::new().header("X-Version-R2", "0.3"),
|
middleware::DefaultHeaders::new().header("X-Version-R2", "0.3"),
|
||||||
)
|
)
|
||||||
.default_resource(|r| {
|
.default_service(
|
||||||
r.route(web::route().to(|| HttpResponse::MethodNotAllowed()))
|
web::route().to(|| HttpResponse::MethodNotAllowed()),
|
||||||
})
|
)
|
||||||
.route(web::get().to_async(index_async)),
|
.route(web::get().to_async(index_async)),
|
||||||
)
|
)
|
||||||
.service(web::resource("/test1.html").to(|| "Test\r\n"))
|
.service(web::resource("/test1.html").to(|| "Test\r\n"))
|
||||||
|
55
src/app.rs
55
src/app.rs
@ -1,4 +1,5 @@
|
|||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use std::fmt;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
@ -207,20 +208,56 @@ where
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Default resource to be used if no matching resource could be found.
|
/// Default service to be used if no matching resource could be found.
|
||||||
pub fn default_resource<F, U>(mut self, f: F) -> Self
|
///
|
||||||
|
/// 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
|
where
|
||||||
F: FnOnce(Resource) -> Resource<U>,
|
F: IntoNewService<U>,
|
||||||
U: NewService<
|
U: NewService<
|
||||||
Request = ServiceRequest,
|
Request = ServiceRequest,
|
||||||
Response = ServiceResponse,
|
Response = ServiceResponse,
|
||||||
Error = Error,
|
Error = Error,
|
||||||
InitError = (),
|
|
||||||
> + 'static,
|
> + 'static,
|
||||||
|
U::InitError: fmt::Debug,
|
||||||
{
|
{
|
||||||
// create and configure default resource
|
// create and configure default resource
|
||||||
self.default = Some(Rc::new(boxed::new_service(
|
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
|
self
|
||||||
@ -420,10 +457,14 @@ mod tests {
|
|||||||
.service(web::resource("/test").to(|| HttpResponse::Ok()))
|
.service(web::resource("/test").to(|| HttpResponse::Ok()))
|
||||||
.service(
|
.service(
|
||||||
web::resource("/test2")
|
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())),
|
.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();
|
let req = TestRequest::with_uri("/blah").to_request();
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use std::fmt;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use actix_http::{Error, Response};
|
use actix_http::{Error, Response};
|
||||||
@ -313,22 +314,24 @@ where
|
|||||||
self.wrap(mw)
|
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
|
/// By default *405* response get returned. Resource does not use
|
||||||
/// default handler from `App` or `Scope`.
|
/// 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
|
where
|
||||||
F: FnOnce(Resource) -> R,
|
F: IntoNewService<U>,
|
||||||
R: IntoNewService<U>,
|
|
||||||
U: NewService<
|
U: NewService<
|
||||||
Request = ServiceRequest,
|
Request = ServiceRequest,
|
||||||
Response = ServiceResponse,
|
Response = ServiceResponse,
|
||||||
Error = Error,
|
Error = Error,
|
||||||
> + 'static,
|
> + 'static,
|
||||||
|
U::InitError: fmt::Debug,
|
||||||
{
|
{
|
||||||
// create and configure default resource
|
// create and configure default resource
|
||||||
self.default = Rc::new(RefCell::new(Some(Rc::new(boxed::new_service(
|
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
|
self
|
||||||
@ -626,7 +629,9 @@ mod tests {
|
|||||||
.service(
|
.service(
|
||||||
web::resource("/test").route(web::get().to(|| HttpResponse::Ok())),
|
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 req = TestRequest::with_uri("/test").to_request();
|
||||||
let resp = call_success(&mut srv, req);
|
let resp = call_success(&mut srv, req);
|
||||||
@ -642,7 +647,9 @@ mod tests {
|
|||||||
App::new().service(
|
App::new().service(
|
||||||
web::resource("/test")
|
web::resource("/test")
|
||||||
.route(web::get().to(|| HttpResponse::Ok()))
|
.route(web::get().to(|| HttpResponse::Ok()))
|
||||||
.default_resource(|r| r.to(|| HttpResponse::BadRequest())),
|
.default_service(|r: ServiceRequest| {
|
||||||
|
r.into_response(HttpResponse::BadRequest())
|
||||||
|
}),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
28
src/scope.rs
28
src/scope.rs
@ -1,4 +1,5 @@
|
|||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use std::fmt;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use actix_http::Response;
|
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.
|
/// 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
|
where
|
||||||
F: FnOnce(Resource) -> Resource<U>,
|
F: IntoNewService<U>,
|
||||||
U: NewService<
|
U: NewService<
|
||||||
Request = ServiceRequest,
|
Request = ServiceRequest,
|
||||||
Response = ServiceResponse,
|
Response = ServiceResponse,
|
||||||
Error = Error,
|
Error = Error,
|
||||||
InitError = (),
|
|
||||||
> + 'static,
|
> + 'static,
|
||||||
|
U::InitError: fmt::Debug,
|
||||||
{
|
{
|
||||||
// create and configure default resource
|
// create and configure default resource
|
||||||
self.default = Rc::new(RefCell::new(Some(Rc::new(boxed::new_service(
|
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
|
self
|
||||||
@ -843,7 +846,9 @@ mod tests {
|
|||||||
App::new().service(
|
App::new().service(
|
||||||
web::scope("/app")
|
web::scope("/app")
|
||||||
.service(web::resource("/path1").to(|| HttpResponse::Ok()))
|
.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() {
|
fn test_default_resource_propagation() {
|
||||||
let mut srv = init_service(
|
let mut srv = init_service(
|
||||||
App::new()
|
App::new()
|
||||||
.service(
|
.service(web::scope("/app1").default_service(
|
||||||
web::scope("/app1")
|
web::resource("").to(|| HttpResponse::BadRequest()),
|
||||||
.default_resource(|r| r.to(|| HttpResponse::BadRequest())),
|
))
|
||||||
)
|
|
||||||
.service(web::scope("/app2"))
|
.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();
|
let req = TestRequest::with_uri("/non-exist").to_request();
|
||||||
|
@ -63,8 +63,8 @@ impl ServiceRequest {
|
|||||||
|
|
||||||
/// Create service response
|
/// Create service response
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn into_response<B>(self, res: Response<B>) -> ServiceResponse<B> {
|
pub fn into_response<B, R: Into<Response<B>>>(self, res: R) -> ServiceResponse<B> {
|
||||||
ServiceResponse::new(self.req, res)
|
ServiceResponse::new(self.req, res.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create service response for error
|
/// Create service response for error
|
||||||
|
Loading…
Reference in New Issue
Block a user