Skip to content

Commit

Permalink
update test case for path
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Sep 21, 2023
1 parent 4bcd3fb commit 0955b14
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/uplink/clients/caddy/apps/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule Uplink.Clients.Caddy.Apps.Server do
@derive Jason.Encoder

field :host, {:array, :string}
field :path, {:array, :string}, default: ["*"]
end

field :handle, {:array, :map}
Expand Down Expand Up @@ -44,6 +45,6 @@ defmodule Uplink.Clients.Caddy.Apps.Server do

defp match_changeset(match, params) do
match
|> cast(params, [:host])
|> cast(params, [:host, :path])
end
end
48 changes: 44 additions & 4 deletions lib/uplink/clients/caddy/config/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,30 @@ defmodule Uplink.Clients.Caddy.Config.Builder do
defp build_route(
%{install: %{deployment: %{app: _app}}, metadata: metadata} = _state
) do
main_routing = Map.get(metadata.main_port, :routing)

main_paths =
if main_routing do
main_routing.paths
else
["*"]
end

main_group =
if main_routing do
"router_#{main_routing.router_id}"
else
"installation_#{metadata.id}"
end

main_route = %{
group: "installation_#{metadata.id}",
match: [%{host: metadata.hosts}],
group: main_group,
match: [
%{
host: metadata.hosts,
path: main_paths
}
],
handle: [
%{
handler: "reverse_proxy",
Expand Down Expand Up @@ -106,9 +127,28 @@ defmodule Uplink.Clients.Caddy.Config.Builder do
port.slug <> "." <> host
end)

routing = Map.get(port, :routing)

paths =
if routing do
routing.paths
else
["*"]
end

group =
if routing,
do: "router_#{routing.router_id}",
else: "installation_#{metadata.id}"

%{
group: "installation_#{metadata.id}",
match: [%{host: hosts}],
group: group,
match: [
%{
host: hosts,
path: paths
}
],
handle: [
%{
handler: "reverse_proxy",
Expand Down
12 changes: 12 additions & 0 deletions lib/uplink/packages/metadata/port.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@ defmodule Uplink.Packages.Metadata.Port do
field :slug, :string
field :source, :integer
field :target, :integer

embeds_one :routing, Routing, primary_key: false do
field :router_id, :integer
field :paths, {:array, :string}, default: ["*"]
end
end

def changeset(port, params) do
port
|> cast(params, [:slug, :source, :target])
|> validate_required([:slug, :source, :target])
|> cast_embed(:routing, with: &routing_changeset/2)
end

defp routing_changeset(routing, params) do
routing
|> cast(params, [:router_id, :paths])
|> validate_required([:router_id, :paths])
end
end
6 changes: 5 additions & 1 deletion test/scenarios/deployment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ defmodule Uplink.Scenarios.Deployment do
"main_port" => %{
"slug" => "web",
"source" => 49152,
"target" => 4000
"target" => 4000,
"routing" => %{
"router_id" => 1,
"paths" => ["/configure*"]
}
},
"ports" => [
%{
Expand Down
4 changes: 4 additions & 0 deletions test/uplink/clients/caddy/config/builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ defmodule Uplink.Clients.Caddy.Config.BuilderTest do
assert %{handle: [handle], match: [match]} = first_route
assert %{handle: [second_handle], match: [second_match]} = second_route

assert match.path == ["/configure*"]

assert second_match.path == ["*"]

assert "grpc.something.com" in second_match.host

[second_upstream] = second_handle.upstreams
Expand Down

0 comments on commit 0955b14

Please sign in to comment.