http_server.erl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. %%--------------------------------------------------------------------
  2. %% A Simple HTTP Server based cowboy
  3. %%
  4. %% It will deliver the http-request params to initialer process
  5. %%--------------------------------------------------------------------
  6. %%
  7. %% Author:wwhai
  8. %%
  9. -module(http_server).
  10. -behaviour(gen_server).
  11. -export([start_link/0]).
  12. -export([get_received_data/0]).
  13. -export([stop/1]).
  14. -export([code_change/3, handle_call/3, handle_cast/2, handle_info/2, init/1, terminate/2]).
  15. -define(HTTP_PORT, 9999).
  16. -define(HTTPS_PORT, 8888).
  17. -record(state, {}).
  18. %%--------------------------------------------------------------------
  19. %% APIs
  20. %%--------------------------------------------------------------------
  21. start_link() ->
  22. gen_server:start_link(?MODULE, [], []).
  23. init([]) ->
  24. EtsOptions = [named_table, public, set, {write_concurrency, true},
  25. {read_concurrency, true}],
  26. emqx_web_hook_http_test = ets:new(emqx_web_hook_http_test, EtsOptions),
  27. ok = start_http(?HTTP_PORT),
  28. ok = start_https(?HTTPS_PORT),
  29. {ok, #state{}}.
  30. handle_call(_Request, _From, State) ->
  31. {reply, ignored, State}.
  32. handle_cast(_Msg, State) ->
  33. {noreply, State}.
  34. handle_info(_Info, State) ->
  35. {noreply, State}.
  36. terminate(_Reason, _State) ->
  37. stop_http(),
  38. stop_https().
  39. code_change(_OldVsn, State, _Extra) ->
  40. {ok, State}.
  41. get_received_data() ->
  42. ets:tab2list(emqx_web_hook_http_test).
  43. stop(Pid) ->
  44. ok = gen_server:stop(Pid).
  45. %%--------------------------------------------------------------------
  46. %% Callbacks
  47. %%--------------------------------------------------------------------
  48. start_http(Port) ->
  49. {ok, _Pid1} = cowboy:start_clear(http, [{port, Port}], #{
  50. env => #{dispatch => compile_router()}
  51. }),
  52. io:format(standard_error, "[TEST LOG] Start http server on 9999 successfully!~n", []).
  53. start_https(Port) ->
  54. Path = emqx_ct_helpers:deps_path(emqx_web_hook, "test/emqx_web_hook_SUITE_data/"),
  55. SslOpts = [{keyfile, Path ++ "/server-key.pem"},
  56. {cacertfile, Path ++ "/ca.pem"},
  57. {certfile, Path ++ "/server-cert.pem"}],
  58. {ok, _Pid2} = cowboy:start_tls(https, [{port, Port}] ++ SslOpts,
  59. #{env => #{dispatch => compile_router()}}),
  60. io:format(standard_error, "[TEST LOG] Start https server on 8888 successfully!~n", []).
  61. stop_http() ->
  62. ok = cowboy:stop_listener(http),
  63. io:format("[TEST LOG] Stopped http server on 9999").
  64. stop_https() ->
  65. ok = cowboy:stop_listener(https),
  66. io:format("[TEST LOG] Stopped https server on 8888").
  67. compile_router() ->
  68. {ok, _} = application:ensure_all_started(cowboy),
  69. cowboy_router:compile([
  70. {'_', [{"/", ?MODULE, #{}}]}
  71. ]).
  72. init(Req, State) ->
  73. Method = cowboy_req:method(Req),
  74. Headers = cowboy_req:headers(Req),
  75. [Params] = case Method of
  76. <<"GET">> -> cowboy_req:parse_qs(Req);
  77. <<"POST">> ->
  78. {ok, PostVals, _} = cowboy_req:read_urlencoded_body(Req),
  79. PostVals
  80. end,
  81. ets:insert(emqx_web_hook_http_test, {Params, Headers}),
  82. {ok, reply(Req, ok), State}.
  83. reply(Req, ok) ->
  84. cowboy_req:reply(200, #{<<"content-type">> => <<"text/plain">>}, <<"ok">>, Req);
  85. reply(Req, error) ->
  86. cowboy_req:reply(404, #{<<"content-type">> => <<"text/plain">>}, <<"deny">>, Req).