Crate tackt

source · []
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

  1. Empty

    This spec will match exactly "/" on any methods.

    #[route]
  2. Only methods

    This spec will match exactly "/" only on GET or PUT request.

    #[route(GET, PUT)]
  3. Only segments

    This spec will match exactly "/path/to/somewhere" on any methods.

    #[route("path" / "to" / "somewhere")]
  4. Methods and segments

    This spec will match exactly "/path/to/somewhere" only on GET 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.

Enums

Error returned when a route does not match.

Traits

A request that has an HTTP method.

A param describes a route’s dependency (route’s second argument).

A request that has a path.

A request that can remove it’s prefix.

A route is a Service that has a Param to determine wether the service should be called or not.

An asynchronous function from a Request to a Response.

Attribute Macros

routemacros

The attribute to describe route’s spec.

Derive Macros