-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor configuration builder for caddy
- Loading branch information
Showing
6 changed files
with
299 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule Uplink.Caddy.Config.Hosts do | ||
alias Uplink.Packages.Metadata | ||
alias Uplink.Packages.Metadata.Port | ||
|
||
def routable?(%{metadata: %Metadata{main_port: nil}}), do: false | ||
|
||
def routable?(%{ | ||
metadata: %Metadata{ | ||
main_port: %{routing: %Port.Routing{}} | ||
} | ||
}), | ||
do: true | ||
|
||
def routable?(%{metadata: %Metadata{hosts: hosts}}) when length(hosts) > 0, | ||
do: true | ||
|
||
def routable?(%{metadata: %Metadata{hosts: []}}), do: false | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
defmodule Uplink.Caddy.Config.Port do | ||
alias Uplink.Packages.Metadata | ||
|
||
alias Uplink.Caddy.Config.Upstreams | ||
|
||
def build(%Metadata{ports: ports} = metadata, install_id) do | ||
ports | ||
|> Enum.map(&build(&1, metadata, install_id)) | ||
|> Enum.reject(&is_nil/1) | ||
end | ||
|
||
def build(%Metadata.Port{} = port, metadata, install_id) do | ||
hosts = Enum.map(metadata.hosts, &merge_slug_and_host(&1, port)) | ||
|
||
routing = Map.get(port, :routing) | ||
|
||
routing_hosts = | ||
if routing do | ||
Enum.map(routing.hosts, &merge_slug_and_host(&1, port)) | ||
else | ||
[] | ||
end | ||
|
||
hosts = | ||
hosts | ||
|> Enum.concat(routing_hosts) | ||
|> Enum.uniq() | ||
|> Enum.sort() | ||
|
||
paths = | ||
if routing && routing.paths != [] do | ||
routing.paths | ||
else | ||
["*"] | ||
end | ||
|
||
group = | ||
if routing, | ||
do: "router_#{routing.router_id}", | ||
else: "installation_#{metadata.id}" | ||
|
||
if hosts == [] do | ||
nil | ||
else | ||
%{ | ||
group: group, | ||
match: [ | ||
%{ | ||
host: hosts, | ||
path: paths | ||
} | ||
], | ||
handle: [ | ||
%{ | ||
handler: "reverse_proxy", | ||
load_balancing: %{ | ||
selection_policy: %{ | ||
policy: "least_conn" | ||
} | ||
}, | ||
health_checks: %{ | ||
passive: %{ | ||
fail_duration: "10s", | ||
max_fails: 3, | ||
unhealthy_request_count: 80, | ||
unhealthy_status: [500, 501, 502, 503, 504], | ||
unhealthy_latency: "30s" | ||
} | ||
}, | ||
upstreams: Upstreams.build(metadata, port, install_id) | ||
} | ||
] | ||
} | ||
end | ||
end | ||
|
||
defp merge_slug_and_host(host, %Metadata.Port{slug: slug}), | ||
do: slug <> "." <> host | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule Uplink.Caddy.Config.Upstreams do | ||
alias Uplink.Cache | ||
alias Uplink.Packages.Metadata | ||
alias Uplink.Packages.Metadata.Port | ||
|
||
def build(%Metadata{instances: instances}, %Port{} = port, install_id) do | ||
instances | ||
|> filter_valid(install_id) | ||
|> Enum.map(fn instance -> | ||
%{ | ||
dial: "#{instance.slug}:#{port.target}", | ||
max_requests: 100 | ||
} | ||
end) | ||
end | ||
|
||
def filter_valid(instances, install_id) do | ||
completed_instances = Cache.get({:install, install_id, "completed"}) | ||
|
||
if is_list(completed_instances) and Enum.count(completed_instances) > 0 do | ||
instances | ||
|> Enum.filter(fn instance -> | ||
instance.slug in completed_instances | ||
end) | ||
else | ||
instances | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.