aboutsummaryrefslogtreecommitdiff
path: root/lib/web/httprouter.ex_
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2018-08-27 13:04:51 +0200
committerAlex Auvolat <alex@adnab.me>2018-08-27 13:04:51 +0200
commit6cc81b55f2466cd7526f47da6980e3eb47041457 (patch)
treebb0d7a917ec31443ca5763f9d1386f94e86d8c89 /lib/web/httprouter.ex_
parentc1dad415d42d0991f3c549c159d85b6146fb0768 (diff)
downloadshard-6cc81b55f2466cd7526f47da6980e3eb47041457.tar.gz
shard-6cc81b55f2466cd7526f47da6980e3eb47041457.zip
Disable web interface
Diffstat (limited to 'lib/web/httprouter.ex_')
-rw-r--r--lib/web/httprouter.ex_53
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/web/httprouter.ex_ b/lib/web/httprouter.ex_
new file mode 100644
index 0000000..57af9f9
--- /dev/null
+++ b/lib/web/httprouter.ex_
@@ -0,0 +1,53 @@
+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} &lt;#{nick}&gt; #{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, "<pre>#{msgtxt}</pre>" <>
+ "<form method=POST><input type=text name=msg /><input type=submit value=send /></form>" <>
+ "<form method=POST><input type=text name=nick value=\"#{Shard.Identity.get_nickname}\" /><input type=submit value=\"change nick\" /></form>" <>
+ "<hr/><pre>#{peerlist}</pre>" <>
+ "<form method=POST><input type=text name=peer /><input type=submit value=\"add peer\" /></form>")
+ end
+end