Skip to content

Commit

Permalink
Initial version of vmq-web-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
mths1 committed Jan 5, 2024
1 parent 589bac7 commit 2bfd9e7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
3 changes: 1 addition & 2 deletions apps/vmq_web_ui/src/vmq_web_ui.erl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ token_valid(Token) ->
true ->
true;
_ ->
ets:remove(webuitoken, Token),
false
end;
_ ->
Expand All @@ -112,7 +111,7 @@ token_valid(Token) ->

logout(Req, _State) ->
Token = cowboy_req:header(<<"x-token">>, Req, undefined),
ets:remove(webuitoken, Token).
ets:delete(webuitoken, Token).

login(Req, State) ->
UserName = cowboy_req:header(<<"username">>, Req, undefined),
Expand Down
62 changes: 55 additions & 7 deletions apps/vmq_web_ui/src/vmq_web_ui_cli.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

register_cli() ->
clique:register_usage(["vmq-admin", "webui"], webui_usage()),
clique:register_usage(["vmq-admin", "webui", "show"], webui_show_usage()),
clique:register_usage(["vmq-admin", "webui", "install"], webui_install_usage()),
show_cmd(),
install_cmd().

Expand All @@ -27,9 +29,10 @@ show_cmd() ->
Table =
[
[
{'Frontend path', code:priv_dir(vmq_web_ui)},
{'Frontend version', "Preview"}
]
{'Item', <<"Frontend Path">>},
{'Value', code:priv_dir(vmq_web_ui) ++ "/www"}
],
[{'Item', <<"Frontend Version">>}, {'Value', "Preview"}]
],
[clique_status:table(Table)];
(_, _, _) ->
Expand All @@ -40,12 +43,43 @@ show_cmd() ->

install_cmd() ->
Cmd = ["vmq-admin", "webui", "install"],
FlagSpecs = [
{version, [
{shortname, "v"},
{longname, "version"},
{typecast, fun(Version) -> Version end}
]},
{repository, [
{shortname, "r"},
{longname, "repo"},
{typecast, fun(Repro) -> Repro end}
]}
],
Callback =
fun(_, _, _) ->
Text = clique_status:text(webui_usage()),
[clique_status:alert([Text])]
fun(_, [], Flags) ->
Version = proplists:get_value(version, Flags, "v0.0.1"),
Rep = proplists:get_value(repository, Flags, "vernemq"),

Link =
"https://github.com/" ++ Rep ++ "/vmq_webadmin/releases/download/" ++ Version ++
"/frontend.zip",
Response = httpc:request(get, {Link, []}, [], []),
Text =
case Response of
{ok, {{_, 200, "OK"}, _Headers, Body}} ->
ok = file:write_file("/tmp/vmq_web_ui.zip", Body),
ok = file:del_dir_r(code:priv_dir(vmq_web_ui) ++ "/www"),
{ok, _} = zip:extract("/tmp/vmq_web_ui.zip", [
{'cwd', code:priv_dir(vmq_web_ui) ++ "/www"}
]),
["Frontend installed. Please clear browser cache and try it out..."];
_ ->
["Error downloading file"]
end,
TextC = clique_status:text(Text),
[clique_status:alert([TextC])]
end,
clique:register_command(Cmd, [], [], Callback).
clique:register_command(Cmd, [], FlagSpecs, Callback).

webui_usage() ->
[
Expand All @@ -56,3 +90,17 @@ webui_usage() ->
" install Install/update frontend \n"
" Use --help after a sub-command for more details.\n"
].

webui_show_usage() ->
[
"vmq-admin webui shown\n",
" Shows VerneMQ Web UI informatiom\n\n",
" Use --help after a sub-command for more details.\n"
].

webui_install_usage() ->
[
"vmq-admin webui install --repro vernemq --version v0.0.1\n",
" Installs a new frontend version. --repro and --version are optional.\n\n",
" Use --help after a sub-command for more details.\n"
].

0 comments on commit 2bfd9e7

Please sign in to comment.