Added plug to strip file extensions
This commit is contained in:
parent
566941228c
commit
0d9003cffd
2 changed files with 30 additions and 0 deletions
|
|
@ -47,5 +47,33 @@ defmodule PinchflatWeb.Endpoint do
|
|||
plug Plug.MethodOverride
|
||||
plug Plug.Head
|
||||
plug Plug.Session, @session_options
|
||||
|
||||
plug :strip_trailing_extension
|
||||
|
||||
plug PinchflatWeb.Router
|
||||
|
||||
defp strip_trailing_extension(%{path_info: []} = conn, _opts), do: conn
|
||||
|
||||
defp strip_trailing_extension(conn, _opts) do
|
||||
path =
|
||||
conn.path_info
|
||||
|> List.last()
|
||||
|> String.split(".")
|
||||
|> Enum.reverse()
|
||||
|
||||
case path do
|
||||
[_] ->
|
||||
conn
|
||||
|
||||
[_format | fragments] ->
|
||||
new_path =
|
||||
fragments
|
||||
|> Enum.reverse()
|
||||
|> Enum.join(".")
|
||||
|
||||
path_fragments = List.replace_at(conn.path_info, -1, new_path)
|
||||
|
||||
%{conn | path_info: path_fragments}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
defmodule PinchflatWeb.Router do
|
||||
use PinchflatWeb, :router
|
||||
|
||||
# IMPORTANT: `strip_trailing_extension` in endpoint.ex removes
|
||||
# the extension from the path
|
||||
pipeline :browser do
|
||||
plug :basic_auth
|
||||
plug :accepts, ["html"]
|
||||
|
|
|
|||
Loading…
Reference in a new issue