refactor(config): use secret files in dev mode

- Refactor `config/dev.exs` to use `config/dev.secret.exs` instead of
  fetching the SMTP information from the environment and using hardcoded
  database credentials. This is useful when different developers have
  different database credentials and they use different SMTP adapters
- Add a sample `dev.secret.exs` in `config/dev.secret.sample.exs`. Also
  gitignore the origin `dev.secret.exs` file
- Add instructions on how to setup the config file for development in
  `README.md`
- Add `Plug.Swoosh.MailboxPreview` plug on "/mailbox" route in
  development mode for easier development with e-mails

Signed-off-by: Akshit Garg <garg.akshit@gmail.com>
This commit is contained in:
Akshit Garg 2021-08-21 16:47:31 +05:30
parent 74ed02c4da
commit 12ec98d15e
No known key found for this signature in database
GPG Key ID: 31EE28F4B925E878
5 changed files with 35 additions and 36 deletions

3
.gitignore vendored
View File

@ -36,3 +36,6 @@ npm-debug.log
.idea .idea
.env .env
.envrc .envrc
# Secret config files
config/*.secret.exs

View File

@ -2,6 +2,8 @@
To start your Phoenix server: To start your Phoenix server:
* Copy `config/dev.secret.sample.exs` to `config/dev.secret.exs`
* Fill in the SMTP and database configuration in `config/dev.secret.exs`
* Install dependencies with `mix deps.get` * Install dependencies with `mix deps.get`
* Create and migrate your database with `mix ecto.setup` * Create and migrate your database with `mix ecto.setup`
* Install Node.js dependencies with `npm install` inside the `assets` directory * Install Node.js dependencies with `npm install` inside the `assets` directory

View File

@ -1,14 +1,5 @@
use Mix.Config use Mix.Config
# Configure your database
config :ketbin, Ketbin.Repo,
username: "postgres",
password: "postgres",
database: "ketbin_dev",
hostname: "localhost",
show_sensitive_data_on_connection_error: true,
pool_size: 10
# For development, we disable any cache and enable # For development, we disable any cache and enable
# debugging and code reloading. # debugging and code reloading.
# #
@ -30,32 +21,6 @@ config :ketbin, KetbinWeb.Endpoint,
] ]
] ]
smtp_relay =
System.get_env("SWOOSH_SMTP_RELAY") ||
raise """
environment variable SWOOSH_SMTP_RELAY is missing.
"""
username =
System.get_env("SWOOSH_USERNAME") ||
raise """
environment variable SWOOSH_USERNAME is missing.
"""
password =
System.get_env("SWOOSH_PASSWORD") ||
raise """
environment variable SWOOSH_PASSWORD is missing.
"""
# configure mailer
config :ketbin, Ketbin.Mailer,
adapter: Swoosh.Adapters.SMTP,
relay: smtp_relay,
username: username,
password: password,
tls: :always,
auth: :always,
port: 587
# ## SSL Support # ## SSL Support
# #
# In order to use HTTPS in development, a self-signed # In order to use HTTPS in development, a self-signed
@ -100,3 +65,6 @@ config :phoenix, :stacktrace_depth, 20
# Initialize plugs at runtime for faster development compilation # Initialize plugs at runtime for faster development compilation
config :phoenix, :plug_init_mode, :runtime config :phoenix, :plug_init_mode, :runtime
# Import the secrets config
import_config "dev.secret.exs"

View File

@ -0,0 +1,24 @@
import Mix.Config
# Configure your database
config :ketbin, Ketbin.Repo,
username: "postgres",
password: "postgres",
database: "ketbin_dev",
hostname: "localhost",
show_sensitive_data_on_connection_error: true,
pool_size: 10
smtp_relay = "<Your SMTP Relay Here>"
username = "<Your SMTP username here>"
password = "<Your SMTP password here>"
# configure mailer
config :ketbin, Ketbin.Mailer,
adapter: Swoosh.Adapters.SMTP,
relay: smtp_relay,
username: username,
password: password,
tls: :always,
auth: :always,
port: 587

View File

@ -59,7 +59,9 @@ defmodule KetbinWeb.Router do
scope "/" do scope "/" do
pipe_through :browser pipe_through :browser
live_dashboard "/dashboard", metrics: KetbinWeb.Telemetry live_dashboard "/dashboard", metrics: KetbinWeb.Telemetry
forward "/mailbox", Plug.Swoosh.MailboxPreview
end end
end end