Prepare to join servers

This commit is contained in:
Maciej 2026-02-28 16:19:35 +02:00
parent 3afaf346c7
commit 984ac15084
Signed by: maciej
GPG key ID: 28243AF437E32F99
12 changed files with 137 additions and 17 deletions

View file

@ -10,6 +10,8 @@ defmodule BirdyChat.Application do
children = [
BirdyChatWeb.Telemetry,
BirdyChat.Identity,
BirdyChat.PeerSupervisor,
{Registry, keys: :unique, name: BirdyChat.PeerRegistry},
{DNSCluster, query: Application.get_env(:birdy_chat, :dns_cluster_query) || :ignore},
{Phoenix.PubSub, name: BirdyChat.PubSub},
# Start a worker by calling: BirdyChat.Worker.start_link(arg)

View file

@ -49,7 +49,12 @@ defmodule BirdyChat.Identity do
peers = System.get_env("BIRDY_CHAT_PEERS")
case {identity, peers} do
{nil, nil} -> %__MODULE__{identity: "test", peers: [], mode: :singleton}
{nil, nil} ->
%__MODULE__{
identity: "test1",
peers: %{"test2" => "http://localhost:4001"},
mode: :test
}
end
end
end

View file

@ -19,4 +19,21 @@ defmodule BirdyChat.Message do
{:error, changeset}
end
end
def find_peer(%{to: to}) do
identity = BirdyChat.Identity.identity()
if String.starts_with?(to, identity) do
{:ok, :local}
else
result =
BirdyChat.Identity.peers()
|> Enum.find(fn {name, _url} -> String.starts_with?(to, name) end)
case result do
{name, _url} -> {:ok, name}
nil -> {:error, :not_found}
end
end
end
end

View file

@ -0,0 +1,9 @@
defmodule BirdyChat.PeerClient do
use Slipstream, restart: :temporary
def start_link(args) do
name = args[:peer_name]
name = {:via, BirdyChat.PeerRegistry, {:peers, name}}
Slipstream.start_link(__MODULE__, args, name: name)
end
end

View file

@ -0,0 +1,17 @@
defmodule BirdyChat.PeerSupervisor do
use DynamicSupervisor
def start_link(init_arg) do
DynamicSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
end
def start_child(peer_name, peer_address) do
spec = {BirdyChat.PeerClient, peer_name: peer_name, peer_address: peer_address}
DynamicSupervisor.start_child(__MODULE__, spec)
end
@impl true
def init(init_arg) do
DynamicSupervisor.init(strategy: :one_for_one, extra_arguments: [init_arg])
end
end

View file

@ -4,11 +4,30 @@ defmodule BirdyChatWeb.Api.Messages.Controller do
def create(conn, params) do
case BirdyChat.Message.validate(params) do
{:ok, changeset} ->
case BirdyChat.MessageWriter.write(changeset.changes) do
:ok ->
case BirdyChat.Message.find_peer(changeset.changes) do
{:ok, :local} ->
case BirdyChat.MessageWriter.write(changeset.changes) do
:ok ->
conn
|> put_status(:created)
|> render(:create, message: changeset.changes)
end
{:ok, peer_name} ->
BirdyChatWeb.ServerChannel.broadcast!(peer_name, changeset.changes)
conn
|> put_status(:created)
|> render(:create, message: changeset.changes)
{:error, :not_found} ->
changeset =
changeset
|> Ecto.Changeset.add_error(:to, "Unknown 'to' server")
conn
|> put_status(:unprocessable_entity)
|> render(:error, changeset: changeset)
end
{:error, changeset} ->

View file

@ -24,6 +24,13 @@ defmodule BirdyChatWeb.ServerChannel do
end
end
# This is how we send messages
def broadcast!(peer, message) do
channel = "server:#{peer}"
encoded_message = :erlang.term_to_binary(message)
BirdyChatWeb.Endpoint.broadcast!(channel, "new_message", {:binary, encoded_message})
end
# Simple token-based authentication. Servers should use the same Phoenix secret key so they will
# have the basis for generating tokens.
defp authorised?(%{"token" => token}, server_id) do