This commit is contained in:
2026-03-02 08:40:30 -07:00
parent 2b013f390b
commit 9955a7f90c
39 changed files with 1388 additions and 1 deletions

9
backend/lib/backend.ex Normal file
View File

@@ -0,0 +1,9 @@
defmodule Backend do
@moduledoc """
Backend keeps the contexts that define your domain
and business logic.
Contexts are also responsible for managing your data, regardless
if it comes from the database, an external API or others.
"""
end

View File

@@ -0,0 +1,37 @@
defmodule Backend.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
@impl true
def start(_type, _args) do
children = [
BackendWeb.Telemetry,
{DNSCluster, query: Application.get_env(:backend, :dns_cluster_query) || :ignore},
{Phoenix.PubSub, name: Backend.PubSub},
# Start the Cluster manager for distributed Erlang
Backend.Cluster,
# Use GlobalSingleton supervisor for instant failover
{Backend.GlobalSingleton, Backend.GameState},
# Start a worker by calling: Backend.Worker.start_link(arg)
# {Backend.Worker, arg},
# Start to serve requests, typically the last entry
BackendWeb.Endpoint
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Backend.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
@impl true
def config_change(changed, _new, removed) do
BackendWeb.Endpoint.config_change(changed, removed)
:ok
end
end

View File

@@ -0,0 +1,78 @@
defmodule Backend.Cluster do
@moduledoc """
Manages Erlang clustering using native :net_kernel functionality.
Automatically connects to other nodes in the cluster.
"""
use GenServer
require Logger
def start_link(opts) do
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
end
@impl true
def init(_opts) do
# Connect to cluster nodes on startup
Process.send_after(self(), :connect_nodes, 1000)
# Periodically retry connection to handle network issues
schedule_connect()
{:ok, %{}}
end
@impl true
def handle_info(:connect_nodes, state) do
connect_to_cluster()
{:noreply, state}
end
@impl true
def handle_info(:periodic_connect, state) do
connect_to_cluster()
schedule_connect()
{:noreply, state}
end
defp connect_to_cluster do
# Get all cluster nodes and exclude ourselves
all_nodes =
System.get_env("CLUSTER_NODES", "")
|> String.split(",", trim: true)
|> Enum.map(&String.to_atom/1)
cluster_nodes = Enum.reject(all_nodes, &(&1 == node()))
failures =
Enum.filter(cluster_nodes, fn node_name ->
case Node.connect(node_name) do
true ->
# Logger.debug("Connected to node: #{node_name}")
false
false ->
Logger.warning("Failed to connect to node: #{node_name}")
true
:ignored ->
# Already connected, not a failure
false
end
end)
connected_nodes = Node.list()
# Expected count is other nodes (not including ourselves)
expected_count = length(cluster_nodes)
actual_count = length(connected_nodes)
if failures != [] or actual_count < expected_count do
Logger.warning(
"Cluster status: #{actual_count}/#{expected_count} nodes connected: #{inspect(connected_nodes)}"
)
end
end
defp schedule_connect do
# Retry connection every 10 seconds
Process.send_after(self(), :periodic_connect, 10_000)
end
end

View File

@@ -0,0 +1,118 @@
defmodule Backend.GameState do
@moduledoc """
GenServer to track all players and their positions in the game.
Uses :global registry for distributed singleton pattern - only one instance
runs across the entire cluster, with automatic failover.
"""
use GenServer
require Logger
@name {:global, __MODULE__}
@pubsub Backend.PubSub
@topic "game_state"
# Client API
def start_link(_opts) do
case GenServer.start_link(__MODULE__, %{}, name: @name) do
{:ok, pid} ->
{:ok, pid}
{:error, {:already_started, pid}} ->
# Another instance is already running globally
:ignore
end
end
def add_player(player_id) do
Logger.info("Player #{player_id} connected")
GenServer.call(@name, {:add_player, player_id})
end
def remove_player(player_id) do
Logger.info("Player #{player_id} removed")
GenServer.cast(@name, {:remove_player, player_id})
end
def move_player(player_id, directions) do
Logger.info("Player #{player_id} moved #{inspect(directions)}")
GenServer.cast(@name, {:move_player, player_id, directions})
end
def get_state do
GenServer.call(@name, :get_state)
end
# Server Callbacks
@impl true
def init(_) do
Logger.info("GameState starting on node: #{node()}")
{:ok, %{}}
end
@impl true
def handle_call({:add_player, player_id}, _from, state) do
# Start players at random position
x = :rand.uniform(800)
y = :rand.uniform(600)
new_state = Map.put(state, player_id, %{x: x, y: y})
broadcast_state(new_state)
{:reply, new_state, new_state}
end
@impl true
def handle_cast({:move_player, player_id, directions}, state) do
Logger.info("Processing player move")
player =
case Map.get(state, player_id) do
nil ->
Logger.warning(
"Move attempted for non-existent player: #{player_id}, creating at random position"
)
%{x: :rand.uniform(800), y: :rand.uniform(600)}
existing_player ->
existing_player
end
step = 10
# Apply all directions to calculate final position
new_position =
Enum.reduce(directions, player, fn direction, acc ->
case direction do
"w" -> %{acc | y: max(0, acc.y - step)}
"a" -> %{acc | x: max(0, acc.x - step)}
"s" -> %{acc | y: min(600, acc.y + step)}
"d" -> %{acc | x: min(800, acc.x + step)}
_ -> acc
end
end)
new_state = Map.put(state, player_id, new_position)
broadcast_state(new_state)
{:noreply, new_state}
end
@impl true
def handle_call(:get_state, _from, state) do
{:reply, state, state}
end
@impl true
def handle_cast({:remove_player, player_id}, state) do
new_state = Map.delete(state, player_id)
broadcast_state(new_state)
{:noreply, new_state}
end
# Private Functions
defp broadcast_state(state) do
Phoenix.PubSub.broadcast(@pubsub, @topic, {:game_state_updated, state})
end
end

View File

@@ -0,0 +1,54 @@
defmodule Backend.GlobalSingleton do
@moduledoc """
Supervisor that ensures a global singleton process runs across the cluster.
If the node running it crashes, another node will take over.
"""
use Supervisor
require Logger
def start_link(module) do
Supervisor.start_link(__MODULE__, module, name: :"#{module}.GlobalSingleton")
end
@impl true
def init(module) do
children = [
%{
id: :monitor_task,
start: {Task, :start_link, [fn -> monitor_loop(module) end]},
restart: :permanent
}
]
Supervisor.init(children, strategy: :one_for_one)
end
defp monitor_loop(module) do
case :global.whereis_name(module) do
:undefined ->
Logger.info("#{module} not running, attempting to start on #{node()}")
case module.start_link([]) do
{:ok, _pid} ->
Logger.info("#{module} started on #{node()}")
{:error, {:already_started, _pid}} ->
Logger.debug("#{module} already started by another node")
_ ->
:ok
end
Process.sleep(100)
monitor_loop(module)
pid when is_pid(pid) ->
ref = Process.monitor(pid)
receive do
{:DOWN, ^ref, :process, ^pid, _reason} ->
Logger.warning("#{module} went down, attempting takeover")
monitor_loop(module)
end
end
end
end

View File

@@ -0,0 +1,3 @@
defmodule Backend.Mailer do
use Swoosh.Mailer, otp_app: :backend
end

View File

@@ -0,0 +1,63 @@
defmodule BackendWeb do
@moduledoc """
The entrypoint for defining your web interface, such
as controllers, components, channels, and so on.
This can be used in your application as:
use BackendWeb, :controller
use BackendWeb, :html
The definitions below will be executed for every controller,
component, etc, so keep them short and clean, focused
on imports, uses and aliases.
Do NOT define functions inside the quoted expressions
below. Instead, define additional modules and import
those modules here.
"""
def static_paths, do: ~w(assets fonts images favicon.ico robots.txt)
def router do
quote do
use Phoenix.Router, helpers: false
# Import common connection and controller functions to use in pipelines
import Plug.Conn
import Phoenix.Controller
end
end
def channel do
quote do
use Phoenix.Channel
end
end
def controller do
quote do
use Phoenix.Controller, formats: [:html, :json]
import Plug.Conn
unquote(verified_routes())
end
end
def verified_routes do
quote do
use Phoenix.VerifiedRoutes,
endpoint: BackendWeb.Endpoint,
router: BackendWeb.Router,
statics: BackendWeb.static_paths()
end
end
@doc """
When used, dispatch to the appropriate controller/live_view/etc.
"""
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
end

View File

@@ -0,0 +1,60 @@
defmodule BackendWeb.GameChannel do
@moduledoc """
Channel for handling game events and player movements.
"""
use BackendWeb, :channel
require Logger
@impl true
def join("game:lobby", _payload, socket) do
Logger.info("WebSocket connected to #{node()}, waiting for name")
Phoenix.PubSub.subscribe(Backend.PubSub, "game_state")
current_state = Backend.GameState.get_state()
{:ok, %{players: current_state}, socket}
end
@impl true
def handle_info({:game_state_updated, state}, socket) do
push(socket, "game_state", %{players: state})
{:noreply, socket}
end
@impl true
def handle_in("join_game", %{"name" => name}, socket) do
Logger.info("Player '#{name}' joining game on #{node()}")
socket = assign(socket, :player_name, name)
Backend.GameState.add_player(name)
{:reply, :ok, socket}
end
@impl true
def handle_in("move", %{"directions" => directions}, socket) do
case socket.assigns[:player_name] do
nil ->
Logger.warning("Move attempted without joining game")
{:noreply, socket}
player_name ->
Logger.debug("Player '#{player_name}' moved #{inspect(directions)} on #{node()}")
Backend.GameState.move_player(player_name, directions)
{:noreply, socket}
end
end
@impl true
def terminate(_reason, socket) do
case socket.assigns[:player_name] do
nil ->
Logger.info("WebSocket disconnected from #{node()}")
player_name ->
Logger.info("Player '#{player_name}' disconnected from #{node()}")
Backend.GameState.remove_player(player_name)
end
:ok
end
end

View File

@@ -0,0 +1,36 @@
defmodule BackendWeb.UserSocket do
use Phoenix.Socket
# A Socket handler
#
# It's possible to control the websocket connection and
# assign values that can be accessed by your channel topics.
## Channels
channel("game:*", BackendWeb.GameChannel)
# Socket params are passed from the client and can
# be used to verify and authenticate a user. After
# verification, you can put default assigns into
# the socket that will be set for all channels, ie
#
# {:ok, assign(socket, :user_id, verified_user_id)}
#
# To deny connection, return `:error` or `{:error, term}`.
@impl true
def connect(_params, socket, _connect_info) do
{:ok, socket}
end
# Socket id's are topics that allow you to identify all sockets for a given user:
#
# def id(socket), do: "user_socket:#{socket.assigns.user_id}"
#
# Would allow you to broadcast a "disconnect" event and terminate
# all active sockets and channels for a given user:
#
#
# Returning `nil` makes this socket anonymous.
@impl true
def id(_socket), do: nil
end

View File

@@ -0,0 +1,21 @@
defmodule BackendWeb.ErrorJSON do
@moduledoc """
This module is invoked by your endpoint in case of errors on JSON requests.
See config/config.exs.
"""
# If you want to customize a particular status code,
# you may add your own clauses, such as:
#
# def render("500.json", _assigns) do
# %{errors: %{detail: "Internal Server Error"}}
# end
# By default, Phoenix returns the status message from
# the template name. For example, "404.json" becomes
# "Not Found".
def render(template, _assigns) do
%{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}}
end
end

