lipu_kasi/lipu_kasi.org

2.4 KiB

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

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

LipuKasi.Application

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

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

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 = [
    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

LipuKasi.Repo

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

defmodule LipuKasi.Repo do
  use Ecto.Repo,
    otp_app: :lipu_kasi,
    adapter: Ecto.Adapters.Postgres
end