move more fully to runtime config with release, improved docker build, updated phx liveview js

This commit is contained in:
Adam Piontek 2023-01-28 08:14:32 -05:00
parent db9f127e7b
commit 573a1e9cfe
38 changed files with 428 additions and 205 deletions

View file

@ -7,12 +7,32 @@ defmodule Shift73k do
if it comes from the database, an external API or others.
"""
@app_vars Application.compile_env(:shift73k, :app_global_vars, time_zone: "America/New_York")
@app_time_zone @app_vars[:time_zone]
@weekdays [:monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday]
def app_time_zone, do: @app_time_zone
defp get_app_config_env do
Application.get_env(:shift73k, :app_global_vars,
time_zone: "America/New_York",
allow_registration: :true,
mailer_reply_to: "admin@example.com",
mailer_from: {"Shift73k", "shift73k@example.com"}
)
end
def weekdays, do: @weekdays
def get_app_time_zone, do:
get_app_config_env() |> Keyword.fetch!(:time_zone) |> IO.inspect(label: "time_zone", pretty: :true)
def get_app_mailer_from, do:
get_app_config_env() |> Keyword.fetch!(:mailer_from) |> IO.inspect(label: "mailer_from", pretty: :true)
def get_app_mailer_reply_to, do:
get_app_config_env() |> Keyword.fetch!(:mailer_reply_to) |> IO.inspect(label: "mailer_reply_to", pretty: :true)
def get_app_allow_reg, do:
get_app_config_env() |> Keyword.fetch!(:allow_registration) |> get_app_allow_reg()
|> IO.inspect(label: "allow_registration", pretty: :true)
def get_app_allow_reg("false"), do: :false
def get_app_allow_reg(:false), do: :false
def get_app_allow_reg(_not_false), do: :true
end

View file

@ -1,16 +1,13 @@
defmodule Shift73k.Mailer.UserEmail do
import Swoosh.Email
import Shift73k, only: [get_app_mailer_from: 0, get_app_mailer_reply_to: 0]
@mailer_vars Application.compile_env(:shift73k, :app_global_vars,
mailer_reply_to: "admin@example.com",
mailer_from: {"Shift73k", "shift73k@example.com"}
)
def compose(user_email, subject, body_text) do
new()
|> from(@mailer_vars[:mailer_from])
|> from(get_app_mailer_from())
|> to(user_email)
|> header("Reply-To", @mailer_vars[:mailer_reply_to])
|> header("Reply-To", get_app_mailer_reply_to())
|> subject(subject)
|> text_body(body_text)
end

28
lib/shift73k/release.ex Normal file
View file

@ -0,0 +1,28 @@
defmodule Shift73k.Release do
@moduledoc """
Used for executing DB release tasks when run in production without Mix
installed.
"""
@app :shift73k
def migrate do
load_app()
for repo <- repos() do
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
end
end
def rollback(repo, version) do
load_app()
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
end
defp repos do
Application.fetch_env!(@app, :ecto_repos)
end
defp load_app do
Application.load(@app)
end
end

View file

@ -1,7 +1,7 @@
defmodule Shift73k.Shifts.Templates.ShiftTemplate do
use Ecto.Schema
import Ecto.Changeset
import Shift73k, only: [app_time_zone: 0]
import Shift73k, only: [get_app_time_zone: 0]
alias Shift73k.Shifts
alias Shift73k.Shifts.Templates.ShiftTemplate
@ -12,7 +12,7 @@ defmodule Shift73k.Shifts.Templates.ShiftTemplate do
field :subject, :string
field :description, :string
field :location, :string
field :time_zone, :string, default: app_time_zone()
field :time_zone, :string, default: get_app_time_zone()
field :time_start, :time, default: ~T[09:00:00]
field :time_end, :time, default: ~T[17:00:00]

View file

@ -1,6 +1,6 @@
defmodule Shift73kWeb.ShiftAssignLive.Index do
use Shift73kWeb, :live_view
import Shift73k, only: [app_time_zone: 0]
import Shift73k, only: [get_app_time_zone: 0]
alias Shift73k.Repo
alias Shift73k.Shifts

View file

@ -127,7 +127,7 @@
disabled: @shift_template.id != @custom_shift.id,
phx_debounce: 250,
list: "tz_list",
placeholder: "Default: #{app_time_zone()}"
placeholder: "Default: #{get_app_time_zone()}"
%>
<datalist id="tz_list">
<%= for tz_name <- Tzdata.zone_list() do %>

View file

@ -33,7 +33,7 @@
<div class="inner-addon left-addon mb-3">
<i class="bi bi-map icon is-left text-muted fs-5"></i>
<%= text_input iimf, :time_zone,
value: Shift73k.app_time_zone(),
value: Shift73k.get_app_time_zone(),
class: @tz_valid && "form-control" || "form-control is-invalid",
phx_debounce: 250,
aria_describedby: "ics-import-tz-error",

View file

@ -1,6 +1,6 @@
defmodule Shift73kWeb.ShiftTemplateLive.FormComponent do
use Shift73kWeb, :live_component
import Shift73k, only: [app_time_zone: 0]
import Shift73k, only: [get_app_time_zone: 0]
alias Shift73k.Shifts.Templates
alias Shift73k.Shifts.Templates.ShiftTemplate

View file

@ -88,7 +88,7 @@
class: input_class(f, :time_zone, "form-control"),
phx_debounce: 250,
list: "tz_list",
placeholder: "Default: #{app_time_zone()}"
placeholder: "Default: #{get_app_time_zone()}"
%>
<datalist id="tz_list">
<%= for tz_name <- Tzdata.zone_list() do %>

View file

@ -1,11 +1,11 @@
defmodule Shift73kWeb.UserLive.ResetPassword do
use Shift73kWeb, :live_view
import Shift73k, only: [get_app_allow_reg: 0]
alias Shift73k.Accounts
alias Shift73k.Accounts.User
@app_vars Application.compile_env(:shift73k, :app_global_vars, allow_registration: :true)
@app_allow_registration @app_vars[:allow_registration]
@impl true
def mount(_params, session, socket) do
@ -41,5 +41,5 @@ defmodule Shift73kWeb.UserLive.ResetPassword do
end
end
def allow_registration, do: @app_allow_registration
def allow_registration, do: get_app_allow_reg()
end

View file

@ -198,7 +198,7 @@ defmodule Shift73kWeb.UserManagementLive.Index do
def dt_out(ndt) do
ndt
|> DateTime.from_naive!(Shift73k.app_time_zone())
|> DateTime.from_naive!(Shift73k.get_app_time_zone())
|> Calendar.strftime("%Y %b %-d, %-I:%M %p")
end
end

View file

@ -4,12 +4,11 @@ defmodule Shift73kWeb.EnsureAllowRegistrationPlug do
"""
import Plug.Conn
import Phoenix.Controller
import Shift73k, only: [get_app_allow_reg: 0]
alias Shift73k.Repo
alias Shift73k.Accounts.User
@app_vars Application.compile_env(:shift73k, :app_global_vars, allow_registration: :true)
@app_allow_registration @app_vars[:allow_registration]
@doc false
@spec init(any()) :: any()
@ -19,7 +18,7 @@ defmodule Shift73kWeb.EnsureAllowRegistrationPlug do
@spec call(Conn.t(), atom() | [atom()]) :: Conn.t()
def call(conn, _opts) do
# If there aren't even any users, or registration is allowed
if !Repo.exists?(User) || @app_allow_registration do
if !Repo.exists?(User) || get_app_allow_reg() do
# We will allow registration
conn
else

