Last active
March 21, 2016 22:49
-
-
Save MoreOutput/a50d6ac1ecc3e00e18a9 to your computer and use it in GitHub Desktop.
Revisions
-
MoreOutput revised this gist
Mar 21, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ -module(couch_gun). -export([connect/2, all_documents/2, get_document/3]). %% Return an id for a couchdb connection -
MoreOutput revised this gist
Mar 21, 2016 . 1 changed file with 15 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ -module(test_handler). -export([init/3, content_types_provided/2, get_doc/2]). init(_Transport, _Req, _Opts) -> {upgrade, protocol, cowboy_rest}. content_types_provided(Req, State) -> {[{<<"application/json">>, get_doc}], Req, State}. get_doc(Req, State) -> {Pid, Protocol} = couchdb:connect(), Doc = list_to_binary(couchdb:get_document(Pid, Protocol, "testing")), {Doc, Req, State}. -
MoreOutput created this gist
Mar 21, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,67 @@ -module(couchdb). -export([connect/2, all_documents/2, get_document/3]). %% Return an id for a couchdb connection connect(_User, _Password) -> gun:open("127.0.0.1", 5984), receive {gun_up, Pid, Protocol} -> {Pid, Protocol}; {gun_down, _Protocol, Reason, _} -> io:format("Lost connection ~n~p", [Reason]) end. %% Document collection functions get(Pid, Protocol, StreamId) -> receive {gun_response, Pid, StreamId, fin, _Status, _Headers} -> no_data; {gun_response, Pid, StreamId, nofin, _Status, _Headers} -> receive_data(Pid, Protocol, StreamId); {'DOWN', Protocol, process, Pid, Reason} -> error_logger:error_msg("Error in coucdb:get/3"), exit(Reason) after 1000 -> exit(timeout) end. all_documents(Pid, Protocol) -> StreamId = gun:get(Pid, "/nomark/_all_docs", [ {<<"accept">>, "application/json"} ], get(Pid, Protocol, StreamId). get_document(Pid, Protocol, DocId) -> StreamId = gun:get(Pid, "/nomark/" ++ DocId, [ {<<"accept">>, "application/json"} ], get(Pid, Protocol, StreamId). %% Functions below for processing DB requests asyncronously receive_data(Pid, Protocol, StreamId, Data2) -> receive {gun_data, Pid, StreamId, fin, Data} -> [Data2, Data]; {gun_data, Pid, StreamId, nofin, Data} -> receive_data(Pid, Protocol, StreamId, [Data2, Data]); {'DOWN', Protocol, process, Pid, Reason} -> error_logger:error_msg("Error in couchdb:receive_data/4"), exit(Reason) after 1000 -> exit(timeout) end. receive_data(Pid, Protocol, StreamId) -> receive {gun_data, Pid, StreamId, nofin, Data} -> receive_data(Pid, Protocol, StreamId, Data); {gun_data, Pid, StreamId, fin, Data} -> [Data]; {'DOWN', Protocol, process, Pid, Reason} -> error_logger:error_msg("Error in couchdb:receive_data/3"), exit(Reason) after 1000 -> exit(timeout) end.