diff --git a/lib/myapp/application.ex b/lib/myapp/application.ex index a133874..9cfe28f 100644 --- a/lib/myapp/application.ex +++ b/lib/myapp/application.ex @@ -14,6 +14,9 @@ defmodule MyApp.Application do supervisor(MyAppWeb.Endpoint, []), # Start your own worker by calling: MyApp.Worker.start_link(arg1, arg2, arg3) # worker(MyApp.Worker, [arg1, arg2, arg3]), + + # start cachex for key/value store + worker(Cachex, [:myapp, []]) ] # See https://hexdocs.pm/elixir/Supervisor.html diff --git a/lib/myapp_web/controllers/page_controller.ex b/lib/myapp_web/controllers/page_controller.ex new file mode 100644 index 0000000..6dbc396 --- /dev/null +++ b/lib/myapp_web/controllers/page_controller.ex @@ -0,0 +1,22 @@ +defmodule MyAppWeb.PageController do + use MyAppWeb, :controller + + # serve index.html - cache the file after reading it to minimize IO + @spec index(map, map) :: any + def index(conn, _params) do + + file = Cachex.get(:myapp, "index.html") + |> get_file + + conn + |> html(file) + end + + defp get_file({:ok, data}), do: data + defp get_file({:missing, _}) do + file = File.read!("./priv/static/index.html") + Cachex.set(:myapp, "index.html", file) + file + end + +end diff --git a/lib/myapp_web/endpoint.ex b/lib/myapp_web/endpoint.ex index f5a5f72..1b4874c 100644 --- a/lib/myapp_web/endpoint.ex +++ b/lib/myapp_web/endpoint.ex @@ -8,8 +8,9 @@ defmodule MyAppWeb.Endpoint do # You should set gzip to true if you are running phoenix.digest # when deploying your static files in production. plug Plug.Static, - at: "/", from: :myapp, gzip: false, - only: ~w(css fonts images js favicon.ico robots.txt) + at: "/", from: :myapp, gzip: true + # serve everything in the static folder + # only: ~w(css *.css fonts images js favicon.ico robots.txt) # Code reloading can be explicitly enabled under the # :code_reloader configuration of your endpoint. diff --git a/lib/myapp_web/router.ex b/lib/myapp_web/router.ex index 510ba1f..e19fde4 100644 --- a/lib/myapp_web/router.ex +++ b/lib/myapp_web/router.ex @@ -54,4 +54,9 @@ defmodule MyAppWeb.Router do end end + # catch all for serving single page web app + scope "/*all", MyAppWeb do + get "/", PageController, :index + end + end diff --git a/mix.exs b/mix.exs index 0c212d2..2c81cf2 100644 --- a/mix.exs +++ b/mix.exs @@ -46,6 +46,7 @@ defmodule MyApp.Mixfile do {:httpoison, "~> 0.13"}, {:dialyxir, "~> 0.5", only: [:dev], runtime: false}, {:ex_guard, "~> 1.3", only: :dev}, + {:cachex, "~> 2.1"}, ] end diff --git a/mix.lock b/mix.lock index 1037af6..5309f38 100644 --- a/mix.lock +++ b/mix.lock @@ -1,5 +1,6 @@ %{"argon2_elixir": {:hex, :argon2_elixir, "1.2.14", "0fc4bfbc1b7e459954987d3d2f3836befd72d63f3a355e3978f5005dd6e80816", [], [{:elixir_make, "~> 0.4", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm"}, "base64url": {:hex, :base64url, "0.0.1", "36a90125f5948e3afd7be97662a1504b934dd5dac78451ca6e9abf85a10286be", [], [], "hexpm"}, + "cachex": {:hex, :cachex, "2.1.0", "fad49b4e78d11c6c314e75bd8c9408f5b78cb065c047442798caed10803ee3be", [], [{:eternal, "~> 1.1", [hex: :eternal, repo: "hexpm", optional: false]}], "hexpm"}, "certifi": {:hex, :certifi, "2.0.0", "a0c0e475107135f76b8c1d5bc7efb33cd3815cb3cf3dea7aefdd174dabead064", [], [], "hexpm"}, "comeonin": {:hex, :comeonin, "4.0.3", "4e257dcb748ed1ca2651b7ba24fdbd1bd24efd12482accf8079141e3fda23a10", [], [{:argon2_elixir, "~> 1.2", [hex: :argon2_elixir, repo: "hexpm", optional: true]}, {:bcrypt_elixir, "~> 0.12.1 or ~> 1.0", [hex: :bcrypt_elixir, repo: "hexpm", optional: true]}, {:pbkdf2_elixir, "~> 0.12", [hex: :pbkdf2_elixir, repo: "hexpm", optional: true]}], "hexpm"}, "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [], [], "hexpm"}, @@ -10,6 +11,7 @@ "dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [], [], "hexpm"}, "ecto": {:hex, :ecto, "2.2.7", "2074106ff4a5cd9cb2b54b12ca087c4b659ddb3f6b50be4562883c1d763fb031", [], [{:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"}, "elixir_make": {:hex, :elixir_make, "0.4.0", "992f38fabe705bb45821a728f20914c554b276838433349d4f2341f7a687cddf", [], [], "hexpm"}, + "eternal": {:hex, :eternal, "1.2.0", "e2a6b6ce3b8c248f7dc31451aefca57e3bdf0e48d73ae5043229380a67614c41", [], [], "hexpm"}, "ex_guard": {:hex, :ex_guard, "1.3.0", "0f5c50b90a7e4c599b45d02448ae53eabffc33adb7bfdfc5f5507715e7662a25", [], [{:fs, "~> 0.9", [hex: :fs, repo: "hexpm", optional: false]}], "hexpm"}, "fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [], [], "hexpm"}, "gettext": {:hex, :gettext, "0.14.0", "1a019a2e51d5ad3d126efe166dcdf6563768e5d06c32a99ad2281a1fa94b4c72", [], [], "hexpm"}, diff --git a/priv/static/index.html b/priv/static/index.html new file mode 100644 index 0000000..7a844c6 --- /dev/null +++ b/priv/static/index.html @@ -0,0 +1,5 @@ + + + Test 123 + + diff --git a/test/myapp_web/controllers/page_controller_test.exs b/test/myapp_web/controllers/page_controller_test.exs new file mode 100644 index 0000000..8ba4de5 --- /dev/null +++ b/test/myapp_web/controllers/page_controller_test.exs @@ -0,0 +1,11 @@ +defmodule MyAppWeb.PageControllerTest do + use MyAppWeb.ConnCase, async: true + + test "index.html should be cached" do + assert Cachex.get(:myapp, "index.html") == {:missing, nil} + conn = build_conn() |> get("/lsakdjfl") + {ok, _} = Cachex.get(:myapp, "index.html") + assert ok == :ok + end + +end diff --git a/test/myapp_web/controllers/thread_controller_test.exs b/test/myapp_web/controllers/thread_controller_test.exs index eb01afe..f056655 100644 --- a/test/myapp_web/controllers/thread_controller_test.exs +++ b/test/myapp_web/controllers/thread_controller_test.exs @@ -1,6 +1,5 @@ defmodule MyAppWeb.ThreadControllerTest do use MyAppWeb.ConnCase, async: true - import MyAppWeb.ThreadController import MyApp.Data.TestHelpers test "insert new thread should fail" do