View file

@ -12,13 +12,13 @@
<% end %>
</li>
<li>
<%= link nav_link_opts(@conn, to: Routes.shift_index_path(@conn, :index), class: "dropdown-item") do %>
<i class="bi bi-card-list me-1"></i> My Scheduled Shifts
<%= link nav_link_opts(@conn, to: Routes.shift_template_index_path(@conn, :index), class: "dropdown-item") do %>
<i class="bi bi-clock-history me-1"></i> My Shift Templates
<% end %>
</li>
<li>
<%= link nav_link_opts(@conn, to: Routes.shift_template_index_path(@conn, :index), class: "dropdown-item") do %>
<i class="bi bi-clock-history me-1"></i> My Shift Templates
<%= link nav_link_opts(@conn, to: Routes.shift_index_path(@conn, :index), class: "dropdown-item") do %>
<i class="bi bi-card-list me-1"></i> My Scheduled Shifts
<% end %>
</li>

View file

@ -1,11 +1,10 @@
defmodule Shift73kWeb.LayoutView do
use Shift73kWeb, :view
import Shift73k, only: [get_app_allow_reg: 0]
alias Shift73k.Repo
alias Shift73k.Accounts.User
alias Shift73kWeb.Roles
@app_vars Application.compile_env(:shift73k, :app_global_vars, allow_registration: :true)
@app_allow_registration @app_vars[:allow_registration]
# With a Vite.js-based workflow, we will import different asset files in development
# and in production builds. Therefore, we will need a way to conditionally render
@ -14,7 +13,7 @@ defmodule Shift73kWeb.LayoutView do
@env Mix.env() # remember value at compile time
def dev_env?, do: @env == :dev
def allow_registration, do: @app_allow_registration
def allow_registration, do: get_app_allow_reg()
def nav_link_opts(conn, opts) do
case Keyword.get(opts, :to) == Phoenix.Controller.current_path(conn) do

View file

@ -1,9 +1,7 @@
defmodule Shift73kWeb.UserConfirmationView do
use Shift73kWeb, :view
import Shift73k, only: [get_app_allow_reg: 0]
alias Shift73k.Accounts.User
@app_vars Application.compile_env(:shift73k, :app_global_vars, allow_registration: :true)
@app_allow_registration @app_vars[:allow_registration]
def allow_registration, do: @app_allow_registration
def allow_registration, do: get_app_allow_reg()
end

View file

@ -1,9 +1,7 @@
defmodule Shift73kWeb.UserResetPasswordView do
use Shift73kWeb, :view
import Shift73k, only: [get_app_allow_reg: 0]
alias Shift73k.Accounts.User
@app_vars Application.compile_env(:shift73k, :app_global_vars, allow_registration: :true)
@app_allow_registration @app_vars[:allow_registration]
def allow_registration, do: @app_allow_registration
def allow_registration, do: get_app_allow_reg()
end

View file

@ -1,9 +1,7 @@
defmodule Shift73kWeb.UserSessionView do
use Shift73kWeb, :view
import Shift73k, only: [get_app_allow_reg: 0]
alias Shift73k.Accounts.User
@app_vars Application.compile_env(:shift73k, :app_global_vars, allow_registration: :true)
@app_allow_registration @app_vars[:allow_registration]
def allow_registration, do: @app_allow_registration
def allow_registration, do: get_app_allow_reg()
end