summaryrefslogtreecommitdiff
path: root/src/storage.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/storage.erl')
-rw-r--r--src/storage.erl89
1 files changed, 38 insertions, 51 deletions
diff --git a/src/storage.erl b/src/storage.erl
index b966a12..e09acdb 100644
--- a/src/storage.erl
+++ b/src/storage.erl
@@ -5,43 +5,42 @@
-module(storage).
%% API (URL)
--export([sendentry/3, entrycommitted/3, fetchnewentries/3]).
+-export([request/3]).
newentries_path() ->
{ok, Value} = application:get_env(plop, newentries_path),
Value.
-sendentry(SessionID, _Env, Input) ->
- lager:debug("~p", [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)),
+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),
- binary_to_list(
- jiffy:encode(
- {[{result, <<"ok">>}]}))
- end),
- lager:debug("result ~p", [R]),
- 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).
+ 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) ->
lists:reverse(fetchnewhashes(Index, [])).
@@ -54,26 +53,14 @@ fetchnewhashes(Index, Acc) ->
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]).
+ {400, [{"Content-Type", "text/html"}],
+ io_lib:format(
+ "<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).
+success(Data) ->
+ {200, [{"Content-Type", "text/json"}], mochijson2:encode(Data)}.