[][src]Crate tide_fluent_routes

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;

  • middleware does not work for now, there is support missing for this in Tide
  • Tide prefix routes are not implemented
  • you can not nest Tide servers
  • serving directories is not possible but a version of this is planned

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::*;

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.

Another problem with Tide routes is that middleware that is only active for certain routes can be difficult to maintain. Adding middleware to a tree is easy, and its very clear where the middleware is applied and where not; (this is still a prototype and middleware is not actually added right now)

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)
        }),
);

Modules

prelude

Import types to use tide_fluent_routes

routebuilder

The RouteBuilder trait defines the internal dsl to build route trees as implemented by all RouteSegments

router

The router trait and its implementation on tide::Server connect the RouteBuilder to tide and allows you to call register on a tide::Server with a fluent route tree

Structs

RouteSegment

A Builder for Tide routes. RouteBuilders can be composed into a tree that represents the tree of path segments, middleware and endpoints that defines the routes in a Tide application. This tree can then be returned as a list of routes to each of the endpoints.

Functions

root

Start building a route. Returns a RouteBuilder for the root of your route