Browse Source

kvs sample index.htm

Namdak Tonpa 5 years ago
parent
commit
c4d3d5ae5a
4 changed files with 64 additions and 16 deletions
  1. 45 6
      index.html
  2. 6 0
      src/money.erl
  3. 2 3
      src/rest.erl
  4. 11 7
      src/rest_kvs.erl

+ 45 - 6
index.html

@@ -36,7 +36,9 @@
 
         <section>
             <a name="usage"></a><h3>USAGE</h3>
-            <div>Just plug REST endpoint directly to your Cowboy router:</div><br/>
+            <div>Just plug REST endpoint directly to your Cowboy router in order
+                 to gain the JSON access for both in-memory ETS and persistent
+                 KVS databases:</div><br/>
         <figure>
         <code> {"/:res",rest_cowboy,[]},
  {"/:res/:id",rest_cowboy,[]},
@@ -44,7 +46,7 @@
  {"/kvs/1/:id/[...]",rest_kvs,[]},</code>
         </figure><br/>
             <div>
-                OCT 2018 © <a href="https://github.com/5HT">5HT</a> <a href="https://5ht.co/license.htm">ISC</a><br />
+                OCT 2019 © <a href="https://github.com/5HT">5HT</a> <a href="https://5ht.co/license.htm">ISC</a><br />
                 VER 5.10
             </div>
         </section>
@@ -52,10 +54,8 @@
 </aside>
 <main>
     <section>
-        <a name="module"></a><h3>Module</h3>
-        <p>
-            Sample REST service implementation:
-        </p>
+        <a name="ets"></a><h3>ETS JSON</h3>
+        <p>Simple table-oriented service implementation:</p>
         <figure>
         <code>
  -module(users).
@@ -86,6 +86,45 @@
  $ curl -i -X GET localhost:8005/rest/users/5HT
  $ curl -i -X DELETE localhost:8005/rest/users/5HT
         </code></figure>
+    <section>
+        <a name="kvs"></a><h3>KVS JSON</h3>
+        <p>Simple chain-oriented API service implementation:</p>
+        <figure><code>
+ $ curl -X GET http://localhost:8005/rest/kvs/0/erp/group
+ {"\/erp\/group":[{"name":"Quanterall","url":"quanterall.com","location":[],"type":[]}]}
+        </code></figure>
+        <figure><code>
+ $ curl -i -X GET http://localhost:8005/rest/kvs/0/plm/FinaTech-Stamps/income
+ HTTP/1.1 200 OK
+ content-length: 1718
+ content-type: application/json
+ date: Fri, 04 Oct 2019 15:58:16 GMT
+ server: Cowboy
+
+ {"\/plm\/FinaTech-Stamps\/income":[{"invoice":"APR-2018-PAY-FTST","account":[],
+ "subaccount":[],"volume":{"fraction":0,"digits":12000},"price":{"fraction":0,
+ "digits":1},"instrument":"USD","type":"crypto","from":[],"to":[]},{"invoice":
+ "AUG-2018-PAY-FTST","account":[],"subaccount":[],"volume":{"fraction":0,
+ "digits":12000},"price":{"fraction":0,"digits":1},"instrument":"USD","type":
+ "crypto","from":[],"to":[]},{"invoice":"FEB-2018-PAY-FTST","account":[],
+ "subaccount":[],"volume":{"fraction":0,"digits":7000},"price":{"fraction":0,
+ "digits":1},"instrument":"USD","type":"crypto","from":[],"to":[]},{"invoice":
+ "JAN-2018-PAY-FTST","account":[],"subaccount":[],"volume":{"fraction":0,"digits":
+ 5000},"price":{"fraction":0,"digits":1},"instrument":"USD","type":"crypto","from":
+ [],"to":[]},{"invoice":"JUL-2018-PAY-FTST","account":[],"subaccount":[],"volume":
+ {"fraction":0,"digits":10000},"price":{"fraction":0,"digits":1},"instrument":
+ "USD","type":"crypto","from":[],"to":[]},{"invoice":"JUN-2018-PAY-FTST",
+ "account":[],"subaccount":[],"volume":{"fraction":0,"digits":10000},"price":
+ {"fraction":0,"digits":1},"instrument":"USD","type":"crypto","from":[],"to":[]},
+ {"invoice":"MAR-2018-PAY-FTST","account":[],"subaccount":[],"volume":
+ {"fraction":0,"digits":10000},"price":{"fraction":0,"digits":1},"instrument":
+ "USD","type":"crypto","from":[],"to":[]},{"invoice":"MAY-2018-PAY-FTST",
+ "account":[],"subaccount":[],"volume":{"fraction":0,"digits":15000},
+ "price":{"fraction":0,"digits":1},"instrument":"USD","type":"crypto",
+ "from":[],"to":[]},{"invoice":"SEP-2018-PAY-FTST","account":[],"subaccount":
+ [],"volume":{"fraction":0,"digits":15000},"price":{"fraction":0,"digits":1},
+ "instrument":"USD","type":"crypto","from":[],"to":[]}]}
+        </code></figure>
     </section>
     <section>
         <h3>Modules</h3>

