mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-05-09 08:00:41 +00:00
Custom Endpoints
This commit is contained in:
parent
002cfb1b4d
commit
5261aa9931
28 changed files with 524 additions and 46 deletions
40
src/lib.rs
40
src/lib.rs
|
@ -25,6 +25,11 @@ This crate is just as safe as you'd expect from anything written in safe Rust -
|
|||
|
||||
# Endpoints
|
||||
|
||||
There are a set of pre-defined endpoints that should cover the majority of REST APIs. However,
|
||||
it is also possible to define your own endpoints.
|
||||
|
||||
## Pre-defined Endpoints
|
||||
|
||||
Assuming you assign `/foobar` to your resource, the following pre-defined endpoints exist:
|
||||
|
||||
| Endpoint Name | Required Arguments | HTTP Verb | HTTP Path |
|
||||
|
@ -70,6 +75,41 @@ fn read(id: u64) -> Success<Foo> {
|
|||
# }
|
||||
```
|
||||
|
||||
## Custom Endpoints
|
||||
|
||||
Defining custom endpoints is done with the `#[endpoint]` macro. The syntax is similar to that
|
||||
of the pre-defined endpoints, but you need to give it more context:
|
||||
|
||||
```rust,no_run
|
||||
# #[macro_use] extern crate gotham_derive;
|
||||
# #[macro_use] extern crate gotham_restful_derive;
|
||||
# use gotham::router::builder::*;
|
||||
# use gotham_restful::*;
|
||||
# use serde::{Deserialize, Serialize};
|
||||
use gotham_restful::gotham::hyper::Method;
|
||||
|
||||
#[derive(Resource)]
|
||||
#[resource(custom_endpoint)]
|
||||
struct CustomResource;
|
||||
|
||||
/// This type is used to parse path parameters.
|
||||
#[derive(Deserialize, StateData, StaticResponseExtender)]
|
||||
# #[cfg_attr(feature = "openapi", derive(OpenapiType))]
|
||||
struct CustomPath {
|
||||
name: String
|
||||
}
|
||||
|
||||
#[endpoint(uri = "custom/:name/read", method = "Method::GET", params = false, body = false)]
|
||||
fn custom_endpoint(path: CustomPath) -> Success<String> {
|
||||
path.name.into()
|
||||
}
|
||||
# fn main() {
|
||||
# gotham::start("127.0.0.1:8080", build_simple_router(|route| {
|
||||
# route.resource::<CustomResource>("custom");
|
||||
# }));
|
||||
# }
|
||||
```
|
||||
|
||||
# Arguments
|
||||
|
||||
Some endpoints require arguments. Those should be
|
||||
|
|
|
@ -83,10 +83,14 @@ macro_rules! implOpenapiRouter {
|
|||
}
|
||||
|
||||
static URI_PLACEHOLDER_REGEX: Lazy<Regex> =
|
||||
Lazy::new(|| Regex::new(r#"(^|/):(?P<name>[^/]+)(/|$)"#).unwrap());
|
||||
Lazy::new(|| Regex::new(r#"(?P<prefix>^|/):(?P<name>[^/]+)(?P<suffix>/|$)"#).unwrap());
|
||||
let uri: &str = &E::uri();
|
||||
let uri =
|
||||
URI_PLACEHOLDER_REGEX.replace_all(uri, |captures: &Captures<'_>| format!("{{{}}}", &captures["name"]));
|
||||
let uri = URI_PLACEHOLDER_REGEX.replace_all(uri, |captures: &Captures<'_>| {
|
||||
format!(
|
||||
"{}{{{}}}{}",
|
||||
&captures["prefix"], &captures["name"], &captures["suffix"]
|
||||
)
|
||||
});
|
||||
let path = if uri.is_empty() {
|
||||
format!("{}/{}", self.0.scope.unwrap_or_default(), self.1)
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue