%%% 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) -> Size = index:size(newentries_path()), index:getrange(newentries_path(), Index, Size - 1). %% Private functions. html(Text, Input) -> {400, [{"Content-Type", "text/html"}], io_lib:format( "

~n" ++ "~s~n" ++ "~p~n" ++ "~n", [Text, Input])}. success(Data) -> {200, [{"Content-Type", "text/json"}], mochijson2:encode(Data)}.