lipu_kasi/lipu_kasi.org

69 lines
2.4 KiB
Org Mode

#+TITLE: LipuKasi backend application
In Elixir, generally there is a frontend application which serves the Phoenix web site, and a backend service which it communicates with over Erlang messaging to do data management, and tasks like that, so that the frontend can stay as a "pure"-ish MVC setup.
This document considers the backend =LipuKasi= application, for now a simple OTP application and an Ecto repository for our backend data objects.
* LipuKasi
#+begin_src elixir :tangle lib/lipu_kasi.ex :mkdirp yes
defmodule LipuKasi do
@moduledoc """
LipuKasi 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
** LipuKasi.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/lipu_kasi/application.ex :noweb tangle :mkdirp yes
defmodule LipuKasi.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
LipuKasiWeb.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 = [
LipuKasi.Repo,
LipuKasiWeb.Endpoint
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: LipuKasi.Supervisor]
Supervisor.start_link(children, opts)
end
#+end_src
** LipuKasi.Repo
The Repo configuration is pretty simple, there's not a lot going on here.
#+begin_src elixir :tangle lib/lipu_kasi/repo.ex :mkdirp yes
defmodule LipuKasi.Repo do
use Ecto.Repo,
otp_app: :lipu_kasi,
adapter: Ecto.Adapters.Postgres
end
#+end_src