Skip to content

Commit a14f85d

Browse files
committed
move cowboy ws proxy to seperate git repo
1 parent 72c4243 commit a14f85d

File tree

10 files changed

+69
-171
lines changed

10 files changed

+69
-171
lines changed

config/config.exs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ config :dispatcher,
4444
# log whenever a layer starts processing
4545
log_layer_start_processing: CH.system_boolean("LOG_LAYER_START_PROCESSING"),
4646
# log whenever a layer matched, and if no matching layer was found
47-
log_layer_matching: CH.system_boolean("LOG_LAYER_MATCHING"),
48-
log_ws_all: CH.system_boolean("LOG_WS_ALL"),
49-
log_ws_backend: CH.system_boolean("LOG_WS_BACKEND"),
50-
log_ws_frontend: CH.system_boolean("LOG_WS_FRONTEND"),
51-
log_ws_unhandled: CH.system_boolean("LOG_WS_UNHANDLED")
47+
log_layer_matching: CH.system_boolean("LOG_LAYER_MATCHING")
5248

5349
# It is also possible to import configuration files, relative to this
5450
# directory. For example, you can emulate configuration per environment

lib/dispatcher.ex

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,68 @@
11
defmodule Dispatcher do
22
use Matcher
33

4-
define_accept_types(
5-
text: ["text/*"],
6-
html: ["text/html", "application/xhtml+html"],
7-
json: ["application/json", "application/vnd.api+json"]
8-
)
9-
10-
# get "/*_rest", %{ accept: %{ html: true } } do
11-
# Proxy.forward conn, [], "http://static/ember-app/index.html"
12-
# end
4+
define_accept_types [
5+
html: [ "text/html", "application/xhtml+html" ],
6+
json: [ "application/json", "application/vnd.api+json" ],
7+
]
8+
9+
@any %{}
10+
@json %{ accept: %{ json: true } }
11+
@html %{ accept: %{ html: true } }
1312

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

18-
post "/hello/erika", %{} do
19-
Plug.Conn.send_resp(conn, 401, "FORBIDDEN")
23+
match "/songs/*path" do
24+
Proxy.forward conn, path, "http://resource/songs/"
25+
end
26+
27+
28+
match "/bands/*path" do
29+
Proxy.forward conn, path, "http://resource/bands/"
2030
end
2131

22-
# 200 microservice dispatching
32+
match "/sessions/*path" do
33+
Proxy.forward conn, path, "http://login/sessions/"
34+
end
35+
36+
match "/accounts/*path" do
37+
Proxy.forward conn, path, "http://registration/accounts/"
38+
end
2339

24-
match "/hello/erika", %{accept: %{json: true}} do
25-
Plug.Conn.send_resp(conn, 200, "{ \"message\": \"Hello Erika\" }\n")
40+
match "/games/*path" do
41+
Proxy.forward conn, path, "http://resource/games/"
2642
end
2743

28-
match "/hello/erika", %{accept: %{html: true}} do
29-
Plug.Conn.send_resp(
30-
conn,
31-
200,
32-
"<html><head><title>Hello</title></head><body>Hello Erika</body></html>"
33-
)
44+
match "/moves/*path" do
45+
Proxy.forward conn, path, "http://resource/moves/"
3446
end
3547

36-
# 404 routes
3748

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

42-
# Websocket example route
43-
# This forwards to /ws?target=<...>
44-
# Then forwards websocket from /ws?target=<...> to ws://localhost:7999
53+
match "/websocket/*path" do
54+
ws(conn, "ws://myMicroservice:8080")
55+
end
4556

46-
match "/ws2" do
47-
ws(conn, "ws://localhost:7999")
57+
match "/echo/*path" do
58+
ws(conn, "ws://myMicroservice:8081")
4859
end
4960

61+
match "/sparql/*path" do
62+
Proxy.forward conn, path, "http://db:8890/sparql"
63+
end
5064

51-
match "__", %{last_call: true} do
52-
send_resp(conn, 404, "Route not found. See config/dispatcher.ex")
65+
match "_", %{ last_call: true } do
66+
send_resp( conn, 404, "Route not found. See config/dispatcher.ex" )
5367
end
5468
end

