Start to split the back-end further in more minor processes.

The back-end will expose only the public APIs to be consumed
by the listeners. The new users server takes care of the users
management.

Other subsystems will follow (i.e. GUI, fedi, blog etc..).
main
absc 2024-09-15 12:14:29 +00:00
parent e5631798ce
commit 5f14a4f6cb
11 changed files with 261 additions and 12 deletions

View File

@ -7,7 +7,6 @@ ERLFLAGS= -I ../../
OBJS= dudeswave_backend.beam dudeswave_backend_app.beam OBJS= dudeswave_backend.beam dudeswave_backend_app.beam
OBJS+= dudeswave_backend_supervisor.beam OBJS+= dudeswave_backend_supervisor.beam
OBJS+= dudeswave_backend_auth.beam dudeswave_backend_user.beam
all: ${OBJS} all: ${OBJS}

View File

@ -43,6 +43,10 @@ init([]) ->
% Public APIs % Public APIs
% %
%
% Users management
%
auth(cookie, User, Cookie) -> auth(cookie, User, Cookie) ->
gen_server:call({local, ?MODULE}, {cookie, User, Cookie}); gen_server:call({local, ?MODULE}, {cookie, User, Cookie});
@ -69,7 +73,7 @@ delete_user(User) ->
% %
handle_call({cookie, User, Cookie}, _From, State) -> handle_call({cookie, User, Cookie}, _From, State) ->
case dudeswave_backend_auth:authenticate(cookie, User, Cookie) of case dudeswave_users:authenticate(cookie, User, Cookie) of
true -> true ->
{reply, true, State}; {reply, true, State};
false -> false ->
@ -79,7 +83,7 @@ handle_call({cookie, User, Cookie}, _From, State) ->
end; end;
handle_call({password, User, Password}, _From, State) -> handle_call({password, User, Password}, _From, State) ->
case dudeswave_backend_auth:authenticate(password, User, Password) of case dudeswave_users:authenticate(password, User, Password) of
{true, Cookie, Validity} -> {true, Cookie, Validity} ->
{reply, {true, Cookie, Validity}, State}; {reply, {true, Cookie, Validity}, State};
false -> false ->
@ -89,32 +93,32 @@ handle_call({password, User, Password}, _From, State) ->
end; end;
handle_call({logout, User, Cookie}, _From, State) -> handle_call({logout, User, Cookie}, _From, State) ->
case dudeswave_backend_auth:logout(User, Cookie) of case dudeswave_users:logout(User, Cookie) of
ok -> {reply, ok, State}; ok -> {reply, ok, State};
{error, Reason} -> {reply, {error, Reason}, State} {error, Reason} -> {reply, {error, Reason}, State}
end; end;
handle_call({user_details, User}, _From, State) -> handle_call({user_details, User}, _From, State) ->
case dudeswave_backend_user:details(User) of case dudeswave_users:details(User) of
{error, not_found} -> {reply, not_found, State}; {error, not_found} -> {reply, not_found, State};
{error, Reason} -> {reply, {error, Reason}, State}; {error, Reason} -> {reply, {error, Reason}, State};
Val -> {reply, Val, State} Val -> {reply, Val, State}
end; end;
handle_call({new_user, User, Password, Email}, _From, State) -> handle_call({new_user, User, Password, Email}, _From, State) ->
case dudeswave_backend_user:new(User, Password, Email) of case dudeswave_users:new(User, Password, Email) of
ok -> {reply, ok, State}; ok -> {reply, ok, State};
{error, Reason} -> {reply, {error, Reason}, State} {error, Reason} -> {reply, {error, Reason}, State}
end; end;
handle_call({update_user, User, Name, Email, Desc}, _From, State) -> handle_call({update_user, User, Name, Email, Desc}, _From, State) ->
case dudeswave_backend_user:update(User, Name, Email, Desc) of case dudeswave_users:update(User, Name, Email, Desc) of
ok -> {reply, ok, State}; ok -> {reply, ok, State};
{error, Reason} -> {reply, {error, Reason}, State} {error, Reason} -> {reply, {error, Reason}, State}
end; end;
handle_call({delete_user, User}, _From, State) -> handle_call({delete_user, User}, _From, State) ->
case dudeswave_backend_user:delete(User) of case dudeswave_users:delete(User) of
ok -> {reply, ok, State}; ok -> {reply, ok, State};
{error, Reason} -> {reply, {error, Reason}, State} {error, Reason} -> {reply, {error, Reason}, State}
end. end.

