http_server.erl 3.4 KB

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