Skip to content

Commit

Permalink
Setup merging for hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Dec 23, 2024
1 parent a778f51 commit 4abc270
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
28 changes: 27 additions & 1 deletion lib/uplink/clients/caddy/config/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ defmodule Uplink.Clients.Caddy.Config.Builder do
) do
main_routing = Map.get(metadata.main_port, :routing)

main_routing_hosts =
if main_routing do
main_routing.hosts
else
[]
end

main_paths =
if main_routing do
main_routing.paths
Expand Down Expand Up @@ -187,11 +194,16 @@ defmodule Uplink.Clients.Caddy.Config.Builder do
}
end)

main_hosts =
metadata.hosts
|> Enum.concat(main_routing_hosts)
|> Enum.uniq()

main_route = %{
group: main_group,
match: [
%{
host: metadata.hosts,
host: main_hosts,
path: main_paths
}
],
Expand Down Expand Up @@ -233,6 +245,20 @@ defmodule Uplink.Clients.Caddy.Config.Builder do

routing = Map.get(port, :routing)

routing_hosts =
if routing do
Enum.map(routing.hosts, fn host ->
port.slug <> "." <> host
end)
else
[]
end

hosts =
hosts
|> Enum.concat(routing_hosts)
|> Enum.uniq()

paths =
if routing do
routing.paths
Expand Down
3 changes: 2 additions & 1 deletion lib/uplink/packages/metadata/port.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Uplink.Packages.Metadata.Port do

embeds_one :routing, Routing, primary_key: false do
field :router_id, :integer
field :hosts, {:array, :string}, default: []
field :paths, {:array, :string}, default: ["*"]
end
end
Expand All @@ -23,7 +24,7 @@ defmodule Uplink.Packages.Metadata.Port do

defp routing_changeset(routing, params) do
routing
|> cast(params, [:router_id, :paths])
|> cast(params, [:router_id, :hosts, :paths])
|> validate_required([:router_id, :paths])
end
end

0 comments on commit 4abc270

Please sign in to comment.