Browse Source

fix cowboy 2.9.0, example

221V 1 year ago
parent
commit
db21119ec6

+ 9 - 0
ebin/rest.app

@@ -0,0 +1,9 @@
+{application, rest, [
+    {description,  "REST SXC"},
+    {vsn,          "2.9"},
+    {applications, [kernel, stdlib, public_key, asn1, ssl, crypto, syntax_tools, compiler, ranch, cowboy, n2o]},
+    {modules, [rest_app, rest_cowboy, rest_sup, rest]},
+    {registered,   [rest_sup]},
+    {mod, { rest_app, []}},
+    {env, []}
+]}.

+ 9 - 0
example/ebin/rest.app

@@ -0,0 +1,9 @@
+{application, rest, [
+    {description,  "REST SXC"},
+    {vsn,          "2.9"},
+    {applications, [kernel, stdlib, cowboy]},
+    {modules, []},
+    {registered,   []},
+    {mod, { rest_users, []}},
+    {env, []}
+]}.

+ 9 - 0
example/ebin/rest_users.app

@@ -0,0 +1,9 @@
+{application, rest_users, [
+    {description,  "REST SXC"},
+    {vsn,          "2.9"},
+    {applications, [kernel, stdlib, public_key, asn1, ssl, crypto, syntax_tools, compiler, ranch, cowboy, n2o]},
+    {modules, [rest_users]},
+    {registered,   []},
+    {mod, { rest_users, []}},
+    {env, []}
+]}.

+ 2 - 2
example/src/rest_users.app.src