+ 6 - 0
src/money.erl

@@ -0,0 +1,6 @@
+-module(money).
+-include_lib("dec/include/dec.hrl").
+-compile({parse_transform, rest}).
+-compile(export_all).
+-rest_record(money).
+new() -> #money{}.

+ 2 - 3
src/rest.erl

@@ -45,7 +45,7 @@ record_field({typed_record_field, {record_field,_,{atom, _, Field},_}, _}) -> Fi
 record_fields(RecordName, [{attribute, _, record, {RecordName, Fields}} | _Forms]) ->
     [record_field(Field) || Field <- Fields];
 record_fields(RecordName, [_ | Forms]) -> record_fields(RecordName, Forms);
-record_fields(RecordName, []) -> [].
+record_fields(__cordName, []) -> [].
 
 last_export_line(Exports) ->
     case lists:reverse(Exports) of
@@ -166,6 +166,5 @@ to_binary(A) when is_atom(A) -> atom_to_binary(A,latin1);
 to_binary(B) when is_binary(B) -> B;
 to_binary(I) when is_integer(I) -> to_binary(integer_to_list(I));
 to_binary(F) when is_float(F) -> float_to_binary(F,[{decimals,9},compact]);
-to_binary(L) when is_list(L) ->  iolist_to_binary(L);
-to_binary({money,_,_}) -> <<"money">>.
+to_binary(L) when is_list(L) ->  iolist_to_binary(L).
 

+ 11 - 7
src/rest_kvs.erl

@@ -46,6 +46,8 @@ content_types_provided(#{bindings := #{resource := Module}} = Req, State) ->
          true  -> [{<<"text/html">>, to_html},
                    {<<"application/json">>, to_json}] end, Req, State}.
 
+% TODO: HTML render broken!
+
 to_html(#{bindings := #{resource := Module, id := Id}} = Req, State) ->
     Body = case Id of
                Id when Id==[];Id==undefined -> [ rest_kvs:to_html(Module, Resource) || Resource <- rest_kvs:get(Module,Id) ];
@@ -57,13 +59,15 @@ to_html(#{bindings := #{resource := Module, id := Id}} = Req, State) ->
 
 default_html_layout(Body) -> [<<"<html><body>">>, Body, <<"</body></html>">>].
 
+% JSON seems fine
+
 to_json(#{bindings := #{resource := Module, id := Id}} = Req, State) ->
     {ok,Resource} = kvs:get(c(Module),c(Id)),
     Type = element(1,Resource),
-    {iolist_to_binary(?REST_JSON:encode(Type:to_json(Resource))), Req, State};
+    {iolist_to_binary([?REST_JSON:encode(Type:to_json(Resource)),"\n"]), Req, State};
 to_json(#{bindings := #{resource := Module}} = Req, State) ->
     Fold = [ begin M = element(1,Resource), M:to_json(Resource) end || Resource <- kvs:all(c(Module))],
-    {iolist_to_binary(?REST_JSON:encode([{Module,Fold}])), Req, State}.
+    {iolist_to_binary([?REST_JSON:encode([{Module,Fold}]),"\n"]), Req, State}.
 
 content_types_accepted(Req, State) ->
   {[{<<"application/x-www-form-urlencoded">>, handle_urlencoded_data},
@@ -97,11 +101,11 @@ handle_data(Mod, Id, Data, Req) ->
                 true  -> rest_kvs:validate(Mod,Id, Data);
                 false -> default_validate(Mod, Id, Data, Req) end,
     case {Valid, Id} of
-        {false, _}         -> false;
-        {true,  <<"undefined">>} -> rest_kvs:post(Type,Mod,Data);
-        {true,  _}         -> case application:get_env(rest,custom_put,false) of
-                                  true  -> Type:put(Mod, Id, Data);
-                                  false -> default_put(Type, Mod, Id, Data, Req) end
+         {false, _}         -> false;
+         {true,  <<"undefined">>} -> rest_kvs:post(Type,Mod,Data);
+         {true,  _}         -> case application:get_env(rest,custom_put,false) of
+                                   true  -> Type:put(Mod, Id, Data);
+                                   false -> default_put(Type, Mod, Id, Data, Req) end
     end.
 
 default_put(Type, Mod, Id, Data, Req) when is_map(Data) -> default_put(Type, Mod, Id, maps:to_list(Data), Req);