76 lines
2.6 KiB
Org Mode
76 lines
2.6 KiB
Org Mode
#+TITLE: Arcology Backend Application
|
|
#+ROAM_ALIAS: Arcology.Application Arcology.Repo
|
|
#+ARCOLOGY_KEY: arcology/application
|
|
#+ROAM_TAGS: Arcology
|
|
#+AUTO_TANGLE: t
|
|
|
|
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
|
|
|
|
#+begin_src elixir :tangle lib/arcology.ex :mkdirp yes
|
|
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
|
|
#+end_src
|
|
|
|
* Arcology.Application
|
|
|
|
This is the OTP application that starts everything else. The [[https://hexdocs.pm/elixir/Application.html][Elixir documentation]] has more information about how this module works.
|
|
|
|
#+begin_src elixir :tangle lib/arcology/application.ex :noweb tangle :mkdirp yes
|
|
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
|
|
#+end_src
|
|
|
|
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 [[https://hexdocs.pm/elixir/Supervisor.html#module-strategies][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.
|
|
|
|
#+begin_src elixir :noweb-ref otp-start
|
|
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
|
|
#+end_src
|
|
|
|
* Arcology.Repo
|
|
|
|
The Repo configuration is pretty simple, there's not a lot going on here.
|
|
|
|
#+begin_src elixir :tangle lib/arcology/repo.ex :mkdirp yes
|
|
defmodule Arcology.Repo do
|
|
use Ecto.Repo,
|
|
otp_app: :arcology,
|
|
adapter: Sqlite.Ecto2
|
|
end
|
|
#+end_src
|