defmodule <%= inspect @module %> do @moduledoc false use Plug.Router alias <%= inspect @simulator.namespace %>.StateServer alias <%= inspect @simulator.namespace %>.WebServer.Responses plug :match plug :fetch_query_params plug Plug.Parsers, parsers: [:json], pass: ["*/*"], json_decoder: Jason <%= if @simulator.options.response_stubs? do %> plug :return_stubbed_responses <% end %> plug :dispatch def call(conn, opts) do {state_server, opts} = Keyword.pop!(opts, :state_server) conn |> assign(:state_server, state_server) |> super(opts) end get "/hello"<%= if @simulator.options.response_stubs? do %>, assigns: %{endpoint_id: :greeting}<% end %> do greeting = StateServer.get_greeting(conn.assigns.state_server) send_resp(conn, 200, Responses.render_greeting(greeting)) end <%= if @simulator.options.response_stubs? do %> defp return_stubbed_responses(conn, _opts) do endpoint_id = fetch_endpoint_id_from_assigns!(conn) case StateServer.get_response_stub(conn.assigns.state_server, endpoint_id) do nil -> conn :internal_server_error -> conn |> send_resp(500, "Internal Server Error") |> halt() :invalid_response -> conn |> send_resp(200, "Invalid") |> halt() end end defp fetch_endpoint_id_from_assigns!(conn) do case Map.fetch(conn.assigns, :endpoint_id) do {:ok, endpoint_id} when is_atom(endpoint_id) -> endpoint_id _ -> path = Plug.Router.match_path(conn) method = conn.method |> to_string() |> String.downcase() raise """ endpoint_id must be added to the assigns of the route definition #{method} #{inspect(path)}, assigns: %{endpoint_id: :my_endpoint_id} do #... end """ end end <% end %> end