Skip to content

Commit d554997

Browse files
authored
Merge pull request #248 from permaweb/feat/router_itempotent_register
feat: add idempotent route registration and conditional mounting
2 parents bbdf5e5 + 5a1a9a2 commit d554997

File tree

2 files changed

+85
-5
lines changed

2 files changed

+85
-5
lines changed

src/dev_green_zone.erl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,13 @@ join_peer(PeerLocation, PeerID, _M1, M2, InitOpts) ->
349349
},
350350
hb_http_server:set_opts(NewOpts),
351351
?event(successfully_joined_greenzone),
352-
353-
% After successfully joining, try to mount encrypted volume
354-
try_mount_encrypted_volume(AESKey, NewOpts),
352+
ShouldMount = hb_ao:get(<<"should_mount">>, M2, false, NewOpts),
353+
case ShouldMount of
354+
true ->
355+
try_mount_encrypted_volume(AESKey, NewOpts);
356+
false ->
357+
?event(debug, <<"Not mounting encrypted volume.">>)
358+
end,
355359

356360
{ok, #{
357361
<<"body">> =>

src/dev_router.erl

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,87 @@
2525
%%% </pre>
2626
-module(dev_router).
2727
-export([routes/3, route/2, route/3, preprocess/3]).
28-
-export([match/3, is_relevant/3]).
28+
-export([match/3, is_relevant/3, register/3]).
2929
-include_lib("eunit/include/eunit.hrl").
3030
-include("include/hb.hrl").
3131

3232
-define(DEFAULT_RELAVANT_ROUTES, [
3333
#{ <<"template">> => <<"/.*~process@1.0/.*">> }
3434
]).
3535

36+
%% A exposed register function that allows telling the current node to register
37+
%% a new route with a remote router node. This function should also be itempotent
38+
%% so that it can be called only once.
39+
register(_M1, M2, Opts) ->
40+
?event(debug_pete, {register, {msg, M2}}),
41+
Registered = hb_opts:get(registered, false, Opts),
42+
case Registered of
43+
true ->
44+
{ok, #{
45+
<<"status">> => 208,
46+
<<"message">> => <<"Node already registered.">>
47+
}};
48+
false ->
49+
RouterNode = hb_ao:get(<<"peer-location">>, M2, not_found, Opts),
50+
Prefix = hb_ao:get(<<"prefix">>, M2, not_found, Opts),
51+
Price = hb_ao:get(<<"price">>, M2, not_found, Opts),
52+
Template = hb_ao:get(<<"template">>, M2, not_found, Opts),
53+
54+
% Check if any required parameters are missing
55+
Missing = [
56+
{<<"peer-location">>, RouterNode},
57+
{<<"prefix">>, Prefix},
58+
{<<"price">>, Price},
59+
{<<"template">>, Template}
60+
],
61+
62+
MissingParams = [Param || {Param, Value} <- Missing, Value =:= not_found],
63+
64+
case MissingParams of
65+
[] ->
66+
% All required parameters are present, proceed with registration
67+
Req = #{
68+
<<"path">> => <<"/router~node-process@1.0/schedule">>,
69+
<<"method">> => <<"POST">>,
70+
<<"body">> =>
71+
hb_message:commit(
72+
#{
73+
<<"path">> => <<"register">>,
74+
<<"route">> =>
75+
#{
76+
<<"prefix">> => Prefix,
77+
<<"template">> => Template,
78+
<<"price">> => Price
79+
},
80+
<<"body">> => M2
81+
},
82+
Opts
83+
)
84+
},
85+
case hb_http:post(RouterNode, Req, Opts) of
86+
{ok, _} ->
87+
hb_http_server:set_opts(Opts#{ registered => true }),
88+
{ok, <<"Route registered.">>};
89+
{error, _} ->
90+
{error, <<"Failed to register route.">>}
91+
end;
92+
_ ->
93+
% Some parameters are missing, return error message
94+
ParamList = lists:foldl(
95+
fun(Param, Acc) ->
96+
case Acc of
97+
<<>> -> Param;
98+
_ -> <<Acc/binary, ", ", Param/binary>>
99+
end
100+
end,
101+
<<>>,
102+
MissingParams
103+
),
104+
ErrorMsg = <<"Missing required parameters: ", ParamList/binary>>,
105+
{error, ErrorMsg}
106+
end
107+
end.
108+
36109
%% @doc Device function that returns all known routes.
37110
routes(M1, M2, Opts) ->
38111
?event({routes_msg, M1, M2}),
@@ -394,7 +467,10 @@ preprocess(Msg1, Msg2, Opts) ->
394467
{_, Match} = match(#{ <<"routes">> => TemplateRoutes}, Req, Opts),
395468
case Match of
396469
no_matching_route ->
397-
{error, <<"No matching route found">>};
470+
{ok, #{
471+
<<"status">> => 404,
472+
<<"message">> => <<"No matching template found in the given routes.">>
473+
}};
398474
_ ->
399475
{ok,
400476
[

0 commit comments

Comments
 (0)