Skip to content

Commit

Permalink
Merge pull request #173 from nimblehq/chore/enable-ssl-for-ecto-repo-…
Browse files Browse the repository at this point in the history
…on-heroku

Adjust the Heroku, Release, and Assets addon to make the app deployable
  • Loading branch information
andyduong1920 authored Mar 4, 2022
2 parents 479a098 + 77be419 commit c6b8838
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 30 deletions.
20 changes: 19 additions & 1 deletion lib/nimble_template/addons/github.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
61 changes: 60 additions & 1 deletion lib/nimble_template/addons/variants/phoenix/mix_release.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
1 change: 0 additions & 1 deletion priv/templates/nimble_template/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ README.md
cover
**/tmp/
doc
/priv/static/
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
### Endpoint configuration

- `HEALTH_PATH`: Health path (eg: "/_health")
- `PHX_HOST`: The application endpoint.
- `SECRET_KEY_BASE`: Secret key base
30 changes: 3 additions & 27 deletions priv/templates/nimble_template/.github/workflows/README.md.eex
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,16 @@ 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

- 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,
Expand Down
38 changes: 38 additions & 0 deletions test/nimble_template/addons/github_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions test/nimble_template/addons/variants/mix_release_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c6b8838

Please sign in to comment.