Skip to content

Commit

Permalink
Don't use runtime config in plugs (#486)
Browse files Browse the repository at this point in the history
`init/1` will be called at compile-time in production
  • Loading branch information
doughsay authored Feb 10, 2023
1 parent e8e5ce8 commit 8eac6a1
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions lib/ambry_web/plugs/first_time_setup.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ defmodule AmbryWeb.Plugs.FirstTimeSetup do
alias Plug.Conn

@impl Plug
@spec init([]) :: boolean()
def init([]) do
Application.get_env(:ambry, :first_time_setup, false)
end
@spec init([]) :: []
def init([]), do: []

@impl Plug
@spec call(Conn.t(), boolean) :: Conn.t()
def call(conn, false), do: conn

def call(%Conn{path_info: ["first_time_setup" | _]} = conn, true), do: conn

def call(conn, true) do
conn
|> redirect(to: Routes.first_time_setup_setup_index_path(conn, :index))
|> Conn.halt()
@spec call(Conn.t(), []) :: Conn.t()
def call(conn, []) do
first_time_setup? = Application.get_env(:ambry, :first_time_setup, false)
setup_path? = match?(%Conn{path_info: ["first_time_setup" | _]}, conn)

if first_time_setup? and not setup_path? do
conn
|> redirect(to: Routes.first_time_setup_setup_index_path(conn, :index))
|> Conn.halt()
else
conn
end
end
end

0 comments on commit 8eac6a1

Please sign in to comment.