0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 04:30:25 +00:00
cowboy/examples/echo_get/src/toppage_handler.erl

23 lines
592 B
Erlang
Raw Normal View History

2012-07-21 21:46:21 +02:00
%% Feel free to use, reuse and abuse the code in this file.
%% @doc GET echo handler.
-module(toppage_handler).
-export([init/2]).
2012-07-21 21:46:21 +02:00
init(Req, Opts) ->
Method = cowboy_req:method(Req),
#{echo := Echo} = cowboy_req:match_qs([echo], Req),
Req2 = echo(Method, Echo, Req),
{ok, Req2, Opts}.
2012-07-21 21:46:21 +02:00
echo(<<"GET">>, undefined, Req) ->
2012-08-27 13:28:57 +02:00
cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);
echo(<<"GET">>, Echo, Req) ->
2016-06-08 20:18:09 +02:00
cowboy_req:reply(200, #{
<<"content-type">> => <<"text/plain; charset=utf-8">>
}, Echo, Req);
2012-07-21 21:46:21 +02:00
echo(_, _, Req) ->
%% Method not allowed.
2012-08-27 13:28:57 +02:00
cowboy_req:reply(405, Req).