mirror of
https://github.com/mgerb/classic-wow-forums
synced 2026-01-12 01:52:49 +00:00
server - added distillery for prod builds
This commit is contained in:
73
lib/myapp/release_tasks.ex
Normal file
73
lib/myapp/release_tasks.ex
Normal file
@@ -0,0 +1,73 @@
|
||||
defmodule MyApp.ReleaseTasks do
|
||||
|
||||
@start_apps [
|
||||
:crypto,
|
||||
:ssl,
|
||||
:postgrex,
|
||||
:ecto
|
||||
]
|
||||
|
||||
def myapp, do: :myapp
|
||||
|
||||
def repos, do: Application.get_env(myapp(), :ecto_repos, [])
|
||||
|
||||
def seed do
|
||||
# Run migrations
|
||||
migrate()
|
||||
|
||||
# Run seed script
|
||||
Enum.each(repos(), &run_seeds_for/1)
|
||||
|
||||
# Signal shutdown
|
||||
IO.puts "Success!"
|
||||
:init.stop()
|
||||
end
|
||||
|
||||
defp prepare do
|
||||
me = myapp()
|
||||
|
||||
IO.puts "Loading #{me}.."
|
||||
# Load the code for myapp, but don't start it
|
||||
:ok = Application.load(me)
|
||||
|
||||
IO.puts "Starting dependencies.."
|
||||
# Start apps necessary for executing migrations
|
||||
Enum.each(@start_apps, &Application.ensure_all_started/1)
|
||||
|
||||
# Start the Repo(s) for myapp
|
||||
IO.puts "Starting repos.."
|
||||
Enum.each(repos(), &(&1.start_link(pool_size: 1)))
|
||||
end
|
||||
|
||||
def migrate do
|
||||
prepare()
|
||||
Enum.each(repos(), &run_migrations_for/1)
|
||||
end
|
||||
|
||||
def priv_dir(app), do: "#{:code.priv_dir(app)}"
|
||||
|
||||
defp run_migrations_for(repo) do
|
||||
app = Keyword.get(repo.config, :otp_app)
|
||||
IO.puts "Running migrations for #{app}"
|
||||
Ecto.Migrator.run(repo, migrations_path(repo), :up, all: true)
|
||||
end
|
||||
|
||||
def run_seeds_for(repo) do
|
||||
# Run the seed script if it exists
|
||||
seed_script = seeds_path(repo)
|
||||
if File.exists?(seed_script) do
|
||||
IO.puts "Running seed script.."
|
||||
Code.eval_file(seed_script)
|
||||
end
|
||||
end
|
||||
|
||||
def migrations_path(repo), do: priv_path_for(repo, "migrations")
|
||||
|
||||
def seeds_path(repo), do: priv_path_for(repo, "seeds.exs")
|
||||
|
||||
def priv_path_for(repo, filename) do
|
||||
app = Keyword.get(repo.config, :otp_app)
|
||||
repo_underscore = repo |> Module.split |> List.last |> Macro.underscore
|
||||
Path.join([priv_dir(app), repo_underscore, filename])
|
||||
end
|
||||
end
|
||||
@@ -6,11 +6,11 @@ defmodule MyAppWeb.PageController do
|
||||
def index(conn, _params) do
|
||||
|
||||
# cache index.html if prod
|
||||
file = case Mix.env do
|
||||
:dev -> File.read!("./priv/static/index.html")
|
||||
_ ->
|
||||
file = case System.get_env("MIX_ENV") do
|
||||
:prod ->
|
||||
file = Cachex.get(:myapp, "index.html")
|
||||
|> get_file
|
||||
_ -> File.read!(Application.app_dir(:myapp, "priv/static/index.html"))
|
||||
end
|
||||
|
||||
conn
|
||||
@@ -19,7 +19,7 @@ defmodule MyAppWeb.PageController do
|
||||
|
||||
defp get_file({:ok, data}), do: data
|
||||
defp get_file({:missing, _}) do
|
||||
file = File.read!("./priv/static/index.html")
|
||||
file = File.read!(Application.app_dir(:myapp, "priv/static/index.html"))
|
||||
Cachex.set(:myapp, "index.html", file)
|
||||
file
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user