snapkitty
developer-tools
c++
SNAPKITTYWEST commited on
Commit
f21408d
·
verified ·
1 Parent(s): e18b22f

Add src/hk_api_events_recent_h.erl

Browse files
Files changed (1) hide show
  1. src/hk_api_events_recent_h.erl +25 -0
src/hk_api_events_recent_h.erl ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ %%%-------------------------------------------------------------------
2
+ %%% @doc `/events' API domain, HTTP half: `GET /api/events/recent'
3
+ %%% returns the tail of the append-only audit log directly from
4
+ %%% `hk_event_bus' -- this backs the frontend's "audit" view when it
5
+ %%% first opens (before any live events have arrived over the
6
+ %%% WebSocket) and lets any HTTP client inspect recent activity
7
+ %%% without holding an open connection.
8
+ %%% @end
9
+ %%%-------------------------------------------------------------------
10
+ -module(hk_api_events_recent_h).
11
+
12
+ -export([init/2]).
13
+
14
+ init(Req0 = #{method := <<"GET">>}, State) ->
15
+ Qs = cowboy_req:parse_qs(Req0),
16
+ Limit = case lists:keyfind(<<"limit">>, 1, Qs) of
17
+ {<<"limit">>, LimitBin} -> binary_to_integer(LimitBin);
18
+ false -> 100
19
+ end,
20
+ Events = hk_event_bus:list_recent(Limit),
21
+ Req = hk_api_util:reply_json(200, #{events => Events}, Req0),
22
+ {ok, Req, State};
23
+ init(Req0, State) ->
24
+ Req = hk_api_util:reply_error(405, method_not_allowed, Req0),
25
+ {ok, Req, State}.