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" >> => <<" /~m eta@1.0/.*" >> },
36- #{ <<" template" >> => <<" /~g reenzone@1.0/.*" >> },
37- #{ <<" template" >> => <<" /.*~n ode-process@1.0/.*" >> },
38- #{ <<" template" >> => <<" /~s np@1.0/.*" >> },
39- #{ <<" template" >> => <<" /~p 4@1.0/.*" >> }
32+ -define (DEFAULT_RELAVANT_ROUTES , [
33+ #{ <<" template" >> => <<" /.*~p rocess@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.
403388preprocess (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