Skip to content

Commit

Permalink
Use dynamic app info plug
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrián Quintás committed Mar 11, 2018
1 parent 2231c51 commit a2a2378
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 88 deletions.
39 changes: 26 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
[![Coverage Status](https://coveralls.io/repos/github/heyorbit/metadata_plugs/badge.svg?branch=master)](https://coveralls.io/github/heyorbit/metadata_plugs?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/qgadrian/metadata_plugs/badge.svg?branch=master)](https://coveralls.io/github/qgadrian/metadata_plugs?branch=master)
[![Hex version](https://img.shields.io/hexpm/v/sippet.svg "Hex version")](https://hex.pm/packages/metadata_plugs)
[![Hex Docs](https://img.shields.io/badge/hex-docs-9768d1.svg)](https://hexdocs.pm/metadata_plugs)
[![Build Status](https://travis-ci.org/heyorbit/metadata_plugs.svg?branch=master)](https://travis-ci.org/heyorbit/metadata_plugs)
[![Deps Status](https://beta.hexfaktor.org/badge/all/github/heyorbit/metadata_plugs.svg)](https://beta.hexfaktor.org/github/heyorbit/metadata_plugs)
[![Build Status](https://travis-ci.org/qgadrian/metadata_plugs.svg?branch=master)](https://travis-ci.org/qgadrian/metadata_plugs)
[![Deps Status](https://beta.hexfaktor.org/badge/all/github/qgadrian/metadata_plugs.svg)](https://beta.hexfaktor.org/github/qgadrian/metadata_plugs)

# MetadataPlugs

Collection of plugs to provide different metadata information.

Plugs included:
[Plugs included](#plugs):

* Health
* App version
* [Health](#health)
* [Info](#info)

## Installation

Add to dependencies

```elixir
def deps do
[{:metadata_plugs, "~> 0.1.0"}]
[{:metadata_plugs, "~> 0.2.0"}]
end
```

Expand All @@ -29,19 +29,32 @@ Install dependencies
mix deps.get
```

## Usage
## Plugs

Add the desired plugs to the endpoint file
### Health

Just add the plug to the endpoint file

```elixir
plug(MetadataPlugs.Health)
plug(MetadataPlugs.Info)
```

You can configure the path for the endpoints, for example
You can configure the path for the endpoint

```elixir
plug(MetadataPlugs.Health, health_path: "/healthz")
plug(MetadataPlugs.Health, path: "/healthz")
```

Check [documentation](https://hexdocs.pm/metadata_plugs) for more options
### Info

Add the plug to the endpoint file with the desired environment variables to get the info from.

```elixir
plug(MetadataPlugs.Info, env_vars: ["APP_VERSION", "ENVIRONMENT"])
```

You can configure the path for the endpoint

```elixir
plug(MetadataPlugs.Info, path: "/infoz")
```
55 changes: 55 additions & 0 deletions lib/info_plug.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
defmodule MetadataPlugs.Info do
@behaviour Plug
import Plug.Conn

@path "/info"

@typedoc """
- `:env_vars` -- String list of environment variables names to get the value from (default: []).
- `:path` -- (Optional) Info endpoint path (default: `/info`).
"""

@type opts :: [
path: String.t(),
env_vars: list(String.t())
]

@spec init(opts :: opts) :: map
def init(opts) do
default_opts = [
path: @path,
env_vars: []
]

Keyword.merge(default_opts, opts)
end

@doc """
Resolves a health request
"""
@spec call(Plug.Conn.t(), map) :: Plug.Conn.t()
def call(conn, opts) do
if conn.request_path == opts[:path] and conn.method == "GET" do
send_info(conn, opts[:env_vars])
else
conn
end
end

@spec send_info(Plug.Conn.t(), list(String.t())) :: Plug.Conn.t()
defp send_info(conn, env_vars) do
conn
|> put_resp_content_type("application/json")
|> send_resp(200, info_response(env_vars))
end

@spec info_response(list(String.t())) :: Map.t()
defp info_response(env_vars) do
env_vars
|> Enum.reduce(%{}, fn env_var, acc ->
env_var_value = System.get_env(env_var) || ""
Map.put(acc, env_var, env_var_value)
end)
|> Poison.encode!()
end
end
50 changes: 0 additions & 50 deletions lib/version_plug.ex

This file was deleted.

2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule MetadataPlug.MixProject do
def project do
[
app: :metadata_plugs,
version: "0.1.0",
version: "0.2.0",
elixir: "~> 1.6",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down
8 changes: 8 additions & 0 deletions test/plugs/health_plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ defmodule MetadataPlugs.HealthTest do
assert conn.status == 200
assert conn.resp_body == "{\"status\":\"up\"}"
end

test "when the conn does not match with the plug then the conn is returned" do
another_conn = conn(:get, "/whatever")

conn = MetadataPlugs.Health.call(another_conn, @opts)

assert(conn == another_conn)
end
end
37 changes: 37 additions & 0 deletions test/plugs/info_plug_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
defmodule MetadataPlugs.InfoTest do
use ExUnit.Case
use Plug.Test

doctest MetadataPlugs.Info

@path "/info"
@env_vars ["APP_VERSION", "TEST_VAR"]

@opts MetadataPlugs.Info.init(info_path: @path, env_vars: @env_vars)

test "Given a list of env variables when call the info endpoint then value of the variables is returned" do
Enum.each(@env_vars, &System.put_env(&1, "#{&1}_value"))

expected_env_vars_response =
@env_vars
|> Enum.reduce(%{}, fn env_var, acc -> Map.put(acc, env_var, "#{env_var}_value") end)
|> Poison.encode!()

conn =
:get
|> conn(@path)
|> MetadataPlugs.Info.call(@opts)

assert conn.state == :sent
assert conn.status == 200
assert conn.resp_body == expected_env_vars_response
end

test "when the conn does not match with the plug then the conn is returned" do
another_conn = conn(:get, "/whatever")

conn = MetadataPlugs.Info.call(another_conn, @opts)

assert(conn == another_conn)
end
end
24 changes: 0 additions & 24 deletions test/plugs/version_plug_test.exs

This file was deleted.

0 comments on commit a2a2378

Please sign in to comment.