diff --git a/lib/nimble_template/addons/github.ex b/lib/nimble_template/addons/github.ex index 6b119805..8606ed4d 100644 --- a/lib/nimble_template/addons/github.ex +++ b/lib/nimble_template/addons/github.ex @@ -74,11 +74,29 @@ defmodule NimbleTemplate.Addons.Github do end @impl true - def do_apply(%Project{mix_project?: false} = project, %{github_action_deploy_heroku: true}) do + def do_apply(%Project{mix_project?: false, otp_app: otp_app, web_module: web_module} = project, %{ + github_action_deploy_heroku: true + }) do Generator.copy_file([ {:eex, ".github/workflows/deploy_heroku.yml", ".github/workflows/deploy_heroku.yml"} ]) + Generator.replace_content("config/runtime.exs", "# ssl: true,", "ssl: true,") + + Generator.replace_content( + "config/runtime.exs", + "url: [host: host,", + "url: [scheme: \"https\", host: host," + ) + + Generator.append_content( + "config/prod.exs", + """ + config :#{otp_app}, #{web_module}.Endpoint, + force_ssl: [rewrite_on: [:x_forwarded_proto]] + """ + ) + project end diff --git a/lib/nimble_template/addons/variants/phoenix/mix_release.ex b/lib/nimble_template/addons/variants/phoenix/mix_release.ex index 878c24fb..63f37daf 100644 --- a/lib/nimble_template/addons/variants/phoenix/mix_release.ex +++ b/lib/nimble_template/addons/variants/phoenix/mix_release.ex @@ -5,7 +5,9 @@ defmodule NimbleTemplate.Addons.Phoenix.MixRelease do @impl true def do_apply(%Project{} = project, _opts) do - copy_files(project) + project + |> copy_files() + |> edit_files() project end @@ -23,4 +25,61 @@ defmodule NimbleTemplate.Addons.Phoenix.MixRelease do project end + + defp edit_files(%{otp_app: otp_app, web_module: web_module} = project) do + Generator.delete_content( + "config/runtime.exs", + """ + # Start the phoenix server if environment is set and running in a release + if System.get_env("PHX_SERVER") && System.get_env("RELEASE_NAME") do + config :#{otp_app}, #{web_module}.Endpoint, server: true + end + + """ + ) + + Generator.delete_content( + "config/runtime.exs", + """ + + # ## Using releases + # + # If you are doing OTP releases, you need to instruct Phoenix + # to start each relevant endpoint: + # + # config :#{otp_app}, #{web_module}.Endpoint, server: true + # + # Then you can assemble a release by calling `mix release`. + # See `mix help release` for more information. + """ + ) + + Generator.replace_content( + "config/runtime.exs", + """ + config :#{otp_app}, #{web_module}.Endpoint, + """, + """ + config :#{otp_app}, #{web_module}.Endpoint, + server: true, + """ + ) + + Generator.replace_content( + "config/runtime.exs", + """ + host = System.get_env("PHX_HOST") || "example.com" + """, + """ + host = + System.get_env("PHX_HOST") || + raise \"\"\" + Environment variable PHX_HOST is missing. + Set the Heroku endpoint to this variable. + \"\"\" + """ + ) + + project + end end diff --git a/priv/templates/nimble_template/.dockerignore b/priv/templates/nimble_template/.dockerignore index b042b8b5..865baac3 100644 --- a/priv/templates/nimble_template/.dockerignore +++ b/priv/templates/nimble_template/.dockerignore @@ -12,4 +12,3 @@ README.md cover **/tmp/ doc -/priv/static/ diff --git a/priv/templates/nimble_template/.github/wiki/Environment-Variables.md.eex b/priv/templates/nimble_template/.github/wiki/Environment-Variables.md.eex index d52cd70a..86d149b2 100644 --- a/priv/templates/nimble_template/.github/wiki/Environment-Variables.md.eex +++ b/priv/templates/nimble_template/.github/wiki/Environment-Variables.md.eex @@ -8,3 +8,5 @@ ### Endpoint configuration - `HEALTH_PATH`: Health path (eg: "/_health") +- `PHX_HOST`: The application endpoint. +- `SECRET_KEY_BASE`: Secret key base diff --git a/priv/templates/nimble_template/.github/workflows/README.md.eex b/priv/templates/nimble_template/.github/workflows/README.md.eex index 3a6eba7c..c259f396 100644 --- a/priv/templates/nimble_template/.github/workflows/README.md.eex +++ b/priv/templates/nimble_template/.github/workflows/README.md.eex @@ -16,7 +16,8 @@ The following workflows are supported. - A Heroku API key. It can be generated under your [Account Settings](https://dashboard.heroku.com/account#api-key) - Three Heroku config vars: - **DATABASE_URL**: It will be created automatically when the [PostgreSQL add-on](https://elements.heroku.com/addons/heroku-postgresql) is added. - - **HOST_URL**: if your app name is `acme`, the value of this var is: `acme.herokuapp.com` + - **PHX_HOST**: if your app name is `acme`, the value of this var is: `acme.herokuapp.com` + - **HEALTH_PATH**: Health path (eg: "/_health") - **SECRET_KEY_BASE**: use the `mix phx.gen.secret` to get a new secret and set it as the value of this var. ### How to use @@ -24,32 +25,7 @@ The following workflows are supported. - Defining two [Github secrets](https://docs.github.com/en/actions/reference/encrypted-secrets) to hold the value of Heroku API key and Heroku app name: - HEROKU_API_KEY - HEROKU_APP_NAME -- Making some configuration changes to make the app ready for Heroku (read more in the [official documentation](https://hexdocs.pm/phoenix/heroku.html#making-our-project-ready-for-heroku)) that: - tell Phoenix to use Heroku URL and enforce the SSL usage. Also, bind to the port requested by Heroku in the `$PORT` environment variable. - - Find the `url` configuration in your `config/prod.exs`: - - ```elixir - url: [host: "example.com", port: 80], - ``` - - and replace it with this: - - ```elixir - http: [port: {:system, "PORT"}], - url: [scheme: "https", host: System.get_env("HOST_URL"), port: 443], - force_ssl: [rewrite_on: [:x_forwarded_proto]], - ``` - - Enable SSL for production environment by uncomment it in the file `config/prod.secret.exs`: - - ```elixir - config :gscraper_web, GscraperWeb.Repo, - ssl: true, - ... - ``` - - If you plan on using WebSockets, the timeout for the WebSocket transport needs to be decreased in `lib/hello_web/endpoint.ex`. +- If you plan on using WebSockets, the timeout for the WebSocket transport needs to be decreased in `lib/hello_web/endpoint.ex`. ```elixir socket "/socket", HelloWeb.UserSocket, diff --git a/test/nimble_template/addons/github_test.exs b/test/nimble_template/addons/github_test.exs index 38f0630a..8adb9569 100644 --- a/test/nimble_template/addons/github_test.exs +++ b/test/nimble_template/addons/github_test.exs @@ -287,6 +287,44 @@ defmodule NimbleTemplate.Addons.GithubTest do assert_file(".github/workflows/deploy_heroku.yml") end) end + + test "adjusts config/runtime.exs ", %{ + project: project, + test_project_path: test_project_path + } do + project = %{project | api_project?: true, web_project?: false} + + in_test_project(test_project_path, fn -> + Addons.Github.apply(project, %{github_action_deploy_heroku: true}) + + assert_file("config/runtime.exs", fn file -> + assert file =~ "url: [scheme: \"https\", host: host," + + assert file =~ """ + config :nimble_template, NimbleTemplate.Repo, + ssl: true, + """ + end) + end) + end + + test "adds force_ssl config into config/prod.exs", %{ + project: project, + test_project_path: test_project_path + } do + project = %{project | api_project?: true, web_project?: false} + + in_test_project(test_project_path, fn -> + Addons.Github.apply(project, %{github_action_deploy_heroku: true}) + + assert_file("config/prod.exs", fn file -> + assert file =~ """ + config :nimble_template, NimbleTemplateWeb.Endpoint, + force_ssl: [rewrite_on: [:x_forwarded_proto]] + """ + end) + end) + end end describe "#apply/2 with web_project and github_action_deploy_heroku option" do diff --git a/test/nimble_template/addons/variants/mix_release_test.exs b/test/nimble_template/addons/variants/mix_release_test.exs index 43c58853..6e0e6722 100644 --- a/test/nimble_template/addons/variants/mix_release_test.exs +++ b/test/nimble_template/addons/variants/mix_release_test.exs @@ -32,5 +32,51 @@ defmodule NimbleTemplate.Addons.Phoenix.MixReleaseTest do end) end) end + + test "adjusts the config/runtime.exs", %{ + project: project, + test_project_path: test_project_path + } do + in_test_project(test_project_path, fn -> + PhoenixAddons.MixRelease.apply(project) + + assert_file("config/runtime.exs", fn file -> + assert file =~ """ + config :nimble_template, NimbleTemplateWeb.Endpoint, + server: true, + """ + + assert file =~ """ + host = + System.get_env("PHX_HOST") || + raise \"\"\" + Environment variable PHX_HOST is missing. + Set the Heroku endpoint to this variable. + \"\"\" + """ + + refute file =~ """ + + # ## Using releases + # + # If you are doing OTP releases, you need to instruct Phoenix + # to start each relevant endpoint: + # + # config :nimble_template, NimbleTemplateWeb.Endpoint, server: true + # + # Then you can assemble a release by calling `mix release`. + # See `mix help release` for more information. + """ + + refute file =~ """ + # Start the phoenix server if environment is set and running in a release + if System.get_env("PHX_SERVER") && System.get_env("RELEASE_NAME") do + config :nimble_template, NimbleTemplateWeb.Endpoint, server: true + end + + """ + end) + end) + end end end