View File

@@ -0,0 +1,15 @@
defmodule BackendWeb.HealthController do
use BackendWeb, :controller
# Disable logging for health checks
plug(:disable_logging)
def check(conn, _params) do
json(conn, %{status: "ok", node: node()})
end
defp disable_logging(conn, _opts) do
Logger.metadata(plug_log_level: :none)
conn
end
end

View File

@@ -0,0 +1,61 @@
defmodule BackendWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :backend
# The session will be stored in the cookie and signed,
# this means its contents can be read but not tampered with.
# Set :encryption_salt if you would also like to encrypt it.
@session_options [
store: :cookie,
key: "_backend_key",
signing_salt: "AnIFBp4s",
same_site: "Lax"
]
socket("/live", Phoenix.LiveView.Socket,
websocket: [connect_info: [session: @session_options]],
longpoll: [connect_info: [session: @session_options]]
)
socket("/socket", BackendWeb.UserSocket,
websocket: true,
longpoll: false
)
# Serve at "/" the static files from "priv/static" directory.
#
# When code reloading is disabled (e.g., in production),
# the `gzip` option is enabled to serve compressed
# static files generated by running `phx.digest`.
plug(Plug.Static,
at: "/",
from: :backend,
gzip: not code_reloading?,
only: BackendWeb.static_paths(),
raise_on_missing_only: code_reloading?
)
# Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint.
if code_reloading? do
plug(Phoenix.CodeReloader)
end
plug(Phoenix.LiveDashboard.RequestLogger,
param_key: "request_logger",
cookie_key: "request_logger"
)
plug(Plug.RequestId)
plug(BackendWeb.Plugs.Telemetry, event_prefix: [:phoenix, :endpoint])
plug(Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Phoenix.json_library()
)
plug(Plug.MethodOverride)
plug(Plug.Head)
plug(Plug.Session, @session_options)
plug(BackendWeb.Router)
end

