servercorselixircowboyplug

How would I allow CORS with plug_cowboy in Elixir?


I'm trying to access these endpoints by making API calls elsewhere, how can I allow CORS for this? I'm running this on localhost:4001, and making the API calls from localhost:3000 (react). Thanks in advance. If you need any extra info (or files), please feel free to ask.

defmodule Api.Endpoint do
  @moduledoc """
  A plug that parses requests as JSON,
  dispatches responses and
  makes necessary changes elsewhere.
  """

  use Plug.Router

  plug Plug.Logger

  plug :match
  # Using Poison for JSON decoding
  plug(Plug.Parsers, parsers: [:json], json_decoder: Poison)
  plug :dispatch

  get "/ping" do
    send_resp(conn, 200, Poison.encode!(%{response: "pong!"}))
  end

  post "/events" do
    {status, body} =
      case conn.body_params do
        %{"events" => events} -> {200, process_events(events)}
        _ -> {422, missing_events()}
      end

    send_resp(conn, status, body)
  end

  defp process_events(events) when is_list(events) do
    Poison.encode!(%{response: "Received Events!"})
  end

  defp process_events(_) do
    Poison.encode!(%{response: "Please Send Some Events!"})
  end

  defp missing_events do
    Poison.encode!(%{error: "Expected Payload: { 'events': [...] }"})
  end

  match _ do
    send_resp(conn, 404, "oops... Nothing here :(")
  end
end

Solution

  • Based on you code something like this using corsica as @WeezHard said

    defmodule Api.CORS do
      use Corsica.Router,
      origins: ["http://localhost:3000"],
      allow_credentials: true,
      max_age: 600
    
      resource "/public/*", origins: "*"
      resource "/*"
    end
    

    And then in your endpoint

    defmodule Api.Endpoint do
      @moduledoc """
      A plug that parses requests as JSON,
      dispatches responses and
      makes necessary changes elsewhere.
      """
    
      use Plug.Router
    
      plug Plug.Logger
      plug Api.CORS
      ...
    end