rest_stream_response_app.erl 901 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. %% Feel free to use, reuse and abuse the code in this file.
  2. %% @private
  3. -module(rest_stream_response_app).
  4. -behaviour(application).
  5. %% API.
  6. -export([start/2]).
  7. -export([stop/1]).
  8. %% API.
  9. start(_Type, _Args) ->
  10. Table = ets:new(stream_tab, []),
  11. generate_rows(Table, 1000),
  12. Dispatch = cowboy_router:compile([
  13. {'_', [
  14. {"/[:v1]", [{v1, int}], toppage_handler, Table}
  15. ]}
  16. ]),
  17. {ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
  18. {env, [{dispatch, Dispatch}]}
  19. ]),
  20. rest_stream_response_sup:start_link().
  21. stop(_State) ->
  22. ok.
  23. generate_rows(_Table, 0) -> ok;
  24. generate_rows(Table, N) ->
  25. ets:insert(Table, {key(), val(), val()}),
  26. generate_rows(Table, N - 1).
  27. key() -> key(10).
  28. key(N) -> key(<< (random:uniform(26) - 1) >>, N - 1).
  29. key(Acc, 0) -> binary_part(base64:encode(Acc), 0, 8);
  30. key(Acc, N) -> key(<< Acc/binary, (random:uniform(26) - 1) >>, N - 1).
  31. val() -> random:uniform(50).