Skip to content

Add websocket support with Gun #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ The Dispatcher runs as an application in which the `Dispatcher` module is overri

The disptacher is configured using the dispatcher.ex file in a [mu-project](https://github.com/mu-semtech/mu-project).

The basic (default) configuration of the mu-dispatcher is an Elixir module named `Dispatcher` which uses the `Matcher` functionality.
The basic (default) configuration of the mu-dispatcher is an Elixir module named `Dispatcher` which uses the `Matcher` functionality.
An empty set of accept types is required (`define_accept_types []`).

```elixir
Expand Down Expand Up @@ -251,6 +251,40 @@ If you need to access a part of the API, revert back to the array syntax and def

This specific implementation does require at least one subdomain and it will thus not match `redpencil.io`.

### Forwarding websockets

Dispatcher can forward websocket tunnels.

An example rule is as follows:

```elixir
match "/websocket" do
ws conn, "ws://push-service-ws/"
end
```

Any websocket connections on `/websocket` get's redirected to `/.mu/ws?target=<something>`.
The Gun library listens to websockets on `/.mu/ws` which makes everything work.
The `target` query parameter tells Gun what host to forward to.


Note: following redirects is not required in the websockets specs, and most browsers don't support this.

Example workarounds:
```js
async function createRedirectedWebsocket(url) {
// This prints an error to the console due to unexpected upgrade response
const resp = await fetch(url);
const ws_url = resp.url.replace(/^http/, 'ws');
return new WebSocket(ws_url);
}
```

With the 'ws' npm package it is possible to set a `followRedirects` flag.
```js
const WebSocket = require('ws')
const ws = new WebSocket('ws://localhost/ws2', options={'followRedirects': true});
```

### Fallback routes and 404 pages

Expand Down Expand Up @@ -447,7 +481,7 @@ Forwarding connections is built on top of `plug_mint_proxy` which uses the Mint

### Wiring with Plug
[Plug](https://github.com/elixir-plug/plug) expects call to be matched using its own matcher and dispatcher.
This library provides some extra support.
This library provides some extra support.
Although tying this in within Plug might be simple, the request is dispatched to our own matcher in [plug_router_dispatcher.ex](./lib/plug_router_dispatcher.ex).

### Header manipulation
Expand All @@ -470,5 +504,3 @@ High-level the dispatching works as follows:
4. For each (B) try to find a matched solution
5. If a solution is found, return it
6. If no solution is found, try to find a matched solution with the `last_call` option set to true


44 changes: 24 additions & 20 deletions lib/dispatcher.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
defmodule Dispatcher do
use Matcher

define_accept_types [
text: [ "text/*" ],
html: [ "text/html", "application/xhtml+html" ],
json: [ "application/json", "application/vnd.api+json" ]
]
define_accept_types(
text: ["text/*"],
html: ["text/html", "application/xhtml+html"],
json: ["application/json", "application/vnd.api+json"]
)

# get "/*_rest", %{ accept: %{ html: true } } do
# Proxy.forward conn, [], "http://static/ember-app/index.html"
Expand All @@ -16,35 +16,39 @@ defmodule Dispatcher do
# end

post "/hello/erika", %{} do
Plug.Conn.send_resp conn, 401, "FORBIDDEN"
Plug.Conn.send_resp(conn, 401, "FORBIDDEN")
end

# 200 microservice dispatching

match "/hello/erika", %{ accept: %{ json: true } } do
Plug.Conn.send_resp conn, 200, "{ \"message\": \"Hello Erika\" }"
match "/hello/erika", %{accept: %{json: true}} do
Plug.Conn.send_resp(conn, 200, "{ \"message\": \"Hello Erika\" }\n")
end

match "/hello/erika", %{ accept: %{ html: true } } do
Plug.Conn.send_resp conn, 200, "<html><head><title>Hello</title></head><body>Hello Erika</body></html>"
match "/hello/erika", %{accept: %{html: true}} do
Plug.Conn.send_resp(
conn,
200,
"<html><head><title>Hello</title></head><body>Hello Erika</body></html>"
)
end

# 404 routes

match "/hello/aad/*_rest", %{ accept: %{ json: true } } do
Plug.Conn.send_resp conn, 200, "{ \"message\": \"Hello Aad\" }"
match "/hello/aad/*_rest", %{accept: %{json: true}} do
Plug.Conn.send_resp(conn, 200, "{ \"message\": \"Hello Aad\" }")
end

match "/*_rest", %{ accept: %{ json: true }, last_call: true } do
Plug.Conn.send_resp conn, 404, "{ \"errors\": [ \"message\": \"Not found\", \"status\": 404 } ] }"
end
# Websocket example route
# This forwards to /ws?target=<...>
# Then forwards websocket from /ws?target=<...> to ws://push-service-ws/

match "/*_rest", %{ accept: %{ html: true }, last_call: true } do
Plug.Conn.send_resp conn, 404, "<html><head><title>Not found</title></head><body>No acceptable response found</body></html>"
match "/push-service/ws" do
ws conn, "ws://push-service-ws/"
end

match "/*_rest", %{ last_call: true } do
Plug.Conn.send_resp conn, 404, "No response found"
end

match "__", %{last_call: true} do
send_resp(conn, 404, "Route not found. See config/dispatcher.ex")
end
end
6 changes: 3 additions & 3 deletions lib/manipulators/add_x_rewrite_url_header.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ defmodule Manipulators.AddXRewriteUrlHeader do
@behaviour ProxyManipulator

@impl true
def headers( headers, {frontend_conn, _backend_conn} = connection ) do
def headers(headers, {frontend_conn, _backend_conn} = connection) do
new_headers = [{"x-rewrite-url", frontend_conn.request_path} | headers]
{new_headers, connection}
end

@impl true
def chunk(_,_), do: :skip
def chunk(_, _), do: :skip

@impl true
def finish(_,_), do: :skip
def finish(_, _), do: :skip
end
71 changes: 58 additions & 13 deletions lib/matcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ alias Dispatcher.Log

defmodule Matcher do
defmacro __using__(_opts) do
# Set this attribute _BEFORE_ any code is ran
Module.register_attribute(__CALLER__.module, :websocket, accumulate: true)

quote do
require Matcher
import Matcher
import Plug.Conn, only: [send_resp: 3]
import Proxy, only: [forward: 3]

def layers do
[ :service, :last_call ]
[:service, :last_call]
end

defoverridable layers: 0

def dispatch(conn) do
Expand All @@ -28,6 +32,34 @@ defmodule Matcher do
end
end

defmacro ws(conn, host) do
# host = "ws://localhost:8000/test"

parsed =
URI.parse(host)
|> Log.inspect(:log_ws_all, label: "Creating websocket route")

id = for _ <- 1..24, into: "", do: <<Enum.random('0123456789abcdef')>>

host = parsed.host || "localhost"
port = parsed.port || 80
path = parsed.path || "/"

Module.put_attribute(__CALLER__.module, :websocket, %{
host: host,
port: port,
path: path,
id: id
})

# Return redirect things
quote do
unquote(conn)
|> Plug.Conn.resp(:found, "")
|> Plug.Conn.put_resp_header("location", "/.mu/ws?target=" <> unquote(id))
end
end

defmacro get(path, options \\ quote(do: %{}), do: block) do
quote do
match_method(get, unquote(path), unquote(options), do: unquote(block))
Expand Down Expand Up @@ -98,7 +130,6 @@ defmodule Matcher do
defmacro __before_compile__(_env) do
matchers =
Module.get_attribute(__CALLER__.module, :matchers)
# |> IO.inspect(label: "Discovered matchers")
|> Enum.map(fn {call, path, options, block} ->
make_match_method(call, path, options, block, __CALLER__)
end)
Expand All @@ -110,7 +141,18 @@ defmodule Matcher do
end
end

[last_match_def | matchers]
socket_dict_f =
quote do
def websockets() do
Enum.reduce(@websocket, %{}, fn x, acc -> Map.put(acc, x.id, x) end)
end

def get_websocket(id) do
Enum.find(@websocket, fn x -> x.id == id end)
end
end

[socket_dict_f, last_match_def | matchers]
|> Enum.reverse()
end

Expand Down Expand Up @@ -171,24 +213,29 @@ defmodule Matcher do

new_accept =
case value do
[item] -> # convert item
# convert item
[item] ->
{:%{}, [], [{item, true}]}

[_item | _rest] ->
raise "Multiple items in accept arrays are not supported."

{:%{}, _, _} ->
value
end

new_list =
list
|> Keyword.drop( [:accept] )
|> Keyword.merge( [accept: new_accept] )
|> Keyword.drop([:accept])
|> Keyword.merge(accept: new_accept)

{:%{}, any, new_list}
else
options
end
_ -> options

_ ->
options
end
end

Expand Down Expand Up @@ -223,8 +270,6 @@ defmodule Matcher do
str -> str
end).()

# |> IO.inspect(label: "call name")

# Creates the variable(s) for the parsed path
process_derived_path_elements = fn elements ->
reversed_elements = Enum.reverse(elements)
Expand Down Expand Up @@ -310,7 +355,6 @@ defmodule Matcher do
def dispatch_call(conn, accept_types, layers_fn, call_handler) do
# Extract core info
{method, path, accept_header, host} = extract_core_info_from_conn(conn)
# |> IO.inspect(label: "extracted header")

# Extract core request info
accept_hashes =
Expand All @@ -321,10 +365,11 @@ defmodule Matcher do

# layers |> IO.inspect(label: "layers" )
# Try to find a solution in each of the layers
layers = layers_fn.()
|> Log.inspect(:log_available_layers, "Available layers")
layers =
layers_fn.()
|> Log.inspect(:log_available_layers, "Available layers")

reverse_host = Enum.reverse( host )
reverse_host = Enum.reverse(host)

response_conn =
layers
Expand Down
28 changes: 27 additions & 1 deletion lib/mu_dispatcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,37 @@ defmodule MuDispatcher do
port = 80

children = [
{Plug.Cowboy, scheme: :http, plug: PlugRouterDispatcher, options: [port: port]}
# this is kinda strange, but the 'plug:' field is not used when 'dispatch:' is provided (my understanding)
{Plug.Cowboy,
scheme: :http, plug: PlugRouterDispatcher, options: [dispatch: dispatch, port: port]}
]

Logger.info("Mu Dispatcher starting on port #{port}")

Supervisor.start_link(children, strategy: :one_for_one)
end

defp dispatch do
default = %{
host: "localhost",
port: 80,
path: "/"
}

f = fn req ->
{_, target} =
:cowboy_req.parse_qs(req)
|> Enum.find(fn {head, _} -> head == "target" end)

Dispatcher.get_websocket(target)
end

[
{:_,
[
{"/.mu/ws/[...]", WsHandler, {f, default}},
{:_, Plug.Cowboy.Handler, {PlugRouterDispatcher, []}}
]}
]
end
end
2 changes: 1 addition & 1 deletion lib/plug_router_dispatcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ defmodule PlugRouterDispatcher do
plug(:dispatch)

match _ do
Dispatcher.dispatch( conn )
Dispatcher.dispatch(conn)
end
end
8 changes: 6 additions & 2 deletions lib/proxy.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
defmodule Proxy do
@request_manipulators [Manipulators.AddXRewriteUrlHeader,Manipulators.RemoveAcceptEncodingHeader]
@request_manipulators [
Manipulators.AddXRewriteUrlHeader,
Manipulators.RemoveAcceptEncodingHeader
]
@response_manipulators [Manipulators.AddVaryHeader]
@manipulators ProxyManipulatorSettings.make_settings(
@request_manipulators,
Expand All @@ -13,6 +16,7 @@ defmodule Proxy do
conn,
path,
base,
@manipulators)
@manipulators
)
end
end
Loading