toppage_handler.erl 876 B

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