forked from inaka/cowboy_swagger
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_echo_handler.erl
77 lines (69 loc) · 1.82 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
-module(example_echo_handler).
-behaviour(cowboy_rest).
-include_lib("mixer/include/mixer.hrl").
-mixin([
{example_default,
[
init/2,
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",
responses => #{
<<"200">> => #{
description => <<"Gets echo var from the server 200 OK">>,
content => #{
'text/plain' =>
#{schema => #{
type => string
}
}
}
}
}
},
put =>
#{tags => ["echo"],
description => "Sets echo var in the server",
parameters => [
#{name => <<"echo">>,
description => <<"Echo message">>,
in => <<"path">>,
required => false,
schema =>
#{type => string,
example => <<"Hello, World!">>
}
}
]
}
},
[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 = cowboy_req:binding(echo, Req, ""),
application:set_env(example, echo, Echo),
Body = [<<"You put an echo! ">> , Echo],
Req1 = cowboy_req:set_resp_body(Body, Req),
{true, Req1, State}.