7
dudeswave_users/Makefile Normal file
View File

@ -0,0 +1,7 @@
.PHONY: all clean
all:
${MAKE} -C src
clean:
${MAKE} -C src clean

View File

@ -0,0 +1,10 @@
{application,dudeswave_users,
[{description,"The dudeswave users management module"},
{vsn,"1.0.0"},
{modules,[dudeswave_users,dudeswave_users_app,
dudeswave_users_supervisor,
dudeswave_users_auth,dudeswave_users_user]},
{registered,[]},
{applications,[kernel,stdlib,erts,cowboy,ranch]},
{mod,{dudeswave_users_app,[]}},
{start_phases,[]}]}.

View File

@ -0,0 +1,23 @@
%
% Copyright (c) 2024 Andrea Biscuola <a@abiscuola.com>
%
% Permission to use, copy, modify, and distribute this software for any
% purpose with or without fee is hereby granted, provided that the above
% copyright notice and this permission notice appear in all copies.
%
% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
%
-define(APPBUCK, dudeswave).
-define(USERSBUCK, dudes).
-define(COOKIESBUCK, cookies).
-define(RANDBYTES, 32).
-define(DEFVALIDITY, 365).
-define(DUDENAME, "dudename").
-define(DUDEAUTH, "dudeauth").

View File

@ -0,0 +1,19 @@
.PHONY: all clean
.SUFFIXES: .erl .beam
ERLC?= erlc -server
ERLFLAGS= -I ../../
OBJS= dudeswave_users.beam dudeswave_users_app.beam
OBJS+= dudeswave_users_supervisor.beam
OBJS+= dudeswave_users_auth.beam dudeswave_users_user.beam
all: ${OBJS}
.erl.beam:
${ERLC} ${ERLOPTS} ${ERLFLAGS} $<
clean:
rm -f *.beam

View File

@ -0,0 +1,125 @@
%
% Copyright (c) 2024 Andrea Biscuola <a@abiscuola.com>
%
% Permission to use, copy, modify, and distribute this software for any
% purpose with or without fee is hereby granted, provided that the above
% copyright notice and this permission notice appear in all copies.
%
% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
%
-module(dudeswave_users).
-behaviour(gen_server).
% Server start function
-export([start_link/0]).
% Module callbacks
-export([init/1, handle_call/3, handle_cast/2, terminate/2]).
% Public API exports
-export([auth/3, logout/2, details/1, new/3,
update/4, delete/1]).
%
% Startup functions
%
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
init([]) ->
process_flag(trap_exit, true),
{ok, 0}.
%
% Public APIs
%
auth(cookie, User, Cookie) ->
gen_server:call({local, ?MODULE}, {cookie, User, Cookie});
auth(password, User, Password) ->
gen_server:call({local, ?MODULE}, {password, User, Password}).
logout(User, Cookie) ->
gen_server:call({local, ?MODULE}, {logout, User, Cookie}).
details(User) ->
gen_server:call({local, ?MODULE}, {details, User}).
new(User, Password, Email) ->
gen_server:call({local, ?MODULE}, {new, User, Password, Email}).
update(User, Name, Email, Desc) ->
gen_server:call({local, ?MODULE}, {update, User, Name, Email, Desc}).
delete(User) ->
gen_server:call({local, ?MODULE}, {delete, User}).
%
% Callbacks
%
handle_call({cookie, User, Cookie}, _From, State) ->
case dudeswave_users_auth:authenticate(cookie, User, Cookie) of
true ->
{reply, true, State};
false ->
{reply, false, State};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
handle_call({password, User, Password}, _From, State) ->
case dudeswave_users_auth:authenticate(password, User, Password) of
{true, Cookie, Validity} ->
{reply, {true, Cookie, Validity}, State};
false ->
{reply, false, State};
{error, Reason} ->
{reply, {error, Reason}, State}
end;
handle_call({logout, User, Cookie}, _From, State) ->
case dudeswave_users_auth:logout(User, Cookie) of
ok -> {reply, ok, State};
{error, Reason} -> {reply, {error, Reason}, State}
end;
handle_call({details, User}, _From, State) ->
case dudeswave_users_user:details(User) of
{error, not_found} -> {reply, not_found, State};
{error, Reason} -> {reply, {error, Reason}, State};
Val -> {reply, Val, State}
end;
handle_call({new, User, Password, Email}, _From, State) ->
case dudeswave_users_user:new(User, Password, Email) of
ok -> {reply, ok, State};
{error, Reason} -> {reply, {error, Reason}, State}
end;
handle_call({update, User, Name, Email, Desc}, _From, State) ->
case dudeswave_users_user:update(User, Name, Email, Desc) of
ok -> {reply, ok, State};
{error, Reason} -> {reply, {error, Reason}, State}
end;
handle_call({delete, User}, _From, State) ->
case dudeswave_users_user:delete(User) of
ok -> {reply, ok, State};
{error, Reason} -> {reply, {error, Reason}, State}
end.
handle_cast(_Msg, State) -> {noreply, State}.
terminate(_Reason, _N) -> ok.

