Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 35 lines (28 sloc) 1.116 kb
3a8ab7d @mojombo first commit
authored
1 -module(rebar).
2 -export([start/0, handle/1]).
3
4 start() ->
5 {ok, LSock} = gen_tcp:listen(5500, [binary, {packet, 0}, {active, false}]),
6 loop(LSock).
7
8 loop(LSock) ->
9 {ok, Sock} = gen_tcp:accept(LSock),
10 spawn(rebar, handle, [Sock]),
11 loop(LSock).
12
13 handle(Sock) ->
14 % read the request from the socket
15 {ok, Bin} = gen_tcp:recv(Sock, 0),
16 {ok, Json} = json:decode_string(binary_to_list(Bin)),
17
18 % pull the request apart
19 {Method, Params, Id} = parse(Json),
20 [Module, Function] = string:tokens(Method, ":"),
21
22 % call the function
23 io:format("~p:~p(~p)~n", [Module, Function, Params]),
24 Return = apply(list_to_atom(Module), list_to_atom(Function), tuple_to_list(Params)),
25
26 % send the response
27 gen_tcp:send(Sock, json:encode(json:obj_from_list([{"result", Return}, {"error", null}, {"id", Id}]))),
28 ok = gen_tcp:close(Sock).
29
30 parse(Json) ->
31 {json_object, Body} = Json,
32 {value, {"method", Method}} = lists:keysearch("method", 1, Body),
33 {value, {"params", Params}} = lists:keysearch("params", 1, Body),
34 {value, {"id", Id}} = lists:keysearch("id", 1, Body),
35 {Method, Params, Id}.
Something went wrong with that request. Please try again.