選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

86 行
1.9 KiB

  1. eWSrv
  2. =====
  3. An erlang web server
  4. Build
  5. -----
  6. $ rebar3 compile
  7. ## Usage
  8. ```sh
  9. $ rebar3 shell
  10. ```
  11. ```erlang
  12. %% starting elli
  13. 1 > {ok, Pid} = elli:start_link([{callback, elli_example_callback}, {port, 3000}]).
  14. ```
  15. ## Examples
  16. ### Callback Module
  17. The best source to learn how to write a callback module
  18. is [src/elli_example_callback.erl](src/elli_example_callback.erl) and
  19. its [generated documentation](doc/elli_example_callback.md). There are a bunch of examples used in the tests as well as
  20. descriptions of all the events.
  21. A minimal callback module could look like this:
  22. ```erlang
  23. -module(elli_minimal_callback).
  24. -export([handle/2, handle_event/3]).
  25. -include_lib("elli/include/eWSrv.hrl").
  26. -behaviour(wsHer).
  27. handle(Req, _Args) ->
  28. %% Delegate to our handler function
  29. handle(Req#req.method, wsReq:path(Req), Req).
  30. handle('GET', [<<"hello">>, <<"world">>], _Req) ->
  31. %% Reply with a normal response. `ok' can be used instead of `200'
  32. %% to signal success.
  33. {ok, [], <<"Hello World!">>};
  34. handle(_, _, _Req) ->
  35. {404, [], <<"Not Found">>}.
  36. %% @doc Handle request events, like request completed, exception
  37. %% thrown, client timeout, etc. Must return `ok'.
  38. handle_event(_Event, _Data, _Args) ->
  39. ok.
  40. ```
  41. ### Supervisor Childspec
  42. To add `elli` to a supervisor you can use the following example and adapt it to your needs.
  43. ```erlang
  44. -module(fancyapi_sup).
  45. -behaviour(supervisor).
  46. -export([start_link/0]).
  47. -export([init/1]).
  48. start_link() ->
  49. supervisor:start_link({local, ?MODULE}, ?MODULE, []).
  50. init([]) ->
  51. ElliOpts = [{callback, fancyapi_callback}, {port, 3000}],
  52. ElliSpec = {
  53. fancy_http,
  54. {elli, start_link, [ElliOpts]},
  55. permanent,
  56. 5000,
  57. worker,
  58. [elli]},
  59. {ok, {{one_for_one, 5, 10}, [ElliSpec]}}.
  60. ```
  61. ## Further Reading
  62. For more information about the features and design philosophy of `elli` check out the [overview](doc/README.md).