View File

@ -0,0 +1,31 @@
%
% Copyright (c) 2024 Andrea Biscuola <a@abiscuola.com>
%
% Permission to use, copy, modify, and distribute this software for any
% purpose with or without fee is hereby granted, provided that the above
% copyright notice and this permission notice appear in all copies.
%
% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
%
-module(dudeswave_users_app).
-behaviour(application).
-export([bootstrap/3, start/2, stop/1]).
start(_Type, StartArgs) ->
crypto:rand_seed(),
dudeswave_users_supervisor:start_link(StartArgs).
stop(_State) -> ok.
%
% Bootstrap procedure to be completed
%
bootstrap(_Admin, _Password, _Nodes) -> ok.

View File

@ -13,14 +13,14 @@
% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF % ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. % OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
% %
-module(dudeswave_backend_auth). -module(dudeswave_users_auth).
-moduledoc """ -moduledoc """
Dudes authentication module Dudes authentication module
Here lives all the functions for the APIs needed to handle users authentication. Here lives all the functions for the APIs needed to handle users authentication.
""". """.
-include_lib("dudeswave_backend/include/defines.hrl"). -include_lib("dudeswave_users/include/defines.hrl").
-include_lib("storage/include/storage.hrl"). -include_lib("storage/include/storage.hrl").
-export([authenticate/3, logout/2]). -export([authenticate/3, logout/2]).

View File

@ -0,0 +1,31 @@
%
% Copyright (c) 2024 Andrea Biscuola <a@abiscuola.com>
%
% Permission to use, copy, modify, and distribute this software for any
% purpose with or without fee is hereby granted, provided that the above
% copyright notice and this permission notice appear in all copies.
%
% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
%
-module(dudeswave_users_supervisor).
-behaviour(supervisor).
-export([start/0,
start_link/1,
init/1]).
start() ->
spawn(fun() -> supervisor:start_link({local, ?MODULE}, ?MODULE, _Arg = []) end).
start_link(Args) ->
supervisor:start_link({local, ?MODULE}, ?MODULE, Args).
init([]) ->
{ok, {{one_for_one, 3, 10}, [{tag1, {dudeswave_users, start_link, []}, permanent,
10000, worker, [dudeswave_users]}]}}.

View File

@ -13,9 +13,9 @@
% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF % ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. % OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
% %
-module(dudeswave_backend_user). -module(dudeswave_users_user).
-include_lib("dudeswave_backend/include/defines.hrl"). -include_lib("dudeswave_users/include/defines.hrl").
-include_lib("storage/include/storage.hrl"). -include_lib("storage/include/storage.hrl").
-export([details/1, new/3, update/4, delete/1]). -export([details/1, new/3, update/4, delete/1]).