Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mix tasks to setup ProxyEndpoint and create new Endpoints #723

Merged
merged 53 commits into from
Jan 31, 2025
Merged
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
484bf06
wip
APB9785 Jan 11, 2025
0c340cf
use igniter for web module name
APB9785 Jan 13, 2025
553ffba
fix web module
APB9785 Jan 13, 2025
dcc74ef
inspect otp_app atom
APB9785 Jan 13, 2025
3f36083
inspect web module too
APB9785 Jan 13, 2025
3f95a90
resolve warning
APB9785 Jan 13, 2025
27b8cb3
missing arg
APB9785 Jan 13, 2025
f16ae50
typo
APB9785 Jan 13, 2025
91c5c55
config
APB9785 Jan 13, 2025
2959a44
update runtime config
APB9785 Jan 13, 2025
ef0c4b2
application and router
APB9785 Jan 14, 2025
10ba70a
improve proxy gen
APB9785 Jan 15, 2025
63cd932
only configure runtime in prod
APB9785 Jan 15, 2025
64d61c6
gen site test fix
APB9785 Jan 15, 2025
ce917ed
add proxy_endpoint module to application start
APB9785 Jan 15, 2025
f986289
update port for existing endpoints
APB9785 Jan 15, 2025
d3dc83b
update line number
APB9785 Jan 16, 2025
6ff999e
better handling of ports
APB9785 Jan 16, 2025
728c82a
proxy endpoint finished
APB9785 Jan 17, 2025
a45e4ad
finish site gen
APB9785 Jan 17, 2025
e81540a
Merge branch 'main' into apb/proxy-igniter
APB9785 Jan 17, 2025
ee9ba65
add docs for opts
APB9785 Jan 17, 2025
d1625e6
add localhost to router scope host
APB9785 Jan 17, 2025
2ca68dd
Merge branch 'main' into apb/proxy-igniter
APB9785 Jan 17, 2025
8fc8a74
endpoint ordering
APB9785 Jan 17, 2025
65cb15a
endpoint server: true
APB9785 Jan 17, 2025
0f15ef0
update line numbers
APB9785 Jan 17, 2025
a6ef4ba
remove socket route from existing endpoints
APB9785 Jan 17, 2025
4720cfa
changelog
APB9785 Jan 17, 2025
fece7c6
small fixes
leandrocp Jan 22, 2025
81bd65c
docs and deps
leandrocp Jan 22, 2025
fdc945a
handle special chars in host
leandrocp Jan 22, 2025
f7b1c78
fix gen.site test
leandrocp Jan 22, 2025
f89a432
fix: replace @session_options with app env
leandrocp Jan 23, 2025
c7e9bb7
fix: ignore endpoints without socket call
leandrocp Jan 23, 2025
ea9cab9
check_origin dynamically
leandrocp Jan 24, 2025
6466600
docs
leandrocp Jan 24, 2025
5ce9daa
use the dynamic check_origin
leandrocp Jan 24, 2025
32e107f
Merge branch 'main' into apb/proxy-igniter
APB9785 Jan 24, 2025
a9c3318
update test
APB9785 Jan 24, 2025
9a28ada
use variables
APB9785 Jan 25, 2025
0425f9a
init proxy endpoint config
APB9785 Jan 27, 2025
31855fa
add debug diff
leandrocp Jan 27, 2025
163892e
fix gen_proxy_endpoint
APB9785 Jan 27, 2025
a13a861
Merge branch 'apb/proxy-igniter' of https://github.com/BeaconCMS/beac…
APB9785 Jan 27, 2025
9611fb5
fix gen_site
APB9785 Jan 27, 2025
9188117
Merge branch 'main' into apb/proxy-igniter
leandrocp Jan 29, 2025
29e3ea8
docs and small fixes
leandrocp Jan 29, 2025
0657897
skip config which would be duplicated
APB9785 Jan 30, 2025
390d32a
Merge branch 'apb/proxy-igniter' of https://github.com/BeaconCMS/beac…
APB9785 Jan 30, 2025
05cc4c5
small changes
leandrocp Jan 30, 2025
7111d64
fix: resolve to fallback endpoint first
leandrocp Jan 31, 2025
09a738d
docs
leandrocp Jan 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
improve proxy gen
APB9785 committed Jan 15, 2025
commit 10ba70a15148e332175dbc9db9bca2e055dcdf98
78 changes: 56 additions & 22 deletions lib/mix/tasks/beacon.gen.proxy_endpoint.ex
Original file line number Diff line number Diff line change
@@ -26,41 +26,42 @@ defmodule Mix.Tasks.Beacon.Gen.ProxyEndpoint do