@@ -1,8 +1,8 @@
 {application, rest_users, [
 {application, rest_users, [
     {description,  "REST SXC"},
     {description,  "REST SXC"},
     {vsn,          "2.9"},
     {vsn,          "2.9"},
-    {applications, [kernel, stdlib, cowboy]},
-    {modules, []},
+    {applications, [kernel, stdlib, public_key, asn1, ssl, crypto, syntax_tools, compiler, ranch, cowboy, n2o]},
+    {modules, [rest_users]},
     {registered,   []},
     {registered,   []},
     {mod, { rest_users, []}},
     {mod, { rest_users, []}},
     {env, []}
     {env, []}

+ 14 - 7
example/src/rest_users.erl

@@ -2,28 +2,35 @@
 -compile({parse_transform, rest}).
 -compile({parse_transform, rest}).
 
 
 -export([init/0, populate/1, new/0, exists/1, get/0, get/1, post/1, delete/1]).
 -export([init/0, populate/1, new/0, exists/1, get/0, get/1, post/1, delete/1]).
+-export([keys_allowed/1]).
+
 
 
 -record(user, {id, cn, name, type}).
 -record(user, {id, cn, name, type}).
 -rest_record(user).
 -rest_record(user).
 
 
+keys_allowed(Keys) ->
+  Allow = [<<"id">>, <<"test">>],
+  lists:all(fun(X) ->
+    lists:member(X, Allow)
+  end, Keys).
 
 
 new()                -> #user{}.
 new()                -> #user{}.
-init()               -> ets:new(users, [public, named_table, {keypos, #user.id}]).
-populate(Users)      -> ets:insert(users, Users).
+init()               -> ets:new(rest_users, [public, named_table, {keypos, #user.id}]).
+populate(Users)      -> ets:insert(rest_users, Users).
 
 
 exists(Id)           ->
 exists(Id)           ->
-  X = ets:member(users, erlang:binary_to_list(Id)),
+  X = ets:member(rest_users, erlang:binary_to_list(Id)),
   io:format("Member: ~p~n", [X]),
   io:format("Member: ~p~n", [X]),
   X.
   X.
 
 
-get()                -> ets:tab2list(users).
+get()                -> ets:tab2list(rest_users).
 
 
 get(Id)              ->
 get(Id)              ->
-  [U] = ets:lookup(users, erlang:binary_to_list(Id)),
+  [U] = ets:lookup(rest_users, erlang:binary_to_list(Id)),
   io:format("User: ~p~n", [U]),
   io:format("User: ~p~n", [U]),
   U.
   U.
 
 
-delete(Id)           -> ets:delete(users, erlang:binary_to_list(Id)).
-post(#user{} = User) -> ets:insert(users, User);
+delete(Id)           -> ets:delete(rest_users, erlang:binary_to_list(Id)).
+post(#user{} = User) -> ets:insert(rest_users, User);
 post(Data)           -> post(from_json(Data, #user{})).
 post(Data)           -> post(from_json(Data, #user{})).
 
 

+ 2 - 2
example/sys.config

@@ -1,7 +1,7 @@
 [
 [
 {n2o, [
 {n2o, [
-  {port, 8005},
-  {app, rest},
+  {port, 4000},
+  {app, rest_users},
   {formatter, n2o_bert},
   {formatter, n2o_bert},
   {protocols, [n2o_heart, n2o_nitro, n2o_ftp]},
   {protocols, [n2o_heart, n2o_nitro, n2o_ftp]},
   {session, n2o_session},
   {session, n2o_session},

+ 3 - 3
src/rest.app.src

@@ -1,9 +1,9 @@
 {application, rest, [
 {application, rest, [
     {description,  "REST SXC"},
     {description,  "REST SXC"},
     {vsn,          "2.9"},
     {vsn,          "2.9"},
-    {applications, [kernel, stdlib, cowboy]},
-    {modules, []},
-    {registered,   []},
+    {applications, [kernel, stdlib, public_key, asn1, ssl, crypto, syntax_tools, compiler, ranch, cowboy, n2o]},
+    {modules, [rest_app, rest_cowboy, rest_sup, rest]},
+    {registered,   [rest_sup]},
     {mod, { rest_app, []}},
     {mod, { rest_app, []}},
     {env, []}
     {env, []}
 ]}.
 ]}.

+ 14 - 11
src/rest_cowboy.erl

@@ -18,20 +18,21 @@ c(X) -> erlang:list_to_atom(erlang:binary_to_list(X)).
 
 
 
 
 rest_init(Req, _Opts) ->
 rest_init(Req, _Opts) ->
-  {Resource, Req1} = cowboy_req:binding(resource, Req),
+  Resource = cowboy_req:binding(resource, Req),
+  io:format("rest_init:~n ~p~n~p~n", [Resource, rest_module(Resource)]),
   Module = case rest_module(Resource) of
   Module = case rest_module(Resource) of
     {ok, M} -> M;
     {ok, M} -> M;
     _ -> undefined
     _ -> undefined
   end,
   end,
-  {Id, Req2} = cowboy_req:binding(id, Req1),
-  {Origin, Req3} = cowboy_req:header(<<"origin">>, Req2, <<"*">>),
-  Req4 = cowboy_req:set_resp_header(<<"Access-Control-Allow-Origin">>, Origin, Req3),
+  Id = cowboy_req:binding(id, Req),
+  Origin = cowboy_req:header(<<"origin">>, Req, <<"*">>),
+  Req2 = cowboy_req:set_resp_header(<<"Access-Control-Allow-Origin">>, Origin, Req),
   io:format("REST INIT~n"),
   io:format("REST INIT~n"),
-  {ok, Req4, #st{resource_module = Module, resource_id = Id}}.
+  {ok, Req2, #st{resource_module = Module, resource_id = Id}}.
 
 
 resource_exists(#{bindings := #{resource := Module, id := Id}} = Req, State) ->
 resource_exists(#{bindings := #{resource := Module, id := Id}} = Req, State) ->
   M = c(Module),
   M = c(Module),
-  io:format("EXISTS: ~p dymamic: ~p~n", [Id, M:exists(Id)]),
+  io:format("EXISTS: ~p dynamic: ~p~n", [Id, M:exists(Id)]),
   {M:exists(Id), Req, State};
   {M:exists(Id), Req, State};
 resource_exists(#{bindings := #{resource := Module}} = Req, State) ->
 resource_exists(#{bindings := #{resource := Module}} = Req, State) ->
   io:format("resource ~p: no-id~n", [Module]),
   io:format("resource ~p: no-id~n", [Module]),
@@ -40,10 +41,12 @@ resource_exists(#{bindings := #{id := _}} = Req, State) ->
   io:format("EXISTS id: true~n"),
   io:format("EXISTS id: true~n"),
   {true, Req, State}.
   {true, Req, State}.
 
 
-allowed_methods(#{bindings := #{resource := _}} = Req, State) ->
-  {[<<"GET">>, <<"POST">>], Req, State};
+
 allowed_methods(#{bindings := #{resource := _, id := _}} = Req, State) ->
 allowed_methods(#{bindings := #{resource := _, id := _}} = Req, State) ->
-  {[<<"GET">>, <<"PUT">>, <<"DELETE">>], Req, State}.
+  {[<<"GET">>, <<"PUT">>, <<"DELETE">>], Req, State};
+allowed_methods(#{bindings := #{resource := _}} = Req, State) ->
+  {[<<"GET">>, <<"POST">>], Req, State}.
+
 
 
 content_types_provided(#{bindings := #{resource := Module}} = Req, State) ->
 content_types_provided(#{bindings := #{resource := Module}} = Req, State) ->
   {case erlang:function_exported(c(Module), to_html, 1) of
   {case erlang:function_exported(c(Module), to_html, 1) of
@@ -66,7 +69,7 @@ to_html(#{bindings := #{resource := Module, id := Id}} = Req, State) ->
 
 
 
 
 default_html_layout(Body) ->
 default_html_layout(Body) ->
-  [<<"<html><body>">>, Body, <<"</body></html>">>].
+  [<<"<!DOCTYPE html><html><body>">>, Body, <<"</body></html>">>].
 
 
 to_json(#{bindings := #{resource := Module, id := Id}} = Req, State) ->
 to_json(#{bindings := #{resource := Module, id := Id}} = Req, State) ->
   io:format("~p ~p ~p~n", [?FUNCTION_NAME, Module, Id]),
   io:format("~p ~p ~p~n", [?FUNCTION_NAME, Module, Id]),
@@ -91,7 +94,7 @@ content_types_accepted(Req, State) ->
 handle_urlencoded_data(#{bindings := #{resource := Module}} = Req0, State) ->
 handle_urlencoded_data(#{bindings := #{resource := Module}} = Req0, State) ->
   {ok, Data1, Req} = cowboy_req:read_urlencoded_body(Req0),
   {ok, Data1, Req} = cowboy_req:read_urlencoded_body(Req0),
   io:format("FORM: ~p, Data1: ~p~n", [Module, Data1]),
   io:format("FORM: ~p, Data1: ~p~n", [Module, Data1]),
-  {handle_data(c(Module), [], Data1, Req), Req, State};
+  {handle_data(c(Module), [], Data1, Req0), Req, State};
 
 
 handle_urlencoded_data(#{bindings := #{resource := Module, id := Id}} = Req, State) ->
 handle_urlencoded_data(#{bindings := #{resource := Module, id := Id}} = Req, State) ->
   {ok, Data, Req2} = cowboy_req:read_urlencoded_body(Req),
   {ok, Data, Req2} = cowboy_req:read_urlencoded_body(Req),