honungsburk

honungsburk

Minimal setup for Plug.Swoosh.MailboxPreview?

Hi!

I’m building a small internal service to handle emails (it is not a webserver) and for this I have choosen Swoosh. Now I am trying to setup the mailbox preview and I want to use as few dependencies and as minimal setup as possible. Idealy, I would not even need to include it in the production environment but only in dev. What is hte minimal setup to make the preview work? Do I need to use pheonix as a dependency or is plug enough?

This is what I am currently trying:

config:

config :muninn, Phoenix.Endpoint,
  url: [host: "localhost"],
  render_errors: [formats: [html: Phoenix.Template.HTML]],
  pubsub_server: Muninn.PubSub,
  adapter: Bandit.PhoenixAdapter,
  live_view: [signing_salt: "some_salt"]

application:

defmodule Muninn.Application do
  @moduledoc """
  Application module for Muninn.
  """

  use Application
  require Logger
  
  @impl true
  def start(_type, _args) do
    # Oban.Telemetry.attach_default_logger(
    #   level: Application.get_env(:logger, :level),
    #   encode: true
    # )

    children = [
      Muninn.Repo,
      {Oban, Application.fetch_env!(:muninn, Oban)}
    ]

    children =
      if Application.get_env(:muninn, :email_preview) do
        # children ++ [{Phoenix.Endpoint, []}]
        children ++
          [
            # {Plug.Cowboy, scheme: :http, plug: Muninn.Router, options: [port: 4000]}
            {Phoenix.PubSub, name: Muninn.PubSub},
            # Start to serve requests, typically the last entry
            Muninn.Endpoint
          ]
      end

    Supervisor.start_link(children, strategy: :one_for_one)
  end

  # Tell Phoenix to update the endpoint configuration
  # whenever the application is updated.
  @impl true
  def config_change(changed, _new, removed) do
    Muninn.Endpoint.config_change(changed, removed)
    :ok
  end
end

Endpoint:

defmodule Muninn.Endpoint do
  use Phoenix.Endpoint, otp_app: :runar

  # The session will be stored in the cookie and signed,
  # this means its contents can be read but not tampered with.
  # Set :encryption_salt if you would also like to encrypt it.
  @session_options [
    store: :cookie,
    key: "_muninn_key",
    signing_salt: "C6nzbDmG",
    same_site: "Lax"
  ]

  socket("/live", Phoenix.LiveView.Socket,
    websocket: [connect_info: [:peer_data, session: @session_options]],
    longpoll: [connect_info: [:peer_data, session: @session_options]]
  )

  # Serve at "/" the static files from "priv/static" directory.
  #
  # You should set gzip to true if you are running phx.digest
  # when deploying your static files in production.
  plug(Plug.Static,
    at: "/",
    from: :muninn,
    gzip: false,
    only: ~w(assets fonts images favicon.ico robots.txt manifest.json)
  )

  # Code reloading can be explicitly enabled under the
  # :code_reloader configuration of your endpoint.
  if code_reloading? do
    socket("/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket)
    plug(Phoenix.LiveReloader)
    plug(Phoenix.CodeReloader)
    # plug(Phoenix.Ecto.CheckRepoStatus, otp_app: :muninn)
  end

  plug(Plug.RequestId)
  # plug(Plug.Telemetry, event_prefix: [:phoenix, :endpoint])

  plug(Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json],
    pass: ["*/*"],
    json_decoder: Phoenix.json_library()
  )

  plug(Plug.MethodOverride)
  plug(Plug.Head)
  plug(Plug.Session, @session_options)
  plug(Muninn.Router)
end

router file:

defmodule Muninn.Router do
  use Phoenix.Router
  import Plug.Conn
  import Phoenix.Controller
  import Phoenix.LiveReloader

  pipeline :browser do
    plug(:accepts, ["html"])
    plug(:fetch_session)

    if Mix.env() == :dev do
      plug(:fetch_live_reload)
    end
  end

  scope "/dev" do
    pipe_through(:browser)

    forward("/mailbox", Plug.Swoosh.MailboxPreview,
      csp_nonce_assign_key: %{script: :script_csp_nonce, style: :style_csp_nonce}
    )
  end
end

But I’m getting this error:

error: undefined function fetch_live_reload/2 (expected Muninn.Router to define such a function or for it to be imported, but none are available)
└─ deps/phoenix/lib/phoenix/router.ex: Muninn.Router.browser/2
```

First Post!

honungsburk

honungsburk

Got it working by following the instructions in the README: GitHub - swoosh/swoosh: Compose, deliver and test your emails easily in Elixir · GitHub

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement