diff --git a/README.md b/README.md index 9f4b07b..86a577c 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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"}} diff --git a/src/trails_handler.erl b/src/trails_handler.erl index a8b08c7..c462a65 100644 --- a/src/trails_handler.erl +++ b/src/trails_handler.erl @@ -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(). diff --git a/test/trails_SUITE.erl b/test/trails_SUITE.erl index ad0989c..f53fd9b 100644 --- a/test/trails_SUITE.erl +++ b/test/trails_SUITE.erl @@ -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()}. diff --git a/test/trails_test3_handler.erl b/test/trails_test3_handler.erl new file mode 100644 index 0000000..588200c --- /dev/null +++ b/test/trails_test3_handler.erl @@ -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]} + ].