1.9 KiB · text History b6f7c30
defmodule CartosnapApp.Application do
  # See https://elixir.hexdocs.pm/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  @impl true
  def start(_type, _args) do
    attach_sentry()
    topologies = Application.get_env(:libcluster, :topologies, [])

    children = [
      CartosnapAppWeb.Telemetry,
      CartosnapApp.Repo,
      # OIDC providers (discovery, JWKS); starts none while OIDC_ENABLED is off.
      NeiamAuth,
      {DNSCluster, query: Application.get_env(:cartosnap_app, :dns_cluster_query) || :ignore},
      cluster_child(topologies),
      {Phoenix.PubSub, name: CartosnapApp.PubSub},
      CartosnapAppWeb.Presence,
      {Oban, Application.fetch_env!(:cartosnap_app, Oban)},
      # Start to serve requests, typically the last entry
      CartosnapAppWeb.Endpoint
    ]

    # See https://elixir.hexdocs.pm/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: CartosnapApp.Supervisor]
    Supervisor.start_link(Enum.reject(children, &is_nil/1), opts)
  end

  # libcluster only runs where a topology is configured (prod, via
  # runtime.exs); in dev and test there is no Cluster.Supervisor at all.
  defp cluster_child([]), do: nil

  defp cluster_child(topologies),
    do: {Cluster.Supervisor, [topologies, [name: CartosnapApp.ClusterSupervisor]]}

  # Crashes and error logs to Vigil, when a SENTRY_DSN is configured
  # (config/runtime.exs); without one, nothing is attached.
  defp attach_sentry do
    if Sentry.Config.dsn() do
      :logger.add_handler(:sentry_handler, Sentry.LoggerHandler, %{
        config: %{metadata: [:request_id], capture_log_messages: false, level: :error}
      })
    end

    :ok
  end

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