View File

@@ -0,0 +1,17 @@
defmodule BackendWeb.HealthCheckFilter do
@moduledoc """
Custom Phoenix logger that filters out health check requests.
"""
require Logger
def phoenix_filter(level, msg, ts, meta) do
# Filter out health check logs
case meta do
%{request_path: "/api/health"} ->
:ignore
_ ->
Phoenix.Logger.log(level, msg, ts, meta)
end
end
end

View File

@@ -0,0 +1,14 @@
defmodule BackendWeb.Plugs.Telemetry do
@behaviour Plug
@impl true
def init(opts), do: Plug.Telemetry.init(opts)
@impl true
def call(%{path_info: ["api", "health"]} = conn, {start_event, stop_event, opts}) do
# Suppress logs for health check endpoint
Plug.Telemetry.call(conn, {start_event, stop_event, Keyword.put(opts, :log, false)})
end
def call(conn, args), do: Plug.Telemetry.call(conn, args)
end

View File

@@ -0,0 +1,30 @@
defmodule BackendWeb.Router do
use BackendWeb, :router
pipeline :api do
plug(:accepts, ["json"])
end
scope "/api", BackendWeb do
pipe_through(:api)
get("/health", HealthController, :check)
end
# Enable LiveDashboard and Swoosh mailbox preview in development
if Application.compile_env(:backend, :dev_routes) do
# If you want to use the LiveDashboard in production, you should put
# it behind authentication and allow only admins to access it.
# If your application does not have an admins-only section yet,
# you can use Plug.BasicAuth to set up some basic authentication
# as long as you are also using SSL (which you should anyway).
import Phoenix.LiveDashboard.Router
scope "/dev" do
pipe_through([:fetch_session, :protect_from_forgery])
live_dashboard("/dashboard", metrics: BackendWeb.Telemetry)
forward("/mailbox", Plug.Swoosh.MailboxPreview)
end
end
end

