Integration with web servers
We can use Plug.Cowboy
in a similar way to Chapter 3 to build a server that dispatches the GET /greet
calls to the ExampleController
module. Let’s start by creating an iex
session with the goldcrest
app loaded. We can then require the example router and controller files to use them as part of our HTTP server:
$ iex -S mix iex> Code.require_file("./lib/goldcrest/support/example_controller.ex") iex> Code.require_file("./lib/goldcrest/support/example_router.ex") iex> Mix.install([{:plug_cowboy, "~> 2.0"}]) iex> opts = [ ...> scheme: :http, ...> plug: Goldcrest.ExampleRouter, ...> options: [port: 4040] ...> ] iex> %{start: {mod, fun, args}} = Plug.Cowboy.child_spec(opts) iex> apply(mod, fun, args) {:ok, #PID<_,_,_>}
Calling the preceding list of functions from iex
starts a Cowboy HTTP server on port 4040
. We can now use curl
to...