1
0
Fork 0
arcology-elixir/arcology.org

2.6 KiB

Arcology Backend Application

In Elixir there is usually a frontend application which serves the Phoenix web site, and a backend service which it communicates with over Erlang messaging to do state management, business logic, and tasks like that, so that the frontend can stay as a "pure" MVC setup.

This document considers the backend Arcology application, for now a simple OTP application and an Ecto repository for our backend data objects.

Arcology

defmodule Arcology do
  @moduledoc """
  Arcology 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

Arcology.Application

This is the OTP application that starts everything else. The Elixir documentation has more information about how this module works.

defmodule Arcology.Application do
  @moduledoc false

  use Application

  <<otp-start>>

  # Tell Phoenix to update the endpoint configuration
  # whenever the application is updated.
  def config_change(changed, _new, removed) do
    ArcologyWeb.Endpoint.config_change(changed, removed)
    :ok
  end
end

When start/2 is called, the Application will start and manage the Repo and the Endpoint. If an application dies, that application will be restarted, that strategy can be changed though. Other applications which I want to manage will be added to the children list. The Repo is defined below, and the web endpoint is defined in another file.

def start(_type, _args) do
  children = [
    Arcology.Repo,
    ArcologyWeb.Endpoint,
    Arcology.FileNotifier,
    {Phoenix.PubSub, [name: Arcology.PubSub, adapter: Phoenix.PubSub.Redis]},
    {Task.Supervisor, name: Arcology.TaskSupervisor}
  ]

  # See https://hexdocs.pm/elixir/Supervisor.html
  # for other strategies and supported options
  opts = [strategy: :one_for_one, name: Arcology.Supervisor]
  Supervisor.start_link(children, opts)
end

Arcology.Repo

The Repo configuration is pretty simple, there's not a lot going on here.

defmodule Arcology.Repo do
  use Ecto.Repo,
    otp_app: :arcology,
    adapter: Sqlite.Ecto2
end