lib/dispatcher/log.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ defmodule Dispatcher.Log do
22
@type log_name ::
33
:log_layer_start_processing
44
| :log_layer_matching
5-
| :log_ws_all
6-
| :log_ws_backend
7-
| :log_ws_frontend
8-
| :log_ws_unhandled
95

106
@spec log(log_name, any()) :: any()
117
def log(name, content) do

lib/manipulators/remove_accept_encoding_header.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ defmodule Manipulators.RemoveAcceptEncodingHeader do
33

44
@impl true
55
def headers(headers, connection) do
6-
# headers =
7-
# headers
8-
# |> Enum.reject( &match?( {"accept_encoding", _}, &1 ) )
6+
headers =
7+
headers
8+
|> Enum.reject( &match?( {"accept_encoding", _}, &1 ) )
99
{headers, connection}
1010
end
1111

lib/matcher.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ defmodule Matcher do
478478
defp sort_and_group_accept_headers(accept) do
479479
accept
480480
|> safe_parse_accept_header()
481-
|> IO.inspect(label: "parsed_accept_header")
481+
# |> IO.inspect(label: "parsed_accept_header")
482482
|> Enum.sort_by(&elem(&1, 3))
483483
|> Enum.group_by(&elem(&1, 3))
484484
|> Map.to_list()

lib/mu_dispatcher.ex

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule MuDispatcher do
1111

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

@@ -21,10 +21,24 @@ defmodule MuDispatcher do
2121
end
2222

2323
defp dispatch do
24+
default = %{
25+
host: "localhost",
26+
port: 80,
27+
path: "/"
28+
}
29+
30+
f = fn req ->
31+
{_, target} =
32+
:cowboy_req.parse_qs(req)
33+
|> Enum.find(fn {head, _} -> head == "target" end)
34+
35+
Dispatcher.get_websocket(target)
36+
end
37+
2438
[
2539
{:_,
2640
[
27-
{"/ws/[...]", WebsocketHandler, %{}},
41+
{"/ws/[...]", WsHandler, {f, default}},
2842
{:_, Plug.Cowboy.Handler, {PlugRouterDispatcher, []}}
2943
]}
3044
]

lib/plug_router_dispatcher.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
alias Dispatcher.Log
2-
31
defmodule PlugRouterDispatcher do
42
use Plug.Router
53

lib/websocket_handler.ex

Lines changed: 0 additions & 122 deletions
This file was deleted.

mix.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ defmodule Dispatcher.Mixfile do
2727
# Type `mix help deps` for more examples and options
2828
defp deps do
2929
[
30+
{:cowboy_ws_proxy, git: "https://github.com/ajuvercr/elixir-cowboy-ws-proxy-handler.git", tag: "v0.1"},
3031
{:plug_mint_proxy,
3132
git: "https://github.com/madnificent/plug-mint-proxy.git", tag: "v0.0.2"},
3233
# {:plug, "~> 1.10.4"},

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"castore": {:hex, :castore, "0.1.11", "c0665858e0e1c3e8c27178e73dffea699a5b28eb72239a3b2642d208e8594914", [:mix], [], "hexpm", "91b009ba61973b532b84f7c09ce441cba7aa15cb8b006cf06c6f4bba18220081"},
44
"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"},
55
"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"},
6+
"cowboy_ws_proxy": {:git, "https://github.com/ajuvercr/elixir-cowboy-ws-proxy-handler.git", "e015e27775af30d4e3d7ca5629d97191cca61555", [tag: "v0.1"]},
67
"cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"},
78
"exsync": {:hex, :exsync, "0.2.4", "5cdc824553e0f4c4bf60018a9a6bbd5d3b51f93ef8401a0d8545f93127281d03", [:mix], [{:file_system, "~> 0.2", [hex: :file_system, repo: "hexpm", optional: false]}], "hexpm", "f7622d8bb98abbe473aa066ae46f91afdf7a5346b8b89728404f7189d2e80896"},
89
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},

0 commit comments

Comments
 (0)