|
| 1 | +%% Feel free to use, reuse and abuse the code in this file. |
| 2 | + |
| 3 | +%% @doc Hello world handler. |
| 4 | +-module(toppage_handler). |
| 5 | + |
| 6 | +-export([init/3]). |
| 7 | +-export([content_types_provided/2]). |
| 8 | +-export([hello_to_html/2]). |
| 9 | +-export([hello_to_json/2]). |
| 10 | +-export([hello_to_text/2]). |
| 11 | + |
| 12 | +init(_Transport, _Req, []) -> |
| 13 | + {upgrade, protocol, cowboy_http_rest}. |
| 14 | + |
| 15 | +content_types_provided(Req, State) -> |
| 16 | + {[ |
| 17 | + {<<"text/html">>, hello_to_html}, |
| 18 | + {<<"application/json">>, hello_to_json}, |
| 19 | + {<<"text/plain">>, hello_to_text} |
| 20 | + ], Req, State}. |
| 21 | + |
| 22 | +hello_to_html(Req, State) -> |
| 23 | + Body = <<"<html> |
| 24 | +<head> |
| 25 | + <meta charset=\"utf-8\"> |
| 26 | + <title>REST Hello World!</title> |
| 27 | +</head> |
| 28 | +<body> |
| 29 | + <p>REST Hello World as HTML!</p> |
| 30 | +</body> |
| 31 | +</html>">>, |
| 32 | + {Body, Req, State}. |
| 33 | + |
| 34 | +hello_to_json(Req, State) -> |
| 35 | + Body = <<"{\"rest\": \"Hello World!\"}">>, |
| 36 | + {Body, Req, State}. |
| 37 | + |
| 38 | +hello_to_text(Req, State) -> |
| 39 | + {<<"REST Hello World as text!">>, Req, State}. |
0 commit comments