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

PR: Ping to Wake Heroku Dyno #39

Merged
merged 6 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ from scratch.

### Try it on Heroku: [phxtodo.herokuapp.com](https://phxtodo.herokuapp.com)

<!-- wake heroku app before visit. see: https://github.com/dwyl/ping -->
![wake-sleeping-heroku-app](https://phxtodo.herokuapp.com/ping)

Try the Heroku version.
Add a few items to the list and test the functionality.

Expand Down
8 changes: 8 additions & 0 deletions lib/app_web/controllers/ping_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule AppWeb.PingController do
use AppWeb, :controller

# see: github.com/dwyl/ping
def ping(conn, params) do
Ping.render_pixel(conn, params)
end
end
13 changes: 4 additions & 9 deletions lib/app_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,20 @@ defmodule AppWeb.Router do
plug :put_secure_browser_headers
end

pipeline :api do
plug :accepts, ["json"]
end

scope "/", AppWeb do
pipe_through :browser

# see: https://github.com/dwyl/ping
get "/ping", PingController, :ping

get "/", ItemController, :index
resources "/items", ItemController
get "/items/toggle/:id", ItemController, :toggle
get "/clear", ItemController, :clear_completed
# this route will "catch all" so has to be last:
get "/:filter", ItemController, :index
end

# Other scopes may use custom stacks.
# scope "/api", AppWeb do
# pipe_through :api
# end

# Enables LiveDashboard only for development
#
# If you want to use the LiveDashboard in production, you should put
Expand Down
2 changes: 1 addition & 1 deletion lib/app_web/views/item_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ defmodule AppWeb.ItemView do

# check if there are items with status 0 or 1, used to hide/show footer
def got_items?(items) do
Enum.filter(items, fn i -> i.status < 2 end) |> Enum.count > 0
Enum.filter(items, fn i -> i.status < 2 end) |> Enum.count() > 0
end
end
3 changes: 3 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ defmodule App.MixProject do
{:jason, "~> 1.0"},
{:plug_cowboy, "~> 2.0"},

# ping to wake the ("Free") Heroku dyno: https://github.com/dwyl/ping
{:ping, "~> 1.0.1"},

# Test Code Coverage:
{:excoveralls, "~> 0.12.2", only: :test}
]
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"phoenix_live_reload": {:hex, :phoenix_live_reload, "1.2.2", "38d94c30df5e2ef11000697a4fbe2b38d0fbf79239d492ff1be87bbc33bc3a84", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "a3dec3d28ddb5476c96a7c8a38ea8437923408bc88da43e5c45d97037b396280"},
"phoenix_live_view": {:hex, :phoenix_live_view, "0.12.1", "42f591c781edbf9fab921319076b7ac635d43aa23e6748d2644563326236d7e4", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.4.16 or ~> 1.5.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14", [hex: :phoenix_html, repo: "hexpm", optional: false]}], "hexpm", "585321e98df1cd5943e370b9784e950a37ca073744eb534660c9048967c52ab6"},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "2.0.0", "a1ae76717bb168cdeb10ec9d92d1480fec99e3080f011402c0a2d68d47395ffb", [:mix], [], "hexpm", "c52d948c4f261577b9c6fa804be91884b381a7f8f18450c5045975435350f771"},
"ping": {:hex, :ping, "1.0.1", "1f44c7b7151af18627edbd7946a376935871f285fc9c11e9f16ddb1555ea65b5", [:mix], [{:plug, "~> 1.10.1", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "c1421bebcb6601e7fb1158700ca99eb2dc0976b45fe2a26023db5a9318aadfa6"},
"plug": {:hex, :plug, "1.10.1", "c56a6d9da7042d581159bcbaef873ba9d87f15dce85420b0d287bca19f40f9bd", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "b5cd52259817eb8a31f2454912ba1cff4990bca7811918878091cb2ab9e52cb8"},
"plug_cowboy": {:hex, :plug_cowboy, "2.2.2", "7a09aa5d10e79b92d332a288f21cc49406b1b994cbda0fde76160e7f4cc890ea", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e82364b29311dbad3753d588febd7e5ef05062cd6697d8c231e0e007adab3727"},
"plug_crypto": {:hex, :plug_crypto, "1.1.2", "bdd187572cc26dbd95b87136290425f2b580a116d3fb1f564216918c9730d227", [:mix], [], "hexpm", "6b8b608f895b6ffcfad49c37c7883e8df98ae19c6a28113b02aa1e9c5b22d6b5"},
Expand Down
14 changes: 13 additions & 1 deletion test/app_web/controllers/item_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ defmodule AppWeb.ItemControllerTest do
test "deletes chosen item", %{conn: conn, item: item} do
conn = delete(conn, Routes.item_path(conn, :delete, item))
conn = get(conn, Routes.item_path(conn, :show, item))
assert html_response(conn, 200) =~ "2" # Status: 2
# Status: 2
assert html_response(conn, 200) =~ "2"
end
end

Expand All @@ -99,6 +100,17 @@ defmodule AppWeb.ItemControllerTest do
end
end

describe "clear_completed/2" do
setup [:create_item]

test "clear_completed/2", %{conn: conn, item: item} do
get(conn, Routes.item_path(conn, :toggle, item.id))
conn = get(conn, Routes.item_path(conn, :clear_completed))
updated_item = App.Todo.get_item!(item.id)
assert updated_item.status == 2
end
end

defp create_item(_) do
item = fixture(:item)
%{item: item}
Expand Down
11 changes: 11 additions & 0 deletions test/app_web/controllers/ping_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule AppWeb.PingControllerTest do
use AppWeb.ConnCase

# see: https://github.com/dwyl/ping
test "GET /ping (GIF) renders 1x1 pixel", %{conn: conn} do
conn = get(conn, Routes.ping_path(conn, :ping))
assert conn.status == 200
assert conn.state == :sent
assert conn.resp_body =~ <<71, 73, 70, 56, 57>>
end
end
8 changes: 5 additions & 3 deletions test/app_web/views/item_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ defmodule AppWeb.ItemViewTest do

test "pluralise/1 returns item for 1 item and items for < 1 <" do
assert ItemView.pluralise([%{text: "one", status: 0}]) == "item"

assert ItemView.pluralise([
%{text: "one", status: 0},
%{text: "two", status: 0}
]) == "items"
%{text: "one", status: 0},
%{text: "two", status: 0}
]) == "items"

assert ItemView.pluralise([%{text: "one", status: 1}]) == "items"
end
end