From 9d161648cc0b30dd9e0ead12f3bd5a379b26c815 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 31 Aug 2018 15:51:45 +0200 Subject: Remove web interface, deregistration for chat subs on death --- lib/app/chat.ex | 6 ++++++ lib/application.ex | 3 --- lib/web/httprouter.ex_ | 53 -------------------------------------------------- mix.exs | 8 ++------ 4 files changed, 8 insertions(+), 62 deletions(-) delete mode 100644 lib/web/httprouter.ex_ diff --git a/lib/app/chat.ex b/lib/app/chat.ex index a9cfb1e..bc9f5de 100644 --- a/lib/app/chat.ex +++ b/lib/app/chat.ex @@ -117,6 +117,7 @@ defmodule SApp.Chat do end def handle_cast({:subscribe, pid}, state) do + Process.monitor(pid) new_subs = MapSet.put(state.subs, pid) {:noreply, %{ state | subs: new_subs }} end @@ -159,6 +160,11 @@ defmodule SApp.Chat do {:noreply, %{state | store: new_store}} end + def handle_info({:DOWN, _ref, :process, pid, _reason}, state) do + new_subs = MapSet.delete(state.subs, pid) + {:noreply, %{ state | subs: new_subs }} + end + defp push_messages(state, to, start, num) do case ML.read(state.store, start, num) do {:ok, list, rest} -> diff --git a/lib/application.ex b/lib/application.ex index eef02f0..3e3a6ac 100644 --- a/lib/application.ex +++ b/lib/application.ex @@ -23,9 +23,6 @@ defmodule Shard.Application do # Applications & data store { Shard.Manager, listen_port }, - - # Web UI - # Plug.Adapters.Cowboy.child_spec(:http, SWeb.HTTPRouter, [], port: listen_port + 1000) ] # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html diff --git a/lib/web/httprouter.ex_ b/lib/web/httprouter.ex_ deleted file mode 100644 index 57af9f9..0000000 --- a/lib/web/httprouter.ex_ +++ /dev/null @@ -1,53 +0,0 @@ -defmodule SWeb.HTTPRouter do - use Plug.Router - use Plug.ErrorHandler - - plug Plug.Parsers, parsers: [:urlencoded, :multipart] - - plug :match - plug :dispatch - - get "/" do - main_page(conn) - end - - post "/" do - if Map.has_key?(conn.params, "msg") do - SApp.Chat.send(conn.params["msg"]) - end - if Map.has_key?(conn.params, "nick") do - Shard.Identity.set_nickname(conn.params["nick"]) - end - if Map.has_key?(conn.params, "peer") do - [ipstr, portstr] = String.split(conn.params["peer"], ":") - {:ok, ip} = :inet.parse_address (to_charlist ipstr) - {port, _} = Integer.parse portstr - Shard.Manager.add_peer(ip, port) - end - - main_page(conn) - end - - match _ do - send_resp(conn, 404, "Oops!") - end - - def main_page(conn) do - {:ok, messages, _} = GenServer.call(SApp.Chat.Log, {:read, nil, 42}) - - msgtxt = messages - |> Enum.map(fn {ts, nick, msg} -> "#{ts |> DateTime.from_unix! |> DateTime.to_iso8601} <#{nick}> #{msg}\n" end) - - peerlist = SNet.ConnSupervisor - |> DynamicSupervisor.which_children - |> Enum.map(fn {_, pid, _, _} -> "#{GenServer.call(pid, :get_host_str)}\n" end) - - conn - |> put_resp_content_type("text/html") - |> send_resp(200, "
#{msgtxt}
" <> - "
" <> - "
" <> - "
#{peerlist}
" <> - "
") - end -end diff --git a/mix.exs b/mix.exs index 71dac77..7192feb 100644 --- a/mix.exs +++ b/mix.exs @@ -25,13 +25,9 @@ defmodule Shard.MixProject do # Run "mix help deps" to learn about dependencies. defp deps do [ - # {:dep_from_hexpm, "~> 0.3.0"}, - # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}, - # {:cowboy, "~> 1.1.2"}, - # {:plug, "~> 1.3.4"}, - {:excoveralls, "~> 0.10", only: :test}, + {:excoveralls, "~> 0.10", only: :test}, - {:salty, "~> 0.1.3", hex: :libsalty}, + {:salty, "~> 0.1.3", hex: :libsalty}, ] end end -- cgit v1.2.3