Browse Source

wip:

* update to cowboy2
* fix dialyzer
* not tested
Namdak Tonpa 5 years ago
parent
commit
c52c7f2311
5 changed files with 23 additions and 20 deletions
  1. 1 1
      mix.exs
  2. 1 1
      src/rest.app.src
  3. 12 2
      src/rest_app.erl
  4. 9 9
      src/rest_cowboy.erl
  5. 0 7
      src/rest_sup.erl

+ 1 - 1
mix.exs

@@ -3,7 +3,7 @@ defmodule REST.Mixfile do
 
   def project do
     [app: :rest,
-     version: "5.10.0",
+     version: "5.10.1",
      description: "REST erlang interface generator",
      package: package]
   end

+ 1 - 1
src/rest.app.src

@@ -1,6 +1,6 @@
 {application, rest, [
     {description,  "REST Yoctoframework"},
-    {vsn,          "5.10"},
+    {vsn,          "5.10.1"},
     {applications, [kernel, stdlib]},
     {modules, []},
     {registered,   []},

+ 12 - 2
src/rest_app.erl

@@ -1,6 +1,16 @@
 -module(rest_app).
 -behaviour(application).
--export([start/2, stop/1]).
+-behaviour(supervisor).
+-export([init/1,start/2, stop/1]).
 
-start(_StartType, _StartArgs) -> rest_sup:start_link().
+init([]) -> {ok, {{one_for_one, 5, 10}, []}}.
 stop(_State) -> ok.
+start(_StartType, _StartArgs) ->
+   cowboy:start_tls(http,n2o_cowboy:env(rest),
+                 #{env=>#{dispatch=> points() }}),
+   supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+points() -> cowboy_router:compile([{'_', [
+             {"/rest/:resource",     rest_cowboy, []},
+             {"/rest/:resource/:id", rest_cowboy, []}
+            ]}]).

+ 9 - 9
src/rest_cowboy.erl

@@ -20,10 +20,10 @@ rest_init(Req, _Opts) ->
     {ok, Req4, #st{resource_module = Module, resource_id = Id}}.
 
 resource_exists(Req, #st{resource_module = undefined} = State)       -> {false, Req, State};
-resource_exists(Req, #st{resource_id     = undefined} = State)       -> {true, Req, State};
+resource_exists(Req, #st{resource_id     = <<"undefined">>} = State)       -> {true, Req, State};
 resource_exists(Req, #st{resource_module = M, resource_id = Id} = S) -> {M:exists(Id), Req, S}.
 
-allowed_methods(Req, #st{resource_id = undefined} = State) -> {[<<"GET">>, <<"POST">>], Req, State};
+allowed_methods(Req, #st{resource_id = <<"undefined">>} = State) -> {[<<"GET">>, <<"POST">>], Req, State};
 allowed_methods(Req, State)                                -> {[<<"GET">>, <<"PUT">>, <<"DELETE">>], Req, State}.
 
 content_types_provided(Req, #st{resource_module = M} = State) ->
@@ -34,7 +34,7 @@ content_types_provided(Req, #st{resource_module = M} = State) ->
 
 to_html(Req, #st{resource_module = M, resource_id = Id} = State) ->
     Body = case Id of
-               undefined -> [M:to_html(Resource) || Resource <- M:get()];
+               <<"undefined">> -> [M:to_html(Resource) || Resource <- M:get()];
                _ -> M:to_html(M:get(Id)) end,
     Html = case erlang:function_exported(M, html_layout, 2) of
                true  -> M:html_layout(Req, Body);
@@ -45,7 +45,7 @@ default_html_layout(Body) -> [<<"<html><body>">>, Body, <<"</body></html>">>].
 
 to_json(Req, #st{resource_module = M, resource_id = Id} = State) ->
     Struct = case Id of
-                 undefined -> [{M, [ M:to_json(Resource) || Resource <- M:get() ] } ];
+                 <<"undefined">> -> [{M, [ M:to_json(Resource) || Resource <- M:get() ] } ];
                  _         -> M:to_json(M:get(Id)) end,
     {iolist_to_binary(?REST_JSON:encode(Struct)), Req, State}.
 
@@ -67,7 +67,7 @@ handle_data(Mod, Id, Data) ->
                 false -> default_validate(Mod, Id, Data) end,
     case {Valid, Id} of
         {false, _}         -> false;
-        {true,  undefined} -> Mod:post(Data);
+        {true,  <<"undefined">>} -> Mod:post(Data);
         {true,  _}         -> case erlang:function_exported(Mod, put, 2) of
                                   true  -> Mod:put(Id, Data);
                                   false -> default_put(Mod, Id, Data) end
@@ -87,10 +87,10 @@ default_validate(Mod, Id, Data) ->
                   false -> true end,
     validate_match(Mod, Id, Allowed, proplists:get_value(<<"id">>, Data)).
 
-validate_match(_Mod, undefined, true, undefined) -> false;
-validate_match(_Mod, undefined, true, <<"">>)    -> false;
-validate_match( Mod, undefined, true, NewId)     -> not Mod:exists(NewId);
-validate_match(_Mod,       _Id, true, undefined) -> true;
+validate_match(_Mod, <<"undefined">>, true, <<"undefined">>) -> false;
+validate_match(_Mod, <<"undefined">>, true, <<"">>)    -> false;
+validate_match( Mod, <<"undefined">>, true, NewId)     -> not Mod:exists(NewId);
+validate_match(_Mod,       _Id, true, <<"undefined">>) -> true;
 validate_match(_Mod,        Id, true, Id)        -> true;
 validate_match( Mod,       _Id, true, NewId)     -> not Mod:exists(NewId);
 validate_match(   _,         _,    _, _)         -> false.

+ 0 - 7
src/rest_sup.erl

@@ -1,7 +0,0 @@
--module(rest_sup).
--behaviour(supervisor).
--export([start_link/0, init/1]).
-
-start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []).
-init([]) -> {ok, {{one_for_one, 5, 10}, []}}.
-