Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add a simple cowboy http listener to display data queried from the co…

…llector
  • Loading branch information...
commit 2e8d4217369c6ab33b83d9a5c77abe177f2d0f5a 1 parent 5258b7c
@clofresh authored
Showing with 38 additions and 1 deletion.
  1. +3 −1 rebar.config
  2. +35 −0 src/cerebrate.ex
View
4 rebar.config
@@ -1,5 +1,7 @@
{deps, [{dnssd, "0.*",
{git, "https://github.com/andrewtj/dnssd_erlang.git", {branch, "master"}}},
{elixir, "0.*",
- {git, "https://github.com/elixir-lang/elixir.git", {branch, "master"}}}
+ {git, "https://github.com/elixir-lang/elixir.git", {branch, "master"}}},
+ {cowboy, "0.*",
+ {git, "https://github.com/extend/cowboy.git", {branch, "master"}}}
]}.
View
35 src/cerebrate.ex
@@ -3,11 +3,19 @@ defmodule :cerebrate do
def start() do
Erlang.application.start :dnssd
+ Erlang.application.start :cowboy
Erlang.application.start :cerebrate
end
def start(_type, _args) do
IO.puts "Starting cerebrate"
+ dispatch = [
+ {:'_', [{:'_', CerebrateWeb, []}]}
+ ]
+ Erlang.cowboy.start_listener(:cerebrate_http_listener, 100,
+ :cowboy_tcp_transport, [{:port, 8080}],
+ :cowboy_http_protocol, [{:dispatch, dispatch}]
+ )
Cerebrate.Supervisor.start_link()
end
@@ -35,3 +43,30 @@ defmodule Cerebrate do
end
end
end
+
+defmodule CerebrateWeb do
+ @behavior :cowboy_http_handler
+
+ def init({_, :http}, req, []) do
+ {:ok, req, :undefined}
+ end
+
+ def handle(req, state) do
+ Process.whereis(:collector) <- {:query, Process.self()}
+ {:ok, req2} = receive do
+ match: data
+ output = Enum.map data, fn({metric, value}) ->
+ [metric, float_to_list(value)]
+ end
+ Erlang.cowboy_http_req.reply(200, [], ["Data:", output], req)
+ after: 5000
+ Erlang.cowboy_http_req.reply(500, [], "Timed out", req)
+ end
+ {:ok, req2, state}
+ end
+
+ def terminate(_, _) do
+ :ok
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.