Hi!
I am trying to create a proxy server with elixir, plug and cowboy to filter out websocket messages from a particular application.
However, the source application relies on the Upgrade
header on the /
route to upgrade the connection to a websocket one and returns the index.html file if the header is not present. I have not been able to upgrade the connection conditionally using Plug and Cowboy. I tested by doing the following:
defmodule MyPlug do
import Plug.Conn
def init(options) do
options
end
def call(conn, opts) do
IO.puts("Proxying '#{conn.request_path}'")
case get_req_header(conn, "upgrade") do
["websocket"] ->
Plug.forward(conn, conn.path_info, WebSocketPlug, opts)
_http ->
conn
|> send_resp(200, "an html file")
end
end
end
And for the Websocket plug which is actually not one since it is a Cowboy handler:
defmodule WebSocketPlug do
@behaviour :cowboy_websocket
def init(request, _state) do
{:cowboy_websocket, request, %{}}
end
end
def websocket_handle({:text, _json}, state) do
{:reply, {:text, "hey!"}, state}
end
def websocket_info(info, state) do
{:reply, {:text, info}, state}
end
def websocket_init(state) do
{:ok, state}
end
end
Is there a way to transform my websocket module to a valid Plug handler ?
Thanks for your help!