rest_forbidden_resource.erl 974 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. -module(rest_forbidden_resource).
  2. -export([init/3, rest_init/2, allowed_methods/2, forbidden/2,
  3. content_types_provided/2, content_types_accepted/2,
  4. post_is_create/2, create_path/2, to_text/2, from_text/2]).
  5. init(_Transport, _Req, _Opts) ->
  6. {upgrade, protocol, cowboy_http_rest}.
  7. rest_init(Req, [Forbidden]) ->
  8. {ok, Req, Forbidden}.
  9. allowed_methods(Req, State) ->
  10. {['GET', 'HEAD', 'POST'], Req, State}.
  11. forbidden(Req, State=true) ->
  12. {true, Req, State};
  13. forbidden(Req, State=false) ->
  14. {false, Req, State}.
  15. content_types_provided(Req, State) ->
  16. {[{{<<"text">>, <<"plain">>, []}, to_text}], Req, State}.
  17. content_types_accepted(Req, State) ->
  18. {[{{<<"text">>, <<"plain">>, []}, from_text}], Req, State}.
  19. post_is_create(Req, State) ->
  20. {true, Req, State}.
  21. create_path(Req, State) ->
  22. {Path, Req2} = cowboy_http_req:raw_path(Req),
  23. {Path, Req2, State}.
  24. to_text(Req, State) ->
  25. {<<"This is REST!">>, Req, State}.
  26. from_text(Req, State) ->
  27. {true, Req, State}.