Skip to content

Commit

Permalink
move cowboy ws proxy to seperate git repo
Browse files Browse the repository at this point in the history
  • Loading branch information
ajuvercr committed Jul 21, 2021
1 parent 72c4243 commit a14f85d
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 171 deletions.
6 changes: 1 addition & 5 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ config :dispatcher,
# log whenever a layer starts processing
log_layer_start_processing: CH.system_boolean("LOG_LAYER_START_PROCESSING"),
# log whenever a layer matched, and if no matching layer was found
log_layer_matching: CH.system_boolean("LOG_LAYER_MATCHING"),
log_ws_all: CH.system_boolean("LOG_WS_ALL"),
log_ws_backend: CH.system_boolean("LOG_WS_BACKEND"),
log_ws_frontend: CH.system_boolean("LOG_WS_FRONTEND"),
log_ws_unhandled: CH.system_boolean("LOG_WS_UNHANDLED")
log_layer_matching: CH.system_boolean("LOG_LAYER_MATCHING")

# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
Expand Down
78 changes: 46 additions & 32 deletions lib/dispatcher.ex
Original file line number Diff line number Diff line change
@@ -1,54 +1,68 @@
defmodule Dispatcher do
use Matcher

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"
# end
define_accept_types [
html: [ "text/html", "application/xhtml+html" ],
json: [ "application/json", "application/vnd.api+json" ],
]

@any %{}
@json %{ accept: %{ json: true } }
@html %{ accept: %{ html: true } }

# get "/assets/*rest", %{} do
# Proxy.forward conn, rest, "http://static/assets/"
# In order to forward the 'themes' resource to the
# resource service, use the following forward rule.
#
# docker-compose stop; docker-compose rm; docker-compose up
# after altering this file.
#
# match "/themes/*path", @json do
# Proxy.forward conn, path, "http://resource/themes/"
# end

post "/hello/erika", %{} do
Plug.Conn.send_resp(conn, 401, "FORBIDDEN")
match "/songs/*path" do
Proxy.forward conn, path, "http://resource/songs/"
end


match "/bands/*path" do
Proxy.forward conn, path, "http://resource/bands/"
end

# 200 microservice dispatching
match "/sessions/*path" do
Proxy.forward conn, path, "http://login/sessions/"
end

match "/accounts/*path" do
Proxy.forward conn, path, "http://registration/accounts/"
end

match "/hello/erika", %{accept: %{json: true}} do
Plug.Conn.send_resp(conn, 200, "{ \"message\": \"Hello Erika\" }\n")
match "/games/*path" do
Proxy.forward conn, path, "http://resource/games/"
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 "/moves/*path" do
Proxy.forward conn, path, "http://resource/moves/"
end

# 404 routes

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

# Websocket example route
# This forwards to /ws?target=<...>
# Then forwards websocket from /ws?target=<...> to ws://localhost:7999
match "/websocket/*path" do
ws(conn, "ws://myMicroservice:8080")
end

match "/ws2" do
ws(conn, "ws://localhost:7999")
match "/echo/*path" do
ws(conn, "ws://myMicroservice:8081")
end

match "/sparql/*path" do
Proxy.forward conn, path, "http://db:8890/sparql"
end

match "__", %{last_call: true} do
send_resp(conn, 404, "Route not found. See config/dispatcher.ex")
match "_", %{ last_call: true } do
send_resp( conn, 404, "Route not found. See config/dispatcher.ex" )
end
end
4 changes: 0 additions & 4 deletions lib/dispatcher/log.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ defmodule Dispatcher.Log do
@type log_name ::
:log_layer_start_processing
| :log_layer_matching
| :log_ws_all
| :log_ws_backend
| :log_ws_frontend
| :log_ws_unhandled

@spec log(log_name, any()) :: any()
def log(name, content) do
Expand Down
6 changes: 3 additions & 3 deletions lib/manipulators/remove_accept_encoding_header.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ defmodule Manipulators.RemoveAcceptEncodingHeader do

@impl true
def headers(headers, connection) do
# headers =
# headers
# |> Enum.reject( &match?( {"accept_encoding", _}, &1 ) )
headers =
headers
|> Enum.reject( &match?( {"accept_encoding", _}, &1 ) )
{headers, connection}
end

Expand Down
2 changes: 1 addition & 1 deletion lib/matcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ defmodule Matcher do
defp sort_and_group_accept_headers(accept) do
accept
|> safe_parse_accept_header()
|> IO.inspect(label: "parsed_accept_header")
# |> IO.inspect(label: "parsed_accept_header")
|> Enum.sort_by(&elem(&1, 3))
|> Enum.group_by(&elem(&1, 3))
|> Map.to_list()
Expand Down
18 changes: 16 additions & 2 deletions lib/mu_dispatcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule MuDispatcher do

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

Expand All @@ -21,10 +21,24 @@ defmodule MuDispatcher do
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

[
{:_,
[
{"/ws/[...]", WebsocketHandler, %{}},
{"/ws/[...]", WsHandler, {f, default}},
{:_, Plug.Cowboy.Handler, {PlugRouterDispatcher, []}}
]}
]
Expand Down
2 changes: 0 additions & 2 deletions lib/plug_router_dispatcher.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
alias Dispatcher.Log

defmodule PlugRouterDispatcher do
use Plug.Router

Expand Down
122 changes: 0 additions & 122 deletions lib/websocket_handler.ex

This file was deleted.

1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule Dispatcher.Mixfile do
# Type `mix help deps` for more examples and options
defp deps do
[
{:cowboy_ws_proxy, git: "https://github.com/ajuvercr/elixir-cowboy-ws-proxy-handler.git", tag: "v0.1"},
{:plug_mint_proxy,
git: "https://github.com/madnificent/plug-mint-proxy.git", tag: "v0.0.2"},
# {:plug, "~> 1.10.4"},
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"castore": {:hex, :castore, "0.1.11", "c0665858e0e1c3e8c27178e73dffea699a5b28eb72239a3b2642d208e8594914", [:mix], [], "hexpm", "91b009ba61973b532b84f7c09ce441cba7aa15cb8b006cf06c6f4bba18220081"},
"cowboy": {:hex, :cowboy, "2.9.0", "865dd8b6607e14cf03282e10e934023a1bd8be6f6bacf921a7e2a96d800cd452", [:make, :rebar3], [{:cowlib, "2.11.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "2c729f934b4e1aa149aff882f57c6372c15399a20d54f65c8d67bef583021bde"},
"cowboy_telemetry": {:hex, :cowboy_telemetry, "0.3.1", "ebd1a1d7aff97f27c66654e78ece187abdc646992714164380d8a041eda16754", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3a6efd3366130eab84ca372cbd4a7d3c3a97bdfcfb4911233b035d117063f0af"},
"cowboy_ws_proxy": {:git, "https://github.com/ajuvercr/elixir-cowboy-ws-proxy-handler.git", "e015e27775af30d4e3d7ca5629d97191cca61555", [tag: "v0.1"]},
"cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"},
"exsync": {:hex, :exsync, "0.2.4", "5cdc824553e0f4c4bf60018a9a6bbd5d3b51f93ef8401a0d8545f93127281d03", [:mix], [{:file_system, "~> 0.2", [hex: :file_system, repo: "hexpm", optional: false]}], "hexpm", "f7622d8bb98abbe473aa066ae46f91afdf7a5346b8b89728404f7189d2e80896"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
Expand Down

0 comments on commit a14f85d

Please sign in to comment.