@doc false
def igniter(igniter) do
otp_app = Igniter.Project.Application.app_name(igniter)
{igniter, router} = Beacon.Igniter.select_router!(igniter)
{igniter, fallback_endpoint} = Beacon.Igniter.select_endpoint(igniter, router, "Select a fallback endpoint (default app Endpoint):")
proxy_endpoint_module_name = Igniter.Libs.Phoenix.web_module_name(igniter, "ProxyEndpoint")
signing_salt = Keyword.get_lazy(igniter.args.argv, :signing_salt, fn -> random_string(8) end)

igniter
|> create_proxy_endpoint_module(otp_app, fallback_endpoint, proxy_endpoint_module_name)
|> add_session_options_config(otp_app, signing_salt, igniter.args.options)
|> add_proxy_endpoint_config(otp_app, proxy_endpoint_module_name, signing_salt)
|> update_fallback_endpoint_signing_salt(otp_app, fallback_endpoint, signing_salt)
|> Igniter.add_notice("""
ProxyEndpoint generated successfully.

This enables your application to serve sites at multiple hosts, each with their own Endpoint.
""")
end

defp create_proxy_endpoint_module(igniter, otp_app, fallback_endpoint, proxy_endpoint_module_name) do
case Igniter.Project.Module.module_exists(igniter, proxy_endpoint_module_name) do
{true, igniter} ->
Igniter.add_notice(igniter, """
Module #{inspect(proxy_endpoint_module_name)} already exists. Skipping.
""")

