Skip to content

Commit

Permalink
Flexible encode method for payload
Browse files Browse the repository at this point in the history
  • Loading branch information
HJianBo authored and tigercl committed May 8, 2019
1 parent 42518cf commit 9b55c93
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
8 changes: 4 additions & 4 deletions etc/emqx_web_hook.conf
@@ -1,12 +1,12 @@

web.hook.api.url = http://127.0.0.1:8080

## Encode message payload field with base64
## Encode message payload field
##
## Value: true | false
## Value: base64 | base62
##
## Default: false
## web.hook.encode_payload = false
## Default: undefined
## web.hook.encode_payload = base64

web.hook.rule.client.connected.1 = {"action": "on_client_connected"}
web.hook.rule.client.disconnected.1 = {"action": "on_client_disconnected"}
Expand Down
4 changes: 2 additions & 2 deletions priv/emqx_web_hook.schema
Expand Up @@ -6,8 +6,8 @@
]}.

{mapping, "web.hook.encode_payload", "emqx_web_hook.encode_payload", [
{default, false},
{datatype, {enum, [true, false]}}
{default, undefined},
{datatype, {enum, [base62, base64]}}
]}.

{mapping, "web.hook.rule.client.connected.$name", "emqx_web_hook.rules", [
Expand Down
9 changes: 5 additions & 4 deletions src/emqx_web_hook.erl
Expand Up @@ -298,10 +298,11 @@ format_from(#message{from = ClientId, headers = _HeadersNoUsername}) ->
{a2b(ClientId), <<"undefined">>}.

encode_payload(Payload) ->
case application:get_env(?APP, encode_payload, false) of
false -> Payload;
true -> base64:encode(Payload)
end.
encode_payload(Payload, application:get_env(?APP, encode_payload, undefined)).

encode_payload(Payload, base62) -> emqx_base62:encode(Payload);
encode_payload(Payload, base64) -> base64:encode(Payload);
encode_payload(Payload, _) -> Payload.

a2b(A) when is_atom(A) -> erlang:atom_to_binary(A, utf8);
a2b(A) -> A.
Expand Down

0 comments on commit 9b55c93

Please sign in to comment.