Skip to content

Commit

Permalink
create configuration to use DigitalOcean Postgres on Fly.io #449
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Oct 20, 2024
1 parent 6c14068 commit 6883635
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ npm-debug.log

.env
hoppscotch/node_modules

# Postgres CA Cert
.postgres-cert.crt
ca-certificate.crt
15 changes: 14 additions & 1 deletion config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,20 @@ import Config
# which you should run after static files are built and
# before starting your production server.
config :app, AppWeb.Endpoint,
cache_static_manifest: "priv/static/cache_manifest.json"
cache_static_manifest: "priv/static/cache_manifest.json",
# Uncomment the next line to run "MIX_ENV=prod mix s" on localhost
check_origin: ["//localhost"]

# Do not print debug messages in production
config :logger, level: :debug



# # Configure DB for Digital Ocean via SSL
# config :app, App.Repo,
# ssl: [
# verify: :verify_peer,
# cacertfile: ".postgres-cert.crt"
# ],
# # hexdocs.pm/ecto_sql/Ecto.Adapters.Postgres.html#module-connection-options
# maintenance_database: "defaultdb"
32 changes: 30 additions & 2 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,41 @@ if config_env() == :prod do
For example: ecto://USER:PASS@HOST/DATABASE
"""

ssl = if database_url =~ "ondigitalocean.com" do
filepath = Path.join([File.cwd!, "ca-certificate.crt"])
cert_content_from_env =
System.get_env("POSTGRES_CERT") ||
raise """
environment variable POSTGRES_CERT is missing.
For example: -----BEGIN CERTIFICATE----- ...
"""
File.write(filepath, cert_content_from_env)

[
verify: :verify_peer,
cacertfile: filepath
]
else
IO.puts("Not DigitalOcean")
false
end

# Set Maintenance DB for when the app-db is not available
maintenance_db = if database_url =~ "ondigitalocean.com" do
"defaultdb"
else
"postgres"
end

maybe_ipv6 = if System.get_env("ECTO_IPV6"), do: [:inet6], else: []

config :app, App.Repo,
# ssl: true,
url: database_url,
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
socket_options: maybe_ipv6
socket_options: maybe_ipv6,
ssl: ssl,
maintenance_database: maintenance_db


# The secret key base is used to sign/encrypt cookies and other secrets.
# A default value is used in config/dev.exs and config/test.exs but you
Expand Down

0 comments on commit 6883635

Please sign in to comment.