#tide #routing #http-framework #web #http

tide-fluent-routes

Tide fluent routes is a library that implements a fluent routing API for Tide

6 releases

0.1.5 Jan 29, 2021
0.1.4 Nov 20, 2020
0.1.3 Oct 21, 2020

#531 in HTTP server

Download history 13/week @ 2023-12-04 4/week @ 2023-12-18 2/week @ 2023-12-25 9/week @ 2024-01-01 11/week @ 2024-02-26 20/week @ 2024-03-04 34/week @ 2024-03-11 13/week @ 2024-03-18

78 downloads per month

MIT/Apache

32KB
533 lines

Tide Fluent Routes   Build Status

Tide Fluent Routes is a fluent api to define routes for the Tide HTTP framework. At the moment it supports setting up paths, you can integrate middleware at any place in the route-tree and you can integrate endpoints. Some things that are possible with Tide-native routes are not (yet) possible;

  • Tide prefix routes are not implemented
  • you can not nest Tide servers

To use this you can import Tide Fluent Routes with use tide_fluent_routes::prelude::* it introduces the registerextension method on theTide::Server to register routes from a RouteBuilder. A RouteBuilder can be initialized using the route() method. You can register simple endpoints like this;

use tide_fluent_routes::prelude::*;

let mut server = tide::Server::new();

server.register(
   root()
       .get(endpoint)
       .post(endpoint));

Fluent Routes follows conventions from Tide. All HTTP verbs are supported the same way. Paths can be extended using at but this method takes a router closure that allows building the route as a tree. A complete route tree can be defined like this;


server.register(
    root()
        .get(endpoint)
        .post(endpoint)
        .at("api/v1", |route| route
            .get(endpoint)
            .post(endpoint)
        )
        .at("api/v2", |route| route
            .get(endpoint)
            .post(endpoint)
        )
);

This eliminates the need to introduce variables for partial pieces of your route tree.

Including routes defined in other functions also looks very natural, this makes it easy to compose large route trees from smaller trees defined elsewhere;


fn v1_routes(routes: RouteSegment<()>) -> RouteSegment<()> {
    routes
        .at("articles", |route| route
            .get(endpoint)
            .post(endpoint)
            .at(":id", |route| route
                .get(endpoint)
                .put(endpoint)
                .delete(endpoint)
            )
        )
}

fn v2_routes(routes: RouteSegment<()>) -> RouteSegment<()> {
    routes
        .at("articles", |route| route
            .get(endpoint))
}

server.register(
    root()
        .get(endpoint)
        .post(endpoint)
        .at("api/v1", v1_routes)
        .at("api/v2", v2_routes));

With vanilla Tide routes it can be hard to see what middleware is active for what endpoints. Adding middleware to a tree is easy, and its very clear where the middleware is applied;

server.register(
    root()
        .get(endpoint)
        .post(endpoint)
        .at("api/v1", |route| route
            .with(dummy_middleware, |route| route
                .get(endpoint)
            )
            .post(endpoint)
        )
        .at("api/v2", |route| route
            .get(endpoint)
            .get(endpoint)
        ),
);

Serving directories is possible using serve_dir, this works the same as with normal Tide routes, fluent routes adds the serve_file convenience method for serving single files.

use tide_fluent_routes::prelude::*;
use tide_fluent_routes::fs::ServeFs;

let mut server = tide::Server::new();

server.register(
    root()
        .serve_file("files/index.html")
        .at("img", |r| r
            .serve_dir("files/images")
        )
);

version: 0.1.2

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~10–23MB
~337K SLoC