mirror of
https://github.com/mgerb/classic-wow-forums
synced 2026-01-12 01:52:49 +00:00
prepare router to serve single page web app
This commit is contained in:
@@ -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
|
||||
|
||||
22
lib/myapp_web/controllers/page_controller.ex
Normal file
22
lib/myapp_web/controllers/page_controller.ex
Normal file
@@ -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
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user