Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
Add local dev server mix task for developing against heimdall (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
emptyflash authored Feb 9, 2017
1 parent f409626 commit 29997ea
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/marathon/binge_watch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ defmodule Heimdall.Marathon.BingeWatch do
end
end

defp register_routes(routes) do
def register_routes(routes) do
DynamicRoutes.unregister_all(:heimdall_routes)
Enum.each routes, fn {host, path, plug, opts} ->
DynamicRoutes.register(:heimdall_routes, host, path, plug, opts)
Expand Down
53 changes: 53 additions & 0 deletions lib/mix/dev_server.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
defmodule Mix.Tasks.DevServer do
@moduledoc false
use Mix.Task

def filters(plugs) do
plugs
|> String.split(",")
|> Poison.encode!
end

def options(opts) do
opts
|> String.split(",")
|> Enum.map(&String.split(&1, "="))
|> Enum.map(fn [k, v | _] -> {k, v} end)
|> Enum.into(%{})
|> Poison.encode!()
end

def routes_from_args(args) do
cond do
length(args) == 0 -> []
length(args) < 4 ->
IO.puts "Not enough parameters for args:"
IO.inspect args
[]
true ->
[host, path, plugs, opts | tail] = args
app = %{
"labels" => %{
"heimdall.host" => host,
"heimdall.path" => path,
"heimdall.filters" => filters(plugs),
"heimdall.options" => options(opts)
}
}
[app] ++ routes_from_args(tail)
end
end

def run(args) do
Mix.Config.persist(heimdall: [register_marathon: false])
Mix.Task.run "app.start", []
routes =
args
|> routes_from_args
|> Heimdall.Marathon.BingeWatch.build_routes
|> Heimdall.Marathon.BingeWatch.register_routes
IO.puts "Registered routes:"
IO.inspect routes
:timer.sleep(:infinity)
end
end
8 changes: 8 additions & 0 deletions lib/plug/no_op.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Heimdall.Plug.NoOp do
@moduledoc """
A plug that does nothing.
"""

def init(opts), do: opts
def call(conn, _opts), do: conn
end

0 comments on commit 29997ea

Please sign in to comment.