web: bundle templates in Controller module at compile time

This commit is contained in:
Timothée Floure 2022-02-19 10:35:47 +01:00
parent 1b44032445
commit a2b4c5cea8
Signed by: tfloure
GPG key ID: 4502C902C00A1E12
2 changed files with 7 additions and 5 deletions

View file

@ -15,6 +15,7 @@ defmodule HAHandler do
def acme_challenge_path, do: Application.get_env(@otp_app, :acme_challenge_path) def acme_challenge_path, do: Application.get_env(@otp_app, :acme_challenge_path)
def static_path(), do: Application.app_dir(@otp_app, "priv/static/") def static_path(), do: Application.app_dir(@otp_app, "priv/static/")
def template_dir(), do: "lib/ha_handler/web/templates"
def otp_app(), do: @otp_app def otp_app(), do: @otp_app
def version(), do: @version def version(), do: @version

View file

@ -3,12 +3,13 @@ defmodule HAHandler.Web.Controller do
alias HAHandler.{HAProxy, PGSQL} alias HAHandler.{HAProxy, PGSQL}
@templates_dir "lib/ha_handler/web/templates" @template_dir "lib/ha_handler/web/templates"
@index_template EEx.compile_file(
Path.join(@template_dir, "index.html.eex")
)
defp render(conn, template, assigns) do defp render(conn, template, assigns) do
template_path = Path.join(@templates_dir, template) {body, _binding} = Code.eval_quoted(template, assigns)
quoted = EEx.compile_file(template_path)
{body, _binding} = Code.eval_quoted(quoted, assigns)
conn conn
|> put_resp_content_type("text/html") |> put_resp_content_type("text/html")
@ -28,6 +29,6 @@ defmodule HAHandler.Web.Controller do
version: HAHandler.version(), version: HAHandler.version(),
env: HAHandler.env() env: HAHandler.env()
] ]
render(conn, "index.html.eex", assigns) render(conn, @index_template, assigns)
end end
end end