defmodule Dough do
  @moduledoc false

  use Application

  import Supervisor.Spec
  import Cachex.Spec

  require Logger

  def start(_type, _args) do
    children = [
      worker(Cachex, [
        :dough,
        [
          expiration:
            expiration(
              default: :timer.seconds(6000),
              interval: :timer.seconds(300),
              lazy: true
            )
        ]
      ]),
      {Plug.Adapters.Cowboy2,
       scheme: :https,
       plug: Dough.Router,
       options: [
         port: 8331,
         otp_app: :dough,
         keyfile: "priv/ssl/localhost.key",
         certfile: "priv/ssl/localhost.crt"
       ]}
    ]

    Logger.info("
██▄   ████▄   ▄     ▄▀   ▄  █
█  █  █   █    █  ▄▀    █   █
█   █ █   █ █   █ █ ▀▄  ██▀▀█
█  █  ▀████ █   █ █   █ █   █
███▀        █▄ ▄█  ███     █
             ▀▀▀          ▀
Starting Dough Server #{Application.get_env(:dough, :current_version)} ...
DNS Proxying to #{Application.get_env(:dough, :dns_server)}
")

    opts = [strategy: :one_for_one, name: Dough.Supervisor]
    Supervisor.start_link(children, opts)
  end
end