codomari · api · manifest

Codomari #5 (manifest)

In this post we are going to implement endpoint which will respond with json like:

{
  "project": "codomari",
  "type": "service",
  "app": "codomari_backend",
  "version": "0.0.3"
}

to achieve it we have endpoint /api/manifest defined in router.ex:

  scope "/api", CodomariBackend do
    pipe_through :api

    get "/", ManifestHandler, :handle
    get "/manifest", ManifestHandler, :handle
  end

and contents of lib/codomari_backend/handlers/api/manifest_handler.ex:

defmodule CodomariBackend.Handlers.Api.ManifestHandler do
  @moduledoc """
  Handler serves information about the service.
  """

  use CodomariBackend, :controller

  @doc """
  Returns information about the service to connection as json.
  """
  @spec handle(Plug.Conn.t(), map) :: Plug.Conn.t()
  def handle(conn, _params) do
    CodomariBackend.manifest()
    |> Jason.OrderedObject.new()
    |> then(&json(conn, &1))
  end
end

as seen from line:

manifest = CodomariBackend.manifest()

we need to define that method in lib/codomari_backend.ex:

defmodule CodomariBackend do
  ...

  def manifest do
    [
      project: :codomari,
      type: :service,
      app: :codomari_backend,
      version: "0.0.3"
    ]
  end

  ...
end

I’ve moved manifest method to CodomariBackend module to have keep related information within module instead of mix.exs which defines dependencies and scripts.

But now need to import and add it to mix.exs:

defmodule CodomariBackend.MixProject do
  ...

  def project do
    Code.require_file("lib/codomari_backend.ex")

    manifest = CodomariBackend.manifest()

    [
      app: manifest[:app],
      version: manifest[:version],
      elixir: "~> 1.14",
      elixirc_paths: elixirc_paths(Mix.env()),
      start_permanent: Mix.env() == :prod,
      aliases: aliases(),
      deps: deps(),
      releases: releases()
    ]
  end

  ...
end

and fixed line in sync script with this:

VERSION=$(grep -E 'version:' lib/codomari_backend.ex | sed -E "s/.*version: \"([^\"]+)\".*/\1/")

let’s check the results!

┌[num8er☮g8way1.local]-(~/My/Codomari/codomari_backend)-[git://main ✔]-
└> curl https://codomari.com/api | jq
{
  "project": "codomari",
  "type": "service",
  "app": "codomari_backend",
  "version": "0.0.3"
}

Don’t forget to check repo: https://github.com/num8er/codomari-backend


Talk Soon!