{false, igniter} ->
Igniter.Project.Module.create_module(igniter, proxy_endpoint_module_name, """
use Beacon.ProxyEndpoint,
otp_app: #{inspect(otp_app)},
session_options: Application.compile_env!(#{inspect(otp_app)}, :session_options),
fallback: #{inspect(fallback_endpoint)}
otp_app = Igniter.Project.Application.app_name(igniter)
{igniter, router} = Beacon.Igniter.select_router!(igniter)
{igniter, fallback_endpoint} = Beacon.Igniter.select_endpoint(igniter, router, "Select a fallback endpoint (default app Endpoint):")
signing_salt = Keyword.get_lazy(igniter.args.argv, :signing_salt, fn -> random_string(8) end)

igniter
|> create_proxy_endpoint_module(otp_app, fallback_endpoint, proxy_endpoint_module_name)
|> add_session_options_config(otp_app, signing_salt, igniter.args.options)
|> add_proxy_endpoint_config(otp_app, proxy_endpoint_module_name, signing_salt)
|> update_fallback_endpoint_signing_salt(otp_app, fallback_endpoint, signing_salt)
|> Igniter.add_notice("""
ProxyEndpoint generated successfully.

This enables your application to serve sites at multiple hosts, each with their own Endpoint.
""")
end
end

defp create_proxy_endpoint_module(igniter, otp_app, fallback_endpoint, proxy_endpoint_module_name) do
Igniter.Project.Module.create_module(igniter, proxy_endpoint_module_name, """
use Beacon.ProxyEndpoint,
otp_app: #{inspect(otp_app)},
session_options: Application.compile_env!(#{inspect(otp_app)}, :session_options),
fallback: #{inspect(fallback_endpoint)}
""")
end

def add_session_options_config(igniter, otp_app, signing_salt, options) do
key = Keyword.get_lazy(options, :key, fn -> "_#{otp_app}_key" end)
same_site = Keyword.get(options, :same_site, "Lax")
@@ -102,6 +103,39 @@ defmodule Mix.Tasks.Beacon.Gen.ProxyEndpoint do
[proxy_endpoint_module_name, :check_origin],
{:code, Sourceror.parse_string!("[]")}
)
|> Igniter.Project.Config.configure(
"runtime.exs",
otp_app,
[proxy_endpoint_module_name, :url],
{:code, Sourceror.parse_string!("[port: 443, scheme: \"https\"]")}
)
|> Igniter.Project.Config.configure(
"runtime.exs",
otp_app,
[proxy_endpoint_module_name, :http],
{:code, Sourceror.parse_string!("[ip: {0, 0, 0, 0, 0, 0, 0, 0}, port: port]")}
)
|> Igniter.Project.Config.configure(
"runtime.exs",
otp_app,
[proxy_endpoint_module_name, :secret_key_base],
{:code, Sourceror.parse_string!("secret_key_base")}
)
|> Igniter.Project.Config.configure(
"dev.exs",
otp_app,
[proxy_endpoint_module_name, :http],
{:code, Sourceror.parse_string!("[ip: {127, 0, 0, 1}, port: 4000]")}
)
|> Igniter.Project.Config.configure("dev.exs", otp_app, [proxy_endpoint_module_name, :check_origin], {:code, Sourceror.parse_string!("false")})
|> Igniter.Project.Config.configure("dev.exs", otp_app, [proxy_endpoint_module_name, :debug_errors], {:code, Sourceror.parse_string!("true")})
# TODO: ensure secret key valid
|> Igniter.Project.Config.configure(
"dev.exs",
otp_app,
[proxy_endpoint_module_name, :secret_key_base],
"A0DSgxjGCYZ6fCIrBlg6L+qC/cdoFq5Rmomm53yacVmN95Wcpl57Gv0sTJjKjtIp"
)
end

defp update_fallback_endpoint_signing_salt(igniter, otp_app, fallback_endpoint, signing_salt) do
40 changes: 34 additions & 6 deletions test/mix/tasks/gen_proxy_endpoint.exs
Original file line number Diff line number Diff line change
@@ -20,13 +20,15 @@ defmodule Mix.Tasks.Beacon.GenProxyEndpointTest do
|> Igniter.compose_task("beacon.gen.proxy_endpoint")
|> assert_creates("lib/test_web/proxy_endpoint.ex", """
defmodule TestWeb.ProxyEndpoint do
@session_options Application.compile_env!(:test, :session_options)
use Beacon.ProxyEndpoint, otp_app: :test, session_options: @session_options, fallback: TestWeb.Endpoint
use Beacon.ProxyEndpoint,
otp_app: :test,
session_options: Application.compile_env!(:test, :session_options),
fallback: TestWeb.Endpoint
end
""")
end

test "update config", %{project: project} do
test "update config.exs", %{project: project} do
project
|> Igniter.compose_task("beacon.gen.proxy_endpoint", signing_salt: "SNUXnTNM")
# add session options config
@@ -44,12 +46,38 @@ defmodule Mix.Tasks.Beacon.GenProxyEndpointTest do
""")
# add proxy endpoint config
|> assert_has_patch("config/config.exs", """
10 + |config :test, TestWeb.ProxyEndpoint, adapter: Bandit.PhoenixAdapter, live_view: [signing_salt: "SNUXnTNM"]
11 + |
10 + |config :test, TestWeb.ProxyEndpoint, adapter: Bandit.PhoenixAdapter, live_view: [signing_salt: "SNUXnTNM"]
11 + |
""")
# update fallback endpoint signing salt
|> assert_has_patch("config/config.exs", """
31 + | live_view: [signing_salt: "SNUXnTNM"]
31 + | live_view: [signing_salt: "SNUXnTNM"]
""")
end

test "update dev.exs", %{project: project} do
project
|> Igniter.compose_task("beacon.gen.proxy_endpoint", signing_salt: "SNUXnTNM")
|> assert_has_patch("config/dev.exs", """
3 + |config :test, TestWeb.ProxyEndpoint,
4 + | http: [ip: {127, 0, 0, 1}, port: 4000],
5 + | check_origin: false,
6 + | debug_errors: true,
7 + | secret_key_base: "A0DSgxjGCYZ6fCIrBlg6L+qC/cdoFq5Rmomm53yacVmN95Wcpl57Gv0sTJjKjtIp"
8 + |
""")
end

test "update runtime.exs", %{project: project} do
project
|> Igniter.compose_task("beacon.gen.proxy_endpoint", signing_salt: "SNUXnTNM")
|> assert_has_patch("config/runtime.exs", """
3 + |config :test, TestWeb.ProxyEndpoint,
4 + | check_origin: [],
5 + | url: [port: 443, scheme: "https"],
6 + | http: [ip: {0, 0, 0, 0, 0, 0, 0, 0}, port: port],
7 + | secret_key_base: secret_key_base
8 + |
""")
end
end

Unchanged files with check annotations Beta

""")
end
test "updates runtime.exs", %{project: project} do

Check failure on line 242 in test/mix/tasks/gen_site_test.exs

GitHub Actions / test: OTP 23 | Elixir 1.14.0 | Phoenix 1.7.0 | LV 0.20.2 | PG 13.0-alpine

test --host option updates runtime.exs (Mix.Tasks.Beacon.GenSiteTest)

Check failure on line 242 in test/mix/tasks/gen_site_test.exs

GitHub Actions / test: OTP 27 | Elixir 1.17 | Phoenix ~> 1.7 | LV ~> 1.0 | PG 16.4-alpine

test --host option updates runtime.exs (Mix.Tasks.Beacon.GenSiteTest)
project
|> Igniter.compose_task("beacon.gen.site", @opts_my_site ++ ~w(--host example.com))
|> assert_has_patch("config/runtime.exs", """