toppage_h.erl 606 B

12345678910111213141516171819202122
  1. %% Feel free to use, reuse and abuse the code in this file.
  2. %% @doc GET echo handler.
  3. -module(toppage_h).
  4. -export([init/2]).
  5. init(Req0, Opts) ->
  6. Method = cowboy_req:method(Req0),
  7. #{echo := Echo} = cowboy_req:match_qs([{echo, [], undefined}], Req0),
  8. Req = echo(Method, Echo, Req0),
  9. {ok, Req, Opts}.
  10. echo(<<"GET">>, undefined, Req) ->
  11. cowboy_req:reply(400, #{}, <<"Missing echo parameter.">>, Req);
  12. echo(<<"GET">>, Echo, Req) ->
  13. cowboy_req:reply(200, #{
  14. <<"content-type">> => <<"text/plain; charset=utf-8">>
  15. }, Echo, Req);
  16. echo(_, _, Req) ->
  17. %% Method not allowed.
  18. cowboy_req:reply(405, Req).