forked from inaka/cowboy_swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_echo_handler.erl
61 lines (54 loc) · 1.47 KB
/
example_echo_handler.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-module(example_echo_handler).
-include_lib("mixer/include/mixer.hrl").
-mixin([
{example_default,
[
init/3,
rest_init/2,
content_types_accepted/2,
content_types_provided/2,
resource_exists/2
]}
]).
-export([ allowed_methods/2
, handle_put/2
, handle_get/2
]).
%trails
-behaviour(trails_handler).
-export([trails/0]).
trails() ->
Metadata =
#{get =>
#{tags => ["echo"],
description => "Gets echo var from the server",
produces => ["text/plain"]
},
put =>
#{tags => ["echo"],
description => "Sets echo var in the server",
produces => ["text/plain"],
parameters => [
#{name => <<"echo">>,
description => <<"Echo message">>,
in => <<"path">>,
required => false,
type => <<"string">>}
]
}
},
[trails:trail("/message/[:echo]", example_echo_handler, [], Metadata)].
%% cowboy
allowed_methods(Req, State) ->
{[<<"GET">>, <<"PUT">>, <<"HEAD">>], Req, State}.
%% internal
handle_get(Req, State) ->
Echo = application:get_env(example, echo, ""),
Body = [<<"You Get an echo!">> , Echo],
{Body, Req, State}.
handle_put(Req, State) ->
{Echo, Req1} = cowboy_req:binding(echo, Req, ""),
application:set_env(example, echo, Echo),
Body = [<<"You put an echo! ">> , Echo],
Req2 = cowboy_req:set_resp_body(Body, Req1),
{true, Req2, State}.