Skip to content

Commit bbdf5e5

Browse files
authored
Merge pull request #247 from permaweb/feat/pattern_matching_non_exempt
feat: switch from exempt routes to relevant routes approach
2 parents dbe6071 + b3a1108 commit bbdf5e5

File tree

1 file changed

+19
-42
lines changed

1 file changed

+19
-42
lines changed

src/dev_router.erl

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,13 @@
2424
%%% map or a path regex.
2525
%%% </pre>
2626
-module(dev_router).
27-
%%% Device API:
2827
-export([routes/3, route/2, route/3, preprocess/3]).
29-
%%% Public utilities:
30-
-export([match/3]).
28+
-export([match/3, is_relevant/3]).
3129
-include_lib("eunit/include/eunit.hrl").
3230
-include("include/hb.hrl").
3331

34-
-define(DEFAULT_EXEMPT_ROUTES, [
35-
#{ <<"template">> => <<"/~meta@1.0/.*">> },
36-
#{ <<"template">> => <<"/~greenzone@1.0/.*">> },
37-
#{ <<"template">> => <<"/.*~node-process@1.0/.*">> },
38-
#{ <<"template">> => <<"/~snp@1.0/.*">> },
39-
#{ <<"template">> => <<"/~p4@1.0/.*">> }
32+
-define(DEFAULT_RELAVANT_ROUTES, [
33+
#{ <<"template">> => <<"/.*~process@1.0/.*">> }
4034
]).
4135

4236
%% @doc Device function that returns all known routes.
@@ -179,7 +173,7 @@ extract_base(RawPath, Opts) when is_binary(RawPath) ->
179173
case ?IS_ID(BasePath) of
180174
true -> BasePath;
181175
false ->
182-
case binary:split(BasePath, [<<"~">>, <<"?">>, <<"&">>], [global]) of
176+
case binary:split(BasePath, [<<"\~">>, <<"?">>, <<"&">>], [global]) of
183177
[BaseMsgID|_] when ?IS_ID(BaseMsgID) -> BaseMsgID;
184178
_ -> hb_crypto:sha256(BasePath)
185179
end
@@ -370,54 +364,34 @@ binary_to_bignum(Bin) when ?IS_ID(Bin) ->
370364
<< Num:256/unsigned-integer >> = hb_util:native_id(Bin),
371365
Num.
372366

373-
%% @doc is_exempt looks at the is_exempt paths opt and if any incoming message path matches it will
374-
%% make the request exempt from preprocessing.
375-
is_exempt(Msg1, Msg2, Opts) ->
376-
ExemptRoutes =
367+
%% @doc is_relevant looks at the relevant_routes paths opt and if any incoming message path matches it will
368+
%% make the request relevant for preprocessing.
369+
is_relevant(Msg1, Msg2, Opts) ->
370+
RelevantRoutes =
377371
hb_opts:get(
378-
exempt_routes,
379-
?DEFAULT_EXEMPT_ROUTES,
372+
relevant_routes,
373+
?DEFAULT_RELAVANT_ROUTES,
380374
Opts
381375
),
382376
Req = hb_ao:get(<<"request">>, Msg2, Opts),
383-
{_, Matches} = match(#{ <<"routes">> => ExemptRoutes}, Req, Opts),
377+
{_, Matches} = match(#{ <<"routes">> => RelevantRoutes}, Req, Opts),
384378
?event(debug_preprocess, { matches, Matches }),
385379
case Matches of
386380
no_matching_route ->
387-
% case hb_ao:resolve(#{
388-
% <<"device">> => <<"router@1.0">>,
389-
% <<"routes">> => hb_ao:get(exempt_routes, Msg1, Opts)},
390-
% Msg2#{ <<"path">> => <<"match">> },
391-
% Opts
392-
% ) of
393-
% {error, no_matching_route} -> {ok, false};
394-
% _ -> {ok, false}
395-
% end;
396381
{ok, false};
397-
IsExempt ->
398-
?event(debug_preprocess, { is_except, IsExempt }),
382+
IsRelevant ->
383+
?event(debug_preprocess, { is_relevant, IsRelevant }),
399384
{ok, true}
400-
end.
385+
end.
401386

402387
%% @doc Preprocess a request to check if it should be relayed to a different node.
403388
preprocess(Msg1, Msg2, Opts) ->
404389
?event(debug_preprocess, called_preprocess),
405-
case is_exempt(Msg1, Msg2, Opts) of
390+
case is_relevant(Msg1, Msg2, Opts) of
406391
{ok, true} ->
407-
?event(debug_preprocess, is_exempt_true),
408-
{ok, hb_ao:get(<<"body">>, Msg2, Opts#{ hashpath => ignore })};
409-
% Request should not be proxied, return the modified parsed list of messages to execute
410-
% {ok, hb_ao:resolve(Msg1, Msg2, Opts)};
411-
{ok, false} ->
412-
?event(debug_dynrouter, { msg1, Msg1 }),
413-
?event(debug_dynrouter, { opts, Opts }),
414392
{ok, TemplateRoutes} = routes(Msg1, Msg2, Opts),
415-
?event(debug_dynrouter, { template_routes, TemplateRoutes }),
416393
Req = hb_ao:get(<<"request">>, Msg2, Opts),
417-
Path = find_target_path(Req, Opts),
418-
?event(debug_dynrouter, { found_path, Path }),
419394
{_, Match} = match(#{ <<"routes">> => TemplateRoutes}, Req, Opts),
420-
?event(debug_dynrouter, { found_match, Match }),
421395
case Match of
422396
no_matching_route ->
423397
{error, <<"No matching route found">>};
@@ -433,7 +407,10 @@ preprocess(Msg1, Msg2, Opts) ->
433407
}
434408
]
435409
}
436-
end
410+
end;
411+
{ok, false} ->
412+
?event(debug_preprocess, is_not_relevant),
413+
{ok, hb_ao:get(<<"body">>, Msg2, Opts#{ hashpath => ignore })}
437414
end.
438415

439416
%%% Tests

0 commit comments

Comments
 (0)