2 releases

0.1.2 Apr 3, 2022
0.1.1 Mar 31, 2022
0.1.0 Mar 31, 2022

#22 in #http-router

GPL-3.0-or-later

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

  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:

Dependencies

~0.8–1.4MB
~26K SLoC