toppage_handler.erl 591 B

12345678910111213141516171819202122
  1. %% Feel free to use, reuse and abuse the code in this file.
  2. %% @doc GET echo handler.
  3. -module(toppage_handler).
  4. -export([init/2]).
  5. init(Req, Opts) ->
  6. Method = cowboy_req:method(Req),
  7. #{echo := Echo} = cowboy_req:match_qs([echo], Req),
  8. Req2 = echo(Method, Echo, Req),
  9. {ok, Req2, 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).