You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 line
1.9 KiB

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