summaryrefslogtreecommitdiff
path: root/lib/powerdnsex/server_setup.ex
blob: 3b5cb14d1e94e47a105b86dce45ec56e15b3a40e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
defmodule PowerDNSex.ServerSetup do
  defmacro __using__(opts \\ []) do
    quote bind_quoted: [opts: opts] do
      use Application

      @name opts[:process_name] || :PowerDNSex
      @config opts[:config]

      @spec start(term, term) :: GenServer.on_start
      def start(_, _), do: start

      @spec start() :: GenServer.on_start
      @doc false
      def start do
        import Supervisor.Spec

        children = [worker(Server, [@name, @config])]

        options = [strategy: :one_for_one, name: :"#{@name}.Supervisor"]

        case Supervisor.start_link(children, options) do
          {:ok, pid} -> {:ok, pid}
          {:error, {:already_started, pid}} -> {:ok, pid}
          other -> other
        end
      end
    end
  end
end