Skip to content

Commit

Permalink
Merge pull request #101 from turtleDeng/master
Browse files Browse the repository at this point in the history
trails_handler supports parameter
  • Loading branch information
elbrujohalcon authored Jul 27, 2021
2 parents 4df473f + 0dd3562 commit 0cd0385
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ Dispatch = cowboy_router:compile(Routes),
```

But now with `trails` you're able to define the routes on each resource handler.
The handler must implement the callback `trails/0` and return the specific
The handler must implement the callback `trails/0` or `trails/1` and return the specific
routes for that handler. For a better understanding, you can check out the
examples in the `test` folder ([trails_test_handler](./test/trails_test_handler.erl)).

Once you have implemented the `trails/0` callback on your handlers, you can do
Once you have implemented the `trails/0` or `trails/1` callback on your handlers, you can do
something like this:

```erlang
Expand All @@ -116,6 +116,7 @@ Handlers =
, spts_serpents_handler
, spts_single_serpent_handler
, spts_news_handler
, {support_params_handler, #{key => value}}
],
Trails =
[ {"/", cowboy_static, {file, "www/index.html"}}
Expand Down
5 changes: 4 additions & 1 deletion src/trails_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

%% @doc Returns the cowboy routes defined in the called module.
-callback trails() -> trails:trails().
-callback trails(Opts :: map()) -> trails:trails().
-optional_callbacks([trails/0, trails/1]).

-spec trails(module()) -> trails:trails().
-spec trails(module() | {module(), map()}) -> trails:trails().
trails({Module, Opts}) -> Module:trails(Opts);
trails(Module) -> Module:trails().
7 changes: 7 additions & 0 deletions test/trails_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,21 @@ basic_trails_routes(_Config) ->
, {"/api/resource1/[:id]", trails_test_handler, []}
, {"/api/:id/resource2", trails_test_handler, [arg0]}
],
ExpectedResponse4 =
[ {"/api/resource5/[:id]", trails_test3_handler, []}
, {"/api/:id/resource6", trails_test3_handler, [#{test_key => test_value}]}
] ++ ExpectedResponse3,
Handlers1 = [trails_test_handler, trails_test2_handler],
Handlers2 = [trails_test2_handler, trails_test_handler],
Handlers3 = [{trails_test3_handler, #{test_key => test_value}}],
Trails1 = StaticRoutes ++ trails:trails(Handlers1),
ExpectedResponse1 = Trails1,
Trails2 = StaticRoutes ++ trails:trails(trails_test_handler),
ExpectedResponse2 = Trails2,
Trails3 = StaticRoutes ++ trails:trails(Handlers2),
ExpectedResponse3 = Trails3,
Trails4 = trails:trails(Handlers3) ++ Trails3,
ExpectedResponse4 = Trails4,
{comment, ""}.

-spec trails_store(config()) -> {atom(), string()}.
Expand Down
12 changes: 12 additions & 0 deletions test/trails_test3_handler.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-module(trails_test3_handler).

-behaviour(trails_handler).

%% API
-export([trails/1]).

trails(Opts) ->
[
{"/api/resource5/[:id]", trails_test3_handler, []},
{"/api/:id/resource6", trails_test3_handler, [Opts]}
].

0 comments on commit 0cd0385

Please sign in to comment.