2 releases
0.1.2 | Apr 3, 2022 |
---|---|
0.1.1 | Mar 31, 2022 |
0.1.0 |
|
#14 in #http-router
38KB
972 lines
tackt
HTTP router for tower service.
See the documentation.
lib.rs
:
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:
Dependencies
~0.8–1.4MB
~27K SLoC