forked from cybergrind/rebar-templates
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cowboy_http.erl
36 lines (32 loc) · 1.25 KB
/
cowboy_http.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
%%%-------------------------------------------------------------------
%%% @author {{author_name}} <{{author_email}}>
%%% @copyright (C) {{copyright_year}}, {{copyright_owner}}
%%% @doc
%%%
%%% @end
%%% Created : {{now_ts}} by {{author_name}} <{{author_email}}>
%%%-------------------------------------------------------------------
-module('{{handlerid}}_http').
-export([init/2]).
%%%===================================================================
%%% Cowboy callbacks
%%%===================================================================
init(Req, Opts) ->
Req2 = handle(Req),
{ok, Req2, Opts}.
%%%===================================================================
%%% Internal functions
%%%===================================================================
handle(Req) ->
Method = cowboy_req:method(Req),
#{echo := Echo} = cowboy_req:match_qs([echo], Req),
echo(Method, Echo, Req).
echo(<<"GET">>, undefined, Req) ->
cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);
echo(<<"GET">>, Echo, Req) ->
cowboy_req:reply(200, [
{<<"content-type">>, <<"text/plain; charset=utf-8">>}
], Echo, Req);
echo(_, _, Req) ->
% Method not allowed.
cowboy_req:reply(405, Req).