diff --git a/lib/marathon/binge_watch.ex b/lib/marathon/binge_watch.ex index 60450a9..4f1698e 100644 --- a/lib/marathon/binge_watch.ex +++ b/lib/marathon/binge_watch.ex @@ -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) diff --git a/lib/mix/dev_server.ex b/lib/mix/dev_server.ex new file mode 100644 index 0000000..baeac15 --- /dev/null +++ b/lib/mix/dev_server.ex @@ -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 diff --git a/lib/plug/no_op.ex b/lib/plug/no_op.ex new file mode 100644 index 0000000..ebce70d --- /dev/null +++ b/lib/plug/no_op.ex @@ -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