forked from gotthardp/lorawan-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlorawan_admin_config_js.erl
More file actions
67 lines (56 loc) · 2.18 KB
/
lorawan_admin_config_js.erl
File metadata and controls
67 lines (56 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
%
% Copyright (c) 2016-2019 Petr Gotthard <[email protected]>
% All rights reserved.
% Distributed under the terms of the MIT License. See the LICENSE file.
%
-module(lorawan_admin_config_js).
-export([init/2]).
-export([allowed_methods/2]).
-export([is_authorized/2]).
-export([forbidden/2]).
-export([content_types_provided/2]).
-export([handle_get/2]).
-include("lorawan.hrl").
-record(state, {scopes, auth_fields}).
init(Req, Scopes) ->
{cowboy_rest, Req, #state{scopes=Scopes}}.
allowed_methods(Req, State) ->
{[<<"OPTIONS">>, <<"GET">>], Req, State}.
is_authorized(Req, #state{scopes=Scopes}=State) ->
case lorawan_admin:handle_authorization(Req, Scopes) of
{true, AuthFields} ->
{true, Req, State#state{auth_fields=AuthFields}};
Else ->
{Else, Req, State}
end.
forbidden(Req, #state{auth_fields=AuthFields}=State) ->
{lorawan_admin:fields_empty(AuthFields), Req, State}.
content_types_provided(Req, State) ->
{[
{{<<"application">>, <<"javascript">>, []}, handle_get}
], Req, State}.
handle_get(Req, State) ->
[#config{items_per_page=Items}] =
mnesia:dirty_read(config, <<"main">>),
{ok, TileServer} = application:get_env(lorawan_server, map_tile_server),
{variable(<<"NodeName">>, atom_to_binary(node(), latin1),
variable(<<"MapTileServer">>, TileServer,
if
Items == undefined ->
variable(<<"ItemsPerPage">>, 30,
variable(<<"InfinitePagination">>, true, <<>>));
true ->
variable(<<"ItemsPerPage">>, Items,
variable(<<"InfinitePagination">>, false, <<>>))
end)), Req, State}.
variable(Name, undefined, Bin) ->
<<"var ", Name/binary, "=null;\r", Bin/binary>>;
variable(Name, Value, Bin) when is_integer(Value) ->
<<"var ", Name/binary, "=", (integer_to_binary(Value))/binary, ";\r", Bin/binary>>;
variable(Name, Value, Bin) when is_binary(Value) ->
<<"var ", Name/binary, "=\"", Value/binary, "\";\r", Bin/binary>>;
variable(Name, true, Bin) ->
<<"var ", Name/binary, "=true;\r", Bin/binary>>;
variable(Name, false, Bin) ->
<<"var ", Name/binary, "=false;\r", Bin/binary>>.
% end of file