Skip to content

Instantly share code, notes, and snippets.

@mazz
Last active May 8, 2019 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mazz/9e649bc5f5b0339f5c03fcbf187bc669 to your computer and use it in GitHub Desktop.
Save mazz/9e649bc5f5b0339f5c03fcbf187bc669 to your computer and use it in GitHub Desktop.
...
config :my_app_api, MyAppApi.Endpoint,
url: [host: "api.%%%%.app"],
secret_key_base: "%%%%",
render_errors: [view: MyAppApi.ErrorView, accepts: ~w(html json)],
pubsub: [name: MyAppApi.PubSub, adapter: Phoenix.PubSub.PG2],
live_view: [signing_salt: "%%%%"]
...
# https://docs.docker.com/compose/compose-file/
version: '3.7'
services:
my_app:
build: .
depends_on:
- postgres
environment:
DATABASE_URL: ecto://my_app:my_app@postgres/my_app
HOSTNAME: localhost
PORT: 80
# mix phx.gen.secret
SECRET_KEY_BASE: %%%%
labels:
- "traefik.frontend.rule=Host:api.%%%%.app"
- "traefik.frontend.entryPoints=http,https"
postgres:
image: postgres:11.0-alpine
environment:
POSTGRES_DB: my_app
POSTGRES_PASSWORD: %%%%
POSTGRES_USER: my_app
ports:
- 15432:5432
volumes:
- postgres-data:/var/lib/postgresql/data
volumes:
postgres-data: {}
networks:
default:
external:
name: web
> docker logs image_id_hash
18:36:14.722 [error] #PID<0.3583.0> running MyAppApi.Endpoint (connection #PID<0.3581.0>, stream id 2) terminated
Server: api.%%%%.app:80 (http)
Request: GET /icons/apple-touch-icon.png
** (exit) an exception was raised:
** (Protocol.UndefinedError) protocol Phoenix.HTML.Safe not implemented for %{error: "unexpected"}. This protocol is implemented for: Decimal, Phoenix.LiveView.Comprehension, Phoenix.LiveView.Rendered, Integer, Date, Float, Phoenix.HTML.Form, Tuple, Atom, NaiveDateTime, DateTime, List, BitString, Time
(phoenix_html) deps/phoenix_html/lib/phoenix_html/safe.ex:1: Phoenix.HTML.Safe.impl_for!/1
(phoenix_html) deps/phoenix_html/lib/phoenix_html/safe.ex:15: Phoenix.HTML.Safe.to_iodata/1
(phoenix) lib/phoenix/controller.ex:729: Phoenix.Controller.__put_render__/5
(my_app_api) lib/my_app_api/endpoint.ex:1: MyAppApi.Endpoint.instrument/4
(phoenix) lib/phoenix/endpoint/render_errors.ex:75: Phoenix.Endpoint.RenderErrors.instrument_render_and_send/5
(phoenix) lib/phoenix/endpoint/render_errors.ex:62: Phoenix.Endpoint.RenderErrors.__catch__/5
(phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:33: Phoenix.Endpoint.Cowboy2Handler.init/2
(cowboy) /app/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
18:36:14.727 [error] #PID<0.3584.0> running MyAppApi.Endpoint (connection #PID<0.3578.0>, stream id 3) terminated
Server: api.%%%%.app:80 (http)
Request: GET /icons/favicon-32x32.png
** (exit) an exception was raised:
** (Protocol.UndefinedError) protocol Phoenix.HTML.Safe not implemented for %{error: "unexpected"}. This protocol is implemented for: Decimal, Phoenix.LiveView.Comprehension, Phoenix.LiveView.Rendered, Integer, Date, Float, Phoenix.HTML.Form, Tuple, Atom, NaiveDateTime, DateTime, List, BitString, Time
(phoenix_html) deps/phoenix_html/lib/phoenix_html/safe.ex:1: Phoenix.HTML.Safe.impl_for!/1
(phoenix_html) deps/phoenix_html/lib/phoenix_html/safe.ex:15: Phoenix.HTML.Safe.to_iodata/1
(phoenix) lib/phoenix/controller.ex:729: Phoenix.Controller.__put_render__/5
(my_app_api) lib/my_app_api/endpoint.ex:1: MyAppApi.Endpoint.instrument/4
(phoenix) lib/phoenix/endpoint/render_errors.ex:75: Phoenix.Endpoint.RenderErrors.instrument_render_and_send/5
(phoenix) lib/phoenix/endpoint/render_errors.ex:62: Phoenix.Endpoint.RenderErrors.__catch__/5
(phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:33: Phoenix.Endpoint.Cowboy2Handler.init/2
(cowboy) /app/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
18:36:14.843 [error] Could not check origin for Phoenix.Socket transport.
Origin of the request: https://api.%%%%.app
This happens when you are attempting a socket connection to
a different host than the one configured in your config/
files. For example, in development the host is configured
to "localhost" but you may be trying to access it from
"127.0.0.1". To fix this issue, you may either:
1. update [url: [host: ...]] to your actual host in the
config file for your current environment (recommended)
2. pass the :check_origin option when configuring your
endpoint or when configuring the transport in your
UserSocket module, explicitly outlining which origins
are allowed:
check_origin: ["https://example.com",
"//another.com:888", "//other.com"]
18:36:15.363 [error] Could not check origin for Phoenix.Socket transport.
01:26:27.023 [error] Could not check origin for Phoenix.Socket transport.
Origin of the request: https://api.%%%%.app
This happens when you are attempting a socket connection to
a different host than the one configured in your config/
files. For example, in development the host is configured
to "localhost" but you may be trying to access it from
"127.0.0.1". To fix this issue, you may either:
1. update [url: [host: ...]] to your actual host in the
config file for your current environment (recommended)
2. pass the :check_origin option when configuring your
endpoint or when configuring the transport in your
UserSocket module, explicitly outlining which origins
are allowed:
check_origin: ["https://example.com",
"//another.com:888", "//other.com"]
01:33:54.659 [error] Postgrex.Protocol (#PID<0.3300.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.3309.0> timed out because it owned the connection for longer than 60000ms (set via the :ownership_timeout option)
02:33:54.662 [error] Postgrex.Protocol (#PID<0.3299.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.3309.0> timed out because it owned the connection for longer than 60000ms (set via the :ownership_timeout option)
03:33:54.664 [error] Postgrex.Protocol (#PID<0.3300.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.3309.0> timed out because it owned the connection for longer than 60000ms (set via the :ownership_timeout option)
04:33:54.667 [error] Postgrex.Protocol (#PID<0.3303.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.3309.0> timed out because it owned the connection for longer than 60000ms (set via the :ownership_timeout option)
05:33:54.670 [error] Postgrex.Protocol (#PID<0.3300.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.3309.0> timed out because it owned the connection for longer than 60000ms (set via the :ownership_timeout option)
06:33:54.673 [error] Postgrex.Protocol (#PID<0.3299.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.3309.0> timed out because it owned the connection for longer than 60000ms (set via the :ownership_timeout option)
07:33:54.677 [error] Postgrex.Protocol (#PID<0.3303.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.3309.0> timed out because it owned the connection for longer than 60000ms (set via the :ownership_timeout option)
08:33:54.681 [error] Postgrex.Protocol (#PID<0.3300.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.3309.0> timed out because it owned the connection for longer than 60000ms (set via the :ownership_timeout option)
09:33:54.685 [error] Postgrex.Protocol (#PID<0.3299.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.3309.0> timed out because it owned the connection for longer than 60000ms (set via the :ownership_timeout option)
10:33:54.689 [error] Postgrex.Protocol (#PID<0.3303.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.3309.0> timed out because it owned the connection for longer than 60000ms (set via the :ownership_timeout option)
11:33:54.693 [error] Postgrex.Protocol (#PID<0.3300.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.3309.0> timed out because it owned the connection for longer than 60000ms (set via the :ownership_timeout option)
12:33:54.696 [error] Postgrex.Protocol (#PID<0.3299.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.3309.0> timed out because it owned the connection for longer than 60000ms (set via the :ownership_timeout option)
13:33:54.699 [error] Postgrex.Protocol (#PID<0.3303.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.3309.0> timed out because it owned the connection for longer than 60000ms (set via the :ownership_timeout option)
14:33:54.701 [error] Postgrex.Protocol (#PID<0.3300.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.3309.0> timed out because it owned the connection for longer than 60000ms (set via the :ownership_timeout option)
defmodule DB.ReleaseTasks do
@start_apps [
:crypto,
:ssl,
:postgrex,
:ecto,
:ecto_sql
]
@repos Application.get_env(:db, :ecto_repos, [])
def migrate(_argv) do
start_services()
run_migrations()
stop_services()
end
def seed(_argv) do
start_services()
run_migrations()
run_seeds()
stop_services()
end
def priv_dir(app), do: "#{:code.priv_dir(app)}"
defp start_services do
IO.puts("Starting dependencies..")
# Start apps necessary for executing migrations
Enum.each(@start_apps, &Application.ensure_all_started/1)
# Start the Repo(s) for app
IO.puts("Starting repos..")
Enum.each(@repos, & &1.start_link(pool_size: 2))
end
defp stop_services do
IO.puts("Success!")
:init.stop()
end
defp run_migrations do
Enum.each(@repos, &run_migrations_for/1)
end
defp run_migrations_for(repo) do
app = Keyword.get(repo.config, :otp_app)
migrations_path = priv_path_for(repo, "migrations")
IO.puts("Running migrations for #{app} repo: #{repo} migrations_path: #{migrations_path}")
Ecto.Migrator.run(repo, migrations_path, :up, all: true)
end
defp run_seeds do
Enum.each(@repos, &run_seeds_for/1)
end
defp run_seeds_for(repo) do
# Run the seed script if it exists
seed_script = priv_path_for(repo, "seeds.exs")
if File.exists?(seed_script) do
IO.puts("Running seed script..")
Code.eval_file(seed_script)
end
end
defp priv_path_for(repo, filename) do
app = Keyword.get(repo.config, :otp_app)
repo_underscore =
repo
|> Module.split()
|> List.last()
|> Macro.underscore()
priv_dir = "#{:code.priv_dir(app)}"
Path.join([priv_dir, repo_underscore, filename])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment