Packages
Metrics for event_bus lib
Retired package: Deprecated - Please refer to latest version.
Current section
Files
Jump to
Current section
Files
lib/event_bus_metrics/web/topics_controller.ex
defmodule EventBus.Metrics.Web.TopicsController do
@moduledoc """
Router for events
"""
use Plug.Router
import EventBus.Metrics.Web.BaseRouter
alias EventBus.Metrics
plug(:match)
plug(:dispatch)
# List topics
get "/" do
topics = Metrics.list_topics()
send_json(conn, 200, %{topics: topics})
end
# Fetch a topic
get "/:topic_name" do
case Metrics.topic_exist?(topic_name) do
true ->
topic = Metrics.find_topic(topic_name)
send_json(conn, 200, %{topic: topic})
false ->
send_not_found(conn, "topic")
end
end
# Fetch topic events
get "/:topic_name/events" do
case Metrics.topic_exist?(topic_name) do
true ->
events = %{events: Metrics.list_events(topic_name)}
send_json(conn, 200, events)
false ->
send_not_found(conn, "topic")
end
end
# Fetch a topic event with details
get "/:topic_name/events/:event_id" do
case Metrics.topic_exist?(topic_name) do
true ->
case Metrics.find_event(topic_name, event_id) do
nil -> send_not_found(conn, "event")
event -> send_json(conn, 200, %{event: event})
end
false ->
send_not_found(conn, "topic")
end
end
options _ do
conn
|> put_resp_header("Access-Control-Allow-Origin", "*")
|> put_resp_header("Access-Control-Allow-Methods", "GET")
|> put_resp_header("Access-Control-Allow-Credentials", "true")
|> send_json(200, nil)
end
match(_, do: send_not_found(conn, "path"))
end