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§
- routes
- Build Router from several routes.
Structs§
- Func
- Wrap a function into a route.
- Method
- The Request Method (VERB)
- Mount
- Mount a Service.
- Or
- Routing branch.
- Router
- The router instance.
- Void
- Void, nothing.
Enums§
- Error
- Error returned when a route does not match.
Traits§
- Method
Req - A request that has an HTTP method.
- Param
- A param describes a route’s dependency (route’s second argument).
- PathReq
- A request that has a path.
- Remove
Prefix - A request that can remove it’s prefix.
- Route
- A route is a
Service
that has aParam
to determine wether the service should be called or not. - Service
- An asynchronous function from a
Request
to aResponse
.
Attribute Macros§
- route
macros
- The attribute to describe route’s spec.