Skip to content

Instantly share code, notes, and snippets.

@matheusca
Created October 17, 2013 20:56
Show Gist options
  • Save matheusca/7032092 to your computer and use it in GitHub Desktop.
Save matheusca/7032092 to your computer and use it in GitHub Desktop.
defmodule Cowboy do
@moduledoc """
Starts cowboy instance.
"""
def start(name, config) do
:application.start(:crypto)
:application.start(:asn1)
:application.start(:public_key)
IO.puts "test 1"
:ok = :application.start(:ssl)
IO.puts "test 2"
:application.start(:ranch)
:application.start(:cowlib)
:application.start(:cowboy)
{:webserver, web_server_config} = :lists.keyfind(:webserver, 1, config)
{_, _host} = :lists.keyfind(:http_host, 1, web_server_config)
{_, port} = :lists.keyfind(:http_port, 1, web_server_config)
{_, acceptors} = :lists.keyfind(:acceptors, 1, web_server_config)
{_, ssl} = :lists.keyfind(:ssl, 1, web_server_config)
dispatch = :cowboy_router.compile([{:_, [{:_, Handler.WeberReqHandler, name}]}])
case ssl do
true ->
{_, cacertifile} = :lists.keyfind(:cacertfile_path, 1, web_server_config)
{_, certfile} = :lists.keyfind(:certfile_path, 1, web_server_config)
{_, keyfile} = :lists.keyfind(:keyfile_path, 1, web_server_config)
{:ok, _} = :cowboy.start_https(:https, acceptors, [{:port, port}, {:cacertfile, cacertifile},
{:certfile,certfile}, {:keyfile, keyfile}],
[env: [dispatch: dispatch]])
_ ->
{:ok, _} = :cowboy.start_http(:http, acceptors, [port: port], [env: [dispatch: dispatch]])
end
case :lists.keyfind(:ws, 1, config) do
false -> :ok
{:ws, ws_config} ->
{_, ws_mod} = :lists.keyfind(:ws_mod, 1, ws_config)
{_, ws_port} = :lists.keyfind(:ws_port, 1, ws_config)
wsDispatch = :cowboy_router.compile([{:_, [{:_, Handler.WeberWebSocketHandler, {name, ws_mod}}]}])
{:ok, _} = :cowboy.start_http(:ws, acceptors, [port: ws_port], [env: [dispatch: wsDispatch]])
_ ->
:ok
end
end
end
☁ weber [refactor_handler] ⚡ mix test test/handler/handler_test.exs
Compiled lib/weber/cowboy.ex
Compiled lib/weber/app.ex
Compiled lib/weber/sup.ex
Compiled lib/weber.ex
Generated weber.app
test 1
1) test response body (HandlerWeberReqHandlerTest)
** (MatchError) no match of right hand side value: {:error, :econnrefused}
stacktrace:
test/handler/handler_test.exs:40: HandlerWeberReqHandlerTest.response/1
test/handler/handler_test.exs:19: HandlerWeberReqHandlerTest."test response body"/1
2) test response body 404 (HandlerWeberReqHandlerTest)
** (MatchError) no match of right hand side value: {:error, :econnrefused}
stacktrace:
test/handler/handler_test.exs:40: HandlerWeberReqHandlerTest.response/1
test/handler/handler_test.exs:33: HandlerWeberReqHandlerTest."test response body 404"/1
3) test response body with static file (HandlerWeberReqHandlerTest)
** (MatchError) no match of right hand side value: {:error, :econnrefused}
stacktrace:
test/handler/handler_test.exs:40: HandlerWeberReqHandlerTest.response/1
test/handler/handler_test.exs:26: HandlerWeberReqHandlerTest."test response body with static file"/1
Finished in 0.2 seconds (0.1s on load, 0.04s on tests)
3 tests, 3 failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment