[][src]Macro jeep_train_macro::router

router!() { /* proc-macro */ }

You use this to define router. Macro will expand her input into match statement.

Example

router! {
    const ROUTER;

    scope "/" {
        get index;
    }

    scope "/hello" {
        plugin LOGGER;
        get hello;

        scope "/:world" {
            get greeting;
        }
    }
}

let's break down the example

  • const ROUTER;

const keyword must come at the top level and it cannot be used twice or more.

Ident given with the const keyword become the name of the router`

  • scope {lietral} {group}

literal is the path, and the definition of the routes is defined at the group.

Keywords

plugin

There can be multiple plugins, but it won't be inherited to child scopes. Plugin keyword must proceed with path expression that resolves to a function. Resolved function will be invoked before the controller.

Example

scope "/hello" {
    plugin PLUGIN;
    get hello;
}

In this case, function PLUGIN will be invoked before hello.

  • {http method in lower case} {ident};

Ident must be a function path. The function path will be invoked when the relevant request is received.

Example

scope "/hello" {
    get hello;
    post new_greeting;
}

Above will become

methodpathfunction
get/hellohello
post/hellonew_greeting

Keywords that defines multiple routes.

Resource

scope "/resource" {
    resource some_path_for_resource;
}
methodpathfunction
get/resource/some_path_for_resource::index
get/resource/newsome_path_for_resource::new
post/resource/some_path_for_resource::create
get/resource/showsome_path_for_resource::show
get/resource/:id/editsome_path_for_resource::edit
put/resource/:idsome_path_for_resource::update
patch/resource/:idsome_path_for_resource::update
delete/resource/some_path_for_resource::destroy

Note that /:id is a parameterized segment