summaryrefslogtreecommitdiff
path: root/src/storage.erl
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2014-10-20 12:22:50 +0200
committerMagnus Ahltorp <map@kth.se>2014-10-24 15:36:35 +0200
commit088e4de4f1e2499f6cc0e332ae8cb34b935a6425 (patch)
tree37d1fad7955ed7d028e9b7bd9d642a20c5928420 /src/storage.erl
parent80c8ef847d996af04ec677a79555d640733641f2 (diff)
Added HTTP API:s for external merge
Diffstat (limited to 'src/storage.erl')
-rw-r--r--src/storage.erl77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/storage.erl b/src/storage.erl
new file mode 100644
index 0000000..243cc6c
--- /dev/null
+++ b/src/storage.erl
@@ -0,0 +1,77 @@
+%%% Copyright (c) 2014, NORDUnet A/S.
+%%% See LICENSE for licensing information.
+
+%%% @doc Storage node API
+
+-module(storage).
+%% API (URL)
+-export([sendentry/3, entrycommitted/3, fetchnewentries/3]).
+
+newentries_path() ->
+ {ok, Value} = application:get_env(plop, newentries_path),
+ Value.
+
+sendentry(SessionID, _Env, Input) ->
+ R = (catch case (catch jiffy:decode(Input)) of
+ {error, E} ->
+ html("sendentry: bad input:", E);
+ {PropList} ->
+ LogEntry = base64:decode(proplists:get_value(<<"entry">>, PropList)),
+ TreeLeafHash = base64:decode(proplists:get_value(<<"treeleafhash">>, PropList)),
+
+ ok = db:add(TreeLeafHash, LogEntry),
+ ok = index:addlast(newentries_path(), TreeLeafHash),
+ binary_to_list(
+ jiffy:encode(
+ {[{result, <<"ok">>}]}))
+ end),
+ deliver(SessionID, R).
+
+entrycommitted(SessionID, _Env, Input) ->
+ R = (catch case (catch jiffy:decode(Input)) of
+ {error, E} ->
+ html("entrycommitted: bad input:", E);
+ {PropList} ->
+ EntryHash = base64:decode(proplists:get_value(<<"entryhash">>, PropList)),
+ LeafHash = base64:decode(proplists:get_value(<<"treeleafhash">>, PropList)),
+ ok = db:add_entryhash(LeafHash, EntryHash),
+ binary_to_list(
+ jiffy:encode(
+ {[{result, <<"ok">>}]}))
+ end),
+ deliver(SessionID, R).
+
+fetchnewhashes(Index) ->
+ lists:reverse(fetchnewhashes(Index, [])).
+
+fetchnewhashes(Index, Acc) ->
+ case index:get(newentries_path(), Index) of
+ noentry ->
+ Acc;
+ Entry ->
+ fetchnewhashes(Index + 1, [Entry | Acc])
+ end.
+
+fetchnewentries(SessionID, _Env, _Input) ->
+ NewHashes = fetchnewhashes(0),
+ Entries = lists:map(fun(LeafHash) ->
+ {[{hash, base64:encode(LeafHash)},
+ {entry, base64:encode(db:entry_for_leafhash(LeafHash))}]}
+ end, NewHashes),
+ R = (catch binary_to_list(
+ jiffy:encode(
+ {[{result, <<"ok">>}, {entries, Entries}]}))),
+ deliver(SessionID, R).
+
+%% Private functions.
+html(Text, Input) ->
+ io_lib:format(
+ "Content-Type: text/html\r\n\r\n" ++
+ "<html><body><p>~n" ++
+ "~s~n" ++
+ "~p~n" ++
+ "</body></html>~n", [Text, Input]).
+
+-spec deliver(any(), string()) -> ok | {error, _Reason}.
+deliver(Session, Data) ->
+ mod_esi:deliver(Session, Data).