Skip to content

Commit be25dd9

Browse files
author
immidisa
committed
Added HTTP handlers for Count & History
1 parent 8ea07b8 commit be25dd9

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed

src/compute_pool.erl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,24 @@
1010
-author("immidisa").
1111

1212
%% API
13-
-export([ start/0, compute/1]).
13+
-export([ start/0, compute/1, history_count/0, history/0]).
1414

1515

1616
start() ->
1717
compute_pool_sup:start_link(?MODULE).
1818

19-
compute(Body) ->
20-
wpool:call(?MODULE, {compute, Body}).
19+
compute(Input) ->
20+
wpool:call(?MODULE, {compute, Input}).
21+
22+
history_count() ->
23+
wpool:call(?MODULE, count).
24+
25+
history()->
26+
wpool:call(?MODULE, history).
27+
28+
29+
30+
2131

2232
%%%===================================================================
2333
%%% private functions

src/count_handler.erl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
%%%-------------------------------------------------------------------
2+
%%% @author immidisa
3+
%%% @copyright (C) 2019, <COMPANY>
4+
%%% @doc
5+
%%%
6+
%%% @end
7+
%%% Created : 28. Apr 2019 05:09
8+
%%%-------------------------------------------------------------------
9+
-module(count_handler).
10+
-author("immidisa").
11+
12+
%% API
13+
-export([init/2]).
14+
-export([content_types_provided/2]).
15+
-export([content_types_accepted/2]).
16+
-export([allowed_methods/2]).
17+
-export([router/2]).
18+
19+
init(Req, Opts) ->
20+
{cowboy_rest, Req, Opts}.
21+
22+
allowed_methods(Req, Opts) ->
23+
{[ <<"GET">>], Req, Opts}.
24+
25+
content_types_provided(Req, Opts) ->
26+
{[{<<"application/json">>, router}], Req, Opts}.
27+
28+
content_types_accepted(Req, Opts) ->
29+
{[{<<"application/json">>, router}], Req, Opts}.
30+
31+
router(Req, Opts) ->
32+
Count=compute_pool:history_count(),
33+
{ok, Req2} = cowboy_req:reply(200,
34+
[{<<"content-type">>, <<"application/json">>}],jsx:encode(Count),
35+
Req),
36+
{ok, Req2, Opts}.
37+

src/fibonacci_server_app.erl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ start(_StartType, _StartArgs) ->
1818
Routes = [ {
1919
'_',
2020
[
21-
{"/fibonacci/compute", compute_handler, []}
21+
{"/fibonacci/compute", compute_handler, []},
22+
{"/fibonacci/history_count", count_handler, []},
23+
{"/fibonacci/history", history_handler, []}
2224
]
2325
} ],
2426
Dispatch = cowboy_router:compile(Routes),

src/history_handler.erl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
%%%-------------------------------------------------------------------
2+
%%% @author immidisa
3+
%%% @copyright (C) 2019, <COMPANY>
4+
%%% @doc
5+
%%%
6+
%%% @end
7+
%%% Created : 28. Apr 2019 05:09
8+
%%%-------------------------------------------------------------------
9+
-module(history_handler).
10+
-author("immidisa").
11+
12+
%% API
13+
-export([init/2]).
14+
-export([content_types_provided/2]).
15+
-export([content_types_accepted/2]).
16+
-export([allowed_methods/2]).
17+
-export([router/2]).
18+
19+
init(Req, Opts) ->
20+
{cowboy_rest, Req, Opts}.
21+
22+
allowed_methods(Req, Opts) ->
23+
{[ <<"GET">>], Req, Opts}.
24+
25+
content_types_provided(Req, Opts) ->
26+
{[{<<"application/json">>, router}], Req, Opts}.
27+
28+
content_types_accepted(Req, Opts) ->
29+
{[{<<"application/json">>, router}], Req, Opts}.
30+
31+
router(Req, Opts) ->
32+
History=compute_pool:history(),
33+
{ok, Req2} = cowboy_req:reply(200,
34+
[{<<"content-type">>, <<"application/json">>}],jsx:encode(History),
35+
Req),
36+
{ok, Req2, Opts}.

0 commit comments

Comments
 (0)