summaryrefslogtreecommitdiff
path: root/src/storage.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/storage.erl')
-rw-r--r--src/storage.erl63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/storage.erl b/src/storage.erl
new file mode 100644
index 0000000..8136308
--- /dev/null
+++ b/src/storage.erl
@@ -0,0 +1,63 @@
+%%% Copyright (c) 2014, NORDUnet A/S.
+%%% See LICENSE for licensing information.
+
+%%% @doc Storage node API
+
+-module(storage).
+%% API (URL)
+-export([request/3]).
+
+newentries_path() ->
+ {ok, Value} = application:get_env(plop, newentries_path),
+ Value.
+
+request(post, "ct/storage/sendentry", Input) ->
+ case (catch mochijson2:decode(Input)) of
+ {error, E} ->
+ html("sendentry: bad input:", E);
+ {struct, 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),
+ success({[{result, <<"ok">>}]})
+ end;
+request(post, "ct/storage/entrycommitted", Input) ->
+ case (catch mochijson2:decode(Input)) of
+ {error, E} ->
+ html("entrycommitted: bad input:", E);
+ {struct, PropList} ->
+ EntryHash = base64:decode(proplists:get_value(<<"entryhash">>, PropList)),
+ LeafHash = base64:decode(proplists:get_value(<<"treeleafhash">>, PropList)),
+ ok = db:add_entryhash(LeafHash, EntryHash),
+ success({[{result, <<"ok">>}]})
+ end;
+request(get, "ct/storage/fetchnewentries", _Input) ->
+ NewHashes = fetchnewhashes(0),
+ Entries = lists:map(fun(LeafHash) ->
+ {[{hash, base64:encode(LeafHash)},
+ {entry, base64:encode(db:entry_for_leafhash(LeafHash))}]}
+ end, NewHashes),
+ success({[{result, <<"ok">>},
+ {entries, Entries}]}).
+
+fetchnewhashes(Index) ->
+ case index:indexsize(newentries_path()) of
+ 0 ->
+ [];
+ Size ->
+ index:getrange(newentries_path(), Index, Size - 1)
+ end.
+
+%% Private functions.
+html(Text, Input) ->
+ {400, [{"Content-Type", "text/html"}],
+ io_lib:format(
+ "<html><body><p>~n" ++
+ "~s~n" ++
+ "~p~n" ++
+ "</body></html>~n", [Text, Input])}.
+
+success(Data) ->
+ {200, [{"Content-Type", "text/json"}], mochijson2:encode(Data)}.