floof/lib/floof/application.ex
Alain Zscheile c48367ff09 send public key with every message
this is more efficient and solves key distribution concerns
2022-11-26 23:41:56 +01:00

60 lines
2 KiB
Elixir

defmodule Floof.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
require Floof.Distributor
@impl true
def start(_type, _args) do
listen_port = Application.fetch_env!(:floof, :listen_port)
listen_opts = Application.fetch_env!(:floof, :listen_opts)
upstreams = Application.fetch_env!(:floof, :upstreams)
markers = Application.fetch_env!(:floof, :markers)
attrs = Application.fetch_env!(:floof, :attrs)
set_markers = Application.fetch_env!(:floof, :set_markers)
spool_dir = Application.fetch_env!(:floof, :spool_dir)
sessions_file = Application.fetch_env!(:floof, :sessions_file)
children =
[
# Starts a worker by calling: Floof.Worker.start_link(arg)
# {Floof.Worker, arg}
{Task.Supervisor, name: Floof.TaskSupervisor},
Floof.DistributorSeen,
{Floof.PacketSpool, spool_dir},
{Floof.Distributor,
Floof.Distributor.fconfig(
markers: markers,
attrs: attrs,
set_markers: set_markers
)},
{Floof.SessionManager, {:sessions, [file: to_charlist(sessions_file)]}}
] ++
if listen_port != nil do
[{Task, fn -> Floof.accept(listen_port, listen_opts) end}]
else
[]
end ++
for upstream <- upstreams do
{host, port, sesskey} =
case upstream do
{host, port, sesskey} -> {host, port, sesskey}
{host, port} -> {host, port, nil}
{host} -> {host, 2540, nil}
end
# this establishes connections to upstream hosts
Supervisor.child_spec({Task, fn -> Floof.connect(host, port, sesskey) end},
id: "upstream:#{inspect(host)}:#{port}"
)
end
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Floof.Supervisor]
Supervisor.start_link(children, opts)
end
end