View File

@@ -0,0 +1,70 @@
defmodule BackendWeb.Telemetry do
use Supervisor
import Telemetry.Metrics
def start_link(arg) do
Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
end
@impl true
def init(_arg) do
children = [
# Telemetry poller will execute the given period measurements
# every 10_000ms. Learn more here: https://hexdocs.pm/telemetry_metrics
{:telemetry_poller, measurements: periodic_measurements(), period: 10_000}
# Add reporters as children of your supervision tree.
# {Telemetry.Metrics.ConsoleReporter, metrics: metrics()}
]
Supervisor.init(children, strategy: :one_for_one)
end
def metrics do
[
# Phoenix Metrics
summary("phoenix.endpoint.start.system_time",
unit: {:native, :millisecond}
),
summary("phoenix.endpoint.stop.duration",
unit: {:native, :millisecond}
),
summary("phoenix.router_dispatch.start.system_time",
tags: [:route],
unit: {:native, :millisecond}
),
summary("phoenix.router_dispatch.exception.duration",
tags: [:route],
unit: {:native, :millisecond}
),
summary("phoenix.router_dispatch.stop.duration",
tags: [:route],
unit: {:native, :millisecond}
),
summary("phoenix.socket_connected.duration",
unit: {:native, :millisecond}
),
sum("phoenix.socket_drain.count"),
summary("phoenix.channel_joined.duration",
unit: {:native, :millisecond}
),
summary("phoenix.channel_handled_in.duration",
tags: [:event],
unit: {:native, :millisecond}
),
# VM Metrics
summary("vm.memory.total", unit: {:byte, :kilobyte}),
summary("vm.total_run_queue_lengths.total"),
summary("vm.total_run_queue_lengths.cpu"),
summary("vm.total_run_queue_lengths.io")
]
end
defp periodic_measurements do
[
# A module, function and arguments to be invoked periodically.
# This function must call :telemetry.execute/3 and a metric must be added above.
# {BackendWeb, :count_users, []}
]
end
end