Skip to content

Commit

Permalink
Use __ prefix for internal endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
hauleth committed Oct 11, 2018
1 parent a7c14cc commit 29db294
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/imager/config/store_transform.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ defmodule Imager.Config.StoreTransform do

require Logger

@reserved ~w[health]
@types ~w[S3 Local Blackhole]

def transform(:stores, entries) do
Expand All @@ -16,7 +15,8 @@ defmodule Imager.Config.StoreTransform do
{:ok, cache} <- get_cache(values) do
path = to_string(path)

if path in @reserved, do: raise("'#{path}' is reserved name")
if String.starts_with?(path, "_"),
do: raise("'#{path}' cannot start with underscore")

if String.contains?(path, "/"),
do: raise("'#{path}' cannot contain '/'")
Expand Down
5 changes: 1 addition & 4 deletions lib/imager_web/plug/metrics_exporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ defmodule ImagerWeb.Plug.MetricsExporter do
opts =
opts
|> Keyword.merge(Application.get_env(:imager, :prometheus, []))
|> Keyword.put_new(:endpoint, "/metrics")
|> Keyword.update(:format, :prometheus_text_format, &parse_format/1)

endpoint = Keyword.fetch!(opts, :endpoint)

case conn.request_path do
^endpoint -> send_stats(conn, opts)
"/__metrics" -> send_stats(conn, opts)
_ -> conn
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/imager_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule ImagerWeb.Router do
scope "/", ImagerWeb.Controllers do
pipe_through(:api)

get("/health", Health, :get)
get("/__health", Health, :get)

get("/:store/*path", Image, :get)
end
Expand Down
13 changes: 10 additions & 3 deletions test/imager/config/store_transform_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule Imager.Config.StoreTransformTest do

property "returns map with stringified atoms" do
check all path <- atom(:alphanumeric),
not String.starts_with?("#{path}", "_"),
config <- store_config() do
assert Map.has_key?(
Subject.transform(:stores, %{path => config}),
Expand Down Expand Up @@ -73,9 +74,15 @@ defmodule Imager.Config.StoreTransformTest do
end
end

test "raises when uses reserved name" do
assert_raise RuntimeError, "'health' is reserved name", fn ->
Subject.transform(:stores, %{health: %{type: "Blackhole"}})
property "raises when uses reserved name" do
check all name <- string(:alphanumeric) do
assert_raise RuntimeError,
"'_#{name}' cannot start with underscore",
fn ->
Subject.transform(:stores, %{
:"_#{name}" => %{type: "Blackhole"}
})
end
end
end

Expand Down
3 changes: 2 additions & 1 deletion test/imager_web/controllers/health_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule ImagerWeb.Controllers.HealthTest do
use ImagerWeb.ConnCase, async: true

test "returns 200 on existing file", %{conn: conn} do
assert %{"status" => "pass"} = conn |> get("/health") |> json_response(200)
assert %{"status" => "pass"} =
conn |> get("/__health") |> json_response(200)
end
end
8 changes: 4 additions & 4 deletions test/imager_web/plug/metrics_exporter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ defmodule ImagerWeb.Plug.MetricsExporterTest do
end

property "non-metric endpoints are passed through" do
opts = Subject.init(endpoint: "/metric")
opts = Subject.init([])

check all path <- path(),
method <- http_method(),
path != "/metric" do
path != "/__metrics" do
conn = conn(method, path)

assert conn == Subject.call(conn, opts)
end
end

test "metric endpoint return 200" do
opts = Subject.init(endpoint: "/metric")
conn = conn(:get, "/metric")
opts = Subject.init([])
conn = conn(:get, "/__metrics")

assert {200, _, _} = sent_resp(Subject.call(conn, opts))
end
Expand Down

0 comments on commit 29db294

Please sign in to comment.