mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
add resource method helpers (#2978)
This commit is contained in:
parent
67efa4a4db
commit
d7c6774ad5
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
## Unreleased - 2023-xx-xx
|
## Unreleased - 2023-xx-xx
|
||||||
|
|
||||||
|
- Add `Resource::{get, post, etc...}` methods for more concisely adding routes that don't need additional guards.
|
||||||
|
|
||||||
## 4.3.1 - 2023-02-26
|
## 4.3.1 - 2023-02-26
|
||||||
|
|
||||||
- Add support for custom methods with the `#[route]` macro. [#2969]
|
- Add support for custom methods with the `#[route]` macro. [#2969]
|
||||||
|
@ -21,7 +21,7 @@ use crate::{
|
|||||||
BoxedHttpService, BoxedHttpServiceFactory, HttpServiceFactory, ServiceRequest,
|
BoxedHttpService, BoxedHttpServiceFactory, HttpServiceFactory, ServiceRequest,
|
||||||
ServiceResponse,
|
ServiceResponse,
|
||||||
},
|
},
|
||||||
Error, FromRequest, HttpResponse, Responder,
|
web, Error, FromRequest, HttpResponse, Responder,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A collection of [`Route`]s that respond to the same path pattern.
|
/// A collection of [`Route`]s that respond to the same path pattern.
|
||||||
@ -38,11 +38,13 @@ use crate::{
|
|||||||
///
|
///
|
||||||
/// let app = App::new().service(
|
/// let app = App::new().service(
|
||||||
/// web::resource("/")
|
/// web::resource("/")
|
||||||
/// .route(web::get().to(|| HttpResponse::Ok())));
|
/// .get(|| HttpResponse::Ok())
|
||||||
|
/// .post(|| async { "Hello World!" })
|
||||||
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// If no matching route is found, [a 405 response is returned with an appropriate Allow header][RFC
|
/// If no matching route is found, an empty 405 response is returned which includes an
|
||||||
/// 9110 §15.5.6]. This default behavior can be overridden using
|
/// [appropriate Allow header][RFC 9110 §15.5.6]. This default behavior can be overridden using
|
||||||
/// [`default_service()`](Self::default_service).
|
/// [`default_service()`](Self::default_service).
|
||||||
///
|
///
|
||||||
/// [RFC 9110 §15.5.6]: https://www.rfc-editor.org/rfc/rfc9110.html#section-15.5.6
|
/// [RFC 9110 §15.5.6]: https://www.rfc-editor.org/rfc/rfc9110.html#section-15.5.6
|
||||||
@ -58,6 +60,7 @@ pub struct Resource<T = ResourceEndpoint> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Resource {
|
impl Resource {
|
||||||
|
/// Constructs new resource that matches a `path` pattern.
|
||||||
pub fn new<T: IntoPatterns>(path: T) -> Resource {
|
pub fn new<T: IntoPatterns>(path: T) -> Resource {
|
||||||
let fref = Rc::new(RefCell::new(None));
|
let fref = Rc::new(RefCell::new(None));
|
||||||
|
|
||||||
@ -368,6 +371,45 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! route_shortcut {
|
||||||
|
($method_fn:ident, $method_upper:literal) => {
|
||||||
|
#[doc = concat!(" Adds a ", $method_upper, " route.")]
|
||||||
|
///
|
||||||
|
/// Use [`route`](Self::route) if you need to add additional guards.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use actix_web::web;
|
||||||
|
/// web::resource("/")
|
||||||
|
#[doc = concat!(" .", stringify!($method_fn), "(|| async { \"Hello World!\" })")]
|
||||||
|
/// # ;
|
||||||
|
/// ```
|
||||||
|
pub fn $method_fn<F, Args>(self, handler: F) -> Self
|
||||||
|
where
|
||||||
|
F: Handler<Args>,
|
||||||
|
Args: FromRequest + 'static,
|
||||||
|
F::Output: Responder + 'static,
|
||||||
|
{
|
||||||
|
self.route(web::$method_fn().to(handler))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Concise routes for well-known HTTP methods.
|
||||||
|
impl<T> Resource<T>
|
||||||
|
where
|
||||||
|
T: ServiceFactory<ServiceRequest, Config = (), Error = Error, InitError = ()>,
|
||||||
|
{
|
||||||
|
route_shortcut!(get, "GET");
|
||||||
|
route_shortcut!(post, "POST");
|
||||||
|
route_shortcut!(put, "PUT");
|
||||||
|
route_shortcut!(patch, "PATCH");
|
||||||
|
route_shortcut!(delete, "DELETE");
|
||||||
|
route_shortcut!(head, "HEAD");
|
||||||
|
route_shortcut!(trace, "TRACE");
|
||||||
|
}
|
||||||
|
|
||||||
impl<T, B> HttpServiceFactory for Resource<T>
|
impl<T, B> HttpServiceFactory for Resource<T>
|
||||||
where
|
where
|
||||||
T: ServiceFactory<
|
T: ServiceFactory<
|
||||||
|
Loading…
Reference in New Issue
Block a user