web: evaluated static paths at runtime

This commit is contained in:
Timothée Floure 2022-02-19 11:39:04 +01:00
parent a2b4c5cea8
commit 8cc771cf65
Signed by: tfloure
GPG key ID: 4502C902C00A1E12
4 changed files with 23 additions and 13 deletions

View file

@ -15,9 +15,21 @@ defmodule HAHandler do
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 template_dir(), do: "lib/ha_handler/web/templates"
def otp_app(), do: @otp_app
def version(), do: @version
def env(), do: @env
def acme_challenges_static_config() do
[
at: "/.well-known/acme-challenge/",
from: acme_challenge_path()
]
end
def assets_static_config() do
[
at: "/static",
from: static_path()
]
end
end

View file

@ -8,25 +8,21 @@ defmodule HAHandler.Web.Router do
use Plug.Router
import HAHandler, only: [acme_challenge_path: 0, static_path: 0]
alias HAHandler.Web.Controller
# Note for plugs: oder is important, as a plug may stop
# want to stop the pipeline!
plug Plug.Logger, log: :debug
# Note: `plug` is a macro, hence evaluated at
# compile-time. The configuration (which path to
# serve) cannot be modified at runtime with this
# system.
plug Plug.Static,
at: "/.well-known/acme-challenge/",
from: acme_challenge_path()
plug Plug.Static,
at: "/static",
from: static_path()
# We use replug to allow for runtime configuration in release (as macros such
# as the `plug` call ae evaluated are compile-time).
plug Replug,
plug: Plug.Static,
opts: {HAHandler, :acme_challenges_static_config}
plug Replug,
plug: Plug.Static,
opts: {HAHandler, :assets_static_config}
plug :match
plug :dispatch

View file

@ -25,6 +25,7 @@ defmodule HAHandler.MixProject do
[
{:plug, "~> 1.12"},
{:plug_cowboy, "~> 2.5"},
{:replug, "~> 0.1.0"},
{:procket, "~> 0.9"},
{:poison, "~> 5.0"},
{:postgrex, "~> 0.16.1"}

View file

@ -14,6 +14,7 @@
"postgrex": {:hex, :postgrex, "0.16.1", "f94628a32c571266f53cd1e5fca705e626e2417bf1eee6f868985d14e874160a", [:mix], [{:connection, "~> 1.1", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "6b225df32c857b9430619dbe30200a7ae664e23415a771ae9209396ee8eeee64"},
"procket": {:hex, :procket, "0.9.5", "ce91c00cb3f4d627fb3827f46ce68e6a969ea265df1afb349c300a07be4bb16c", [:rebar3], [], "hexpm", "1288bba5eb6f08ea5df3dda9e051bdac8f4b1e50adcca5cf4d726e825ad81bed"},
"ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"},
"replug": {:hex, :replug, "0.1.0", "61d35f8c873c0078a23c49579a48f36e45789414b1ec0daee3fd5f4e34221f23", [:mix], [{:plug, "~> 1.8", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "f71f7a57e944e854fe4946060c6964098e53958074c69fb844b96e0bd58cfa60"},
"sweet_xml": {:hex, :sweet_xml, "0.7.2", "4729f997286811fabdd8288f8474e0840a76573051062f066c4b597e76f14f9f", [:mix], [], "hexpm", "6894e68a120f454534d99045ea3325f7740ea71260bc315f82e29731d570a6e8"},
"telemetry": {:hex, :telemetry, "1.0.0", "0f453a102cdf13d506b7c0ab158324c337c41f1cc7548f0bc0e130bbf0ae9452", [:rebar3], [], "hexpm", "73bc09fa59b4a0284efb4624335583c528e07ec9ae76aca96ea0673850aec57a"},
}