Skip to content

Commit

Permalink
Add listener wrappers and logs config
Browse files Browse the repository at this point in the history
Signed-off-by: Zack Siri <[email protected]>
  • Loading branch information
zacksiri committed Nov 10, 2023
1 parent 17152f3 commit 3d13310
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
4 changes: 3 additions & 1 deletion lib/uplink/clients/caddy/apps/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule Uplink.Clients.Caddy.Apps.Server do
@primary_key false
embedded_schema do
field :listen, {:array, :string}
field :listener_wrappers, {:array, :map}
field :logs, :map

embeds_many :routes, Route, primary_key: false do
@derive Jason.Encoder
Expand All @@ -27,7 +29,7 @@ defmodule Uplink.Clients.Caddy.Apps.Server do

def changeset(server, params) do
server
|> cast(params, [:listen])
|> cast(params, [:listen, :listener_wrappers, :logs])
|> cast_embed(:routes, with: &route_changeset/2)
end

Expand Down
18 changes: 13 additions & 5 deletions lib/uplink/clients/caddy/config/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ defmodule Uplink.Clients.Caddy.Config.Builder do
}
},
logs: %{
default: %{
uplink: %{
writer: %{
output: "stdout"
},
encoder: %{
format: "console"
}
},
include: ["http.log.access"]
}
}
}
Expand Down Expand Up @@ -79,9 +80,16 @@ defmodule Uplink.Clients.Caddy.Config.Builder do
%{
"uplink" => %{
listen: [":443"],
listener_wrappers: [
%{wrapper: "proxy_protocol"},
%{wrapper: "tls"}
],
routes:
Enum.map(installs, &build_route/1)
|> List.flatten()
|> List.flatten(),
logs: %{
default_logger_name: "uplink"
}
}
}
end
Expand Down Expand Up @@ -118,7 +126,7 @@ defmodule Uplink.Clients.Caddy.Config.Builder do
handler: "reverse_proxy",
load_balancing: %{
selection_policy: %{
policy: "ip_hash"
policy: "least_conn"
}
},
health_checks: %{
Expand Down Expand Up @@ -176,7 +184,7 @@ defmodule Uplink.Clients.Caddy.Config.Builder do
handler: "reverse_proxy",
load_balancing: %{
selection_policy: %{
policy: "ip_hash"
policy: "least_conn"
}
},
health_checks: %{
Expand Down
9 changes: 8 additions & 1 deletion test/fixtures/caddy/config/get.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"listen": [
":2015"
],
"listener_wrappers": [
{"wrapper": "proxy_protocol"},
{"wrapper": "tls"}
],
"routes": [
{
"handle": [
Expand All @@ -16,7 +20,10 @@
}
]
}
]
],
"logs": {
"default_logger_name": "default"
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions test/uplink/clients/caddy_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ defmodule Uplink.Clients.CaddyTest do

assert {:ok, config} = Caddy.get_config()

assert %{apps: %{http: %{servers: servers}}} = config

assert %{"example" => server} = servers

assert %{
listen: _listen,
listener_wrappers: _wrappers,
logs: %{"default_logger_name" => _},
routes: _routes
} = server

assert %{apps: %Caddy.Apps{}} = config
end
end
Expand Down

0 comments on commit 3d13310

Please sign in to comment.