|
| 1 | +# See LICENSE for licensing information. |
| 2 | + |
| 3 | +# Plain HTTP handlers. |
| 4 | +define tpl_cowboy.http |
| 5 | +-module($(n)). |
| 6 | +-behavior(cowboy_handler). |
| 7 | + |
| 8 | +-export([init/2]). |
| 9 | + |
| 10 | +init(Req, State) -> |
| 11 | + {ok, Req, State}. |
| 12 | +endef |
| 13 | + |
| 14 | +# Loop handlers. |
| 15 | +define tpl_cowboy.loop |
| 16 | +-module($(n)). |
| 17 | +-behavior(cowboy_loop). |
| 18 | + |
| 19 | +-export([init/2]). |
| 20 | +-export([info/3]). |
| 21 | + |
| 22 | +init(Req, State) -> |
| 23 | + {cowboy_loop, Req, State, 5000, hibernate}. |
| 24 | + |
| 25 | +info(_Info, Req, State) -> |
| 26 | + {ok, Req, State, hibernate}. |
| 27 | +endef |
| 28 | + |
| 29 | +# REST handlers. |
| 30 | +define tpl_cowboy.rest |
| 31 | +-module($(n)). |
| 32 | +-behavior(cowboy_rest). |
| 33 | + |
| 34 | +-export([init/2]). |
| 35 | +-export([content_types_provided/2]). |
| 36 | +-export([to_html/2]). |
| 37 | + |
| 38 | +init(Req, State) -> |
| 39 | + {cowboy_rest, Req, State}. |
| 40 | + |
| 41 | +content_types_provided(Req, State) -> |
| 42 | + {[ |
| 43 | + {{<<"text">>, <<"html">>, '*'}, to_html} |
| 44 | + ], Req, State}. |
| 45 | + |
| 46 | +to_html(Req, State) -> |
| 47 | + {<<"<html><body>This is REST!</body></html>">>, Req, State}. |
| 48 | +endef |
| 49 | + |
| 50 | +# Websocket handlers. |
| 51 | +define tpl_cowboy.ws |
| 52 | +-module($(n)). |
| 53 | +-behavior(cowboy_websocket). |
| 54 | + |
| 55 | +-export([init/2]). |
| 56 | +-export([websocket_init/1]). |
| 57 | +-export([websocket_handle/2]). |
| 58 | +-export([websocket_info/2]). |
| 59 | + |
| 60 | +init(Req, State) -> |
| 61 | + {cowboy_websocket, Req, State}. |
| 62 | + |
| 63 | +websocket_init(State) -> |
| 64 | + {ok, State}. |
| 65 | + |
| 66 | +websocket_handle({text, Data}, State) -> |
| 67 | + {reply, {text, Data}, State}; |
| 68 | +websocket_handle({binary, Data}, State) -> |
| 69 | + {reply, {binary, Data}, State}; |
| 70 | +websocket_handle(_Frame, State) -> |
| 71 | + {ok, State}. |
| 72 | + |
| 73 | +websocket_info(_Info, State) -> |
| 74 | + {ok, State}. |
| 75 | +endef |
0 commit comments