Expand description
§tackt
HTTP router for tower service.
§usage overview
use tackt::route;
use tackt::routes;
#[route(GET, PUT: "entity" / id / "resource" / path*)]
async fn resource(
req: http::Request<hyper::Body>,
id: i32,
path: String,
) -> Result<http::Response<hyper::Body>, Box<dyn std::error::Error>> {
let content = format!("resource: {id} {path}");
let body = hyper::Body::from(content);
let response = http::Response::new(body);
Ok(response)
}
let router = routes![resource];
// use the `router` in `hyper::service::make_service_fn`.
NOTE: #[route]
attribute changes the function signature.
§route spec examples
-
Empty
This spec will match exactly
"/"
on any methods.ⓘ#[route]
-
Only methods
This spec will match exactly
"/"
only onGET
orPUT
request.ⓘ#[route(GET, PUT)]
-
Only segments
This spec will match exactly
"/path/to/somewhere"
on any methods.ⓘ#[route("path" / "to" / "somewhere")]
-
Methods and segments
This spec will match exactly
"/path/to/somewhere"
only onGET
request.ⓘ#[route(GET: "path" / "to" / "somewhere")]
§route syntax:
spec: methods ':' segments
/ methods
/ segments
/ empty
methods: identifier [',' identifier]*
segments: segment ['/' segment]* ['/' rest]
segment: literal-str / identifier
rest: identifier '*'
empty:
Macros§
- Build Router from several routes.
Structs§
- Wrap a function into a route.
- The Request Method (VERB)
- Mount a Service.
- Routing branch.
- The router instance.
- Void, nothing.
Enums§
- Error returned when a route does not match.
Traits§
- A request that has an HTTP method.
- A request that has a path.
- A request that can remove it’s prefix.
- An asynchronous function from a
Request
to aResponse
.
Attribute Macros§
- route
macros
The attribute to describe route’s spec.