actix-web/src
Matt Gathu 31348a2339 Provide attribute macro for multiple HTTP methods
What
--
Define a new `route` attribute macro that supports defining multiple
HTTP methods to routed to (handled by) a single handler.

The attribute macro syntax looks like this
```rust
use actix_web::route;

async fn multi_methods() -> &'static str {
    "Hello world!\r\n"
}
```

How
--
This implementation extends the [`GuardType`][1] enum in actix-web-codegen to have a
new `GuardType::Multi` variant that denotes when multiple method guards
are used.

A new `methods` attribute in the `route` attribute macro provides a
comma-separated list of HTTP methods to provide guard for.

The code parses the methods list, matches them to the respective
`GuardType` and uses the `AnyGuard` struct to combine them together.

A constructor method for [`AnyGuard`][2] is added to support this.

The generated code looks like this:
```rust
pub struct multi_methods;
impl actix_web::dev::HttpServiceFactory for multi_methods {
    fn register(self, __config: &mut actix_web::dev::AppService) {
    ¦   async fn multi_methods() -> &'static str {
    ¦   ¦   "Hello world!\r\n"
    ¦   }
    ¦   let __resource = actix_web::Resource::new("/multi")
    ¦   ¦   .name("multi_methods")
    ¦   ¦   .guard(actix_web:💂:AnyGuard::new(<[_]>::into_vec(box [
    ¦   ¦   ¦   Box::new(actix_web:💂:Get()),
    ¦   ¦   ¦   Box::new(actix_web:💂:Post()),
    ¦   ¦   ])))
    ¦   ¦   .to(multi_methods);
    ¦   actix_web::dev::HttpServiceFactory::register(__resource, __config)
    }
}
```

**NOTE: This is my first attempt that implementing this feature.
Feedback and mentorship is highly welcome to improve it :-)**

Why
--
This fixes https://github.com/actix/actix-web/issues/1360

[1]: https://github.com/actix/actix-web/blob/master/actix-web-codegen/src/route.rs#L21
[2]: https://github.com/actix/actix-web/blob/master/src/guard.rs#L104s
2020-09-15 20:26:08 +02:00
..
middleware fix trimming to inaccessible root path (#1678) 2020-09-15 11:32:31 +01:00
types Improve json, form and query extractor config docs (#1661) 2020-09-10 15:40:20 +01:00
app.rs Check format and tweak CI config (#1619) 2020-07-22 00:28:33 +01:00
app_service.rs Check format and tweak CI config (#1619) 2020-07-22 00:28:33 +01:00
config.rs Check format and tweak CI config (#1619) 2020-07-22 00:28:33 +01:00
data.rs Check format and tweak CI config (#1619) 2020-07-22 00:28:33 +01:00
error.rs Implement `std::error::Error` for our custom errors 2020-03-18 00:22:18 -03:00
extract.rs web: Minimize `futures` dependencies 2020-05-19 08:29:12 +09:00
guard.rs Provide attribute macro for multiple HTTP methods 2020-09-15 20:26:08 +02:00
handler.rs web: Minimize `futures` dependencies 2020-05-19 08:29:12 +09:00
info.rs re-export rt in web and add main macro (#1575) 2020-06-22 20:09:48 +01:00
lib.rs prepare web release 3.0.1 (#1676) 2020-09-13 03:24:44 +01:00
request.rs fix continous growth of app data in pooled requests (#1609) 2020-07-18 16:17:00 +01:00
resource.rs Check format and tweak CI config (#1619) 2020-07-22 00:28:33 +01:00
responder.rs Check format and tweak CI config (#1619) 2020-07-22 00:28:33 +01:00
rmap.rs add method to extract matched resource name (#1577) 2020-06-27 16:22:16 +01:00
route.rs fix clippy async_yields_async lints (#1667) 2020-09-11 11:29:17 +01:00
scope.rs fix clippy async_yields_async lints (#1667) 2020-09-11 11:29:17 +01:00
server.rs bump connect and tls versions (#1655) 2020-09-09 09:20:54 +01:00
service.rs match HttpRequest app_data behavior in ServiceRequest (#1618) 2020-08-09 15:51:38 +01:00
test.rs bump actix dependency to v0.10 (#1666) 2020-09-11 12:09:52 +01:00
web.rs web: Minimize `futures` dependencies 2020-05-19 08:29:12 +09:00