diff --git a/lib/application.ex b/lib/application.ex index 17b50c8..8834389 100644 --- a/lib/application.ex +++ b/lib/application.ex @@ -10,17 +10,10 @@ defmodule Sample.Application do end end - def env(_app) do - [ - {:port, 8002} - ] - end - def start(_, _) do + opts = [ strategy: :one_for_one, name: Sample.Supervisor] children = [ { Bandit, scheme: :http, plug: Sample.Static, port: 8004 }, { Bandit, scheme: :http, plug: Sample.WS, port: 8002 } ] - opts = [strategy: :one_for_one, name: Sample.Supervisor] -# :cowboy.start_clear(:http, env(:sample), %{env: %{dispatch: :n2o_cowboy.points()}}) :kvs.join() Supervisor.start_link(children, strategy: :one_for_one, name: Sample.Supervisor) end diff --git a/lib/routes.ex b/lib/routes.ex deleted file mode 100644 index c82820a..0000000 --- a/lib/routes.ex +++ /dev/null @@ -1,21 +0,0 @@ -defmodule Sample.Routes do - require N2O - - def finish(state, context), do: {:ok, state, context} - - def init(state, context) do - %{path: path} = N2O.cx(context, :req) - {:ok, state, N2O.cx(context, path: path, module: route_prefix(path))} - end - - defp route_prefix(<<"/ws/", p::binary>>), do: route(p) - defp route_prefix(<<"/", p::binary>>), do: route(p) - defp route_prefix(path), do: route(path) - - defp route(<<>>), do: Sample.Login - defp route(<<"index", _::binary>>), do: Sample.Index - defp route(<<"login", _::binary>>), do: Sample.Login - defp route(<<"app/index", _::binary>>), do: Sample.Index - defp route(<<"app/login", _::binary>>), do: Sample.Login - defp route(_), do: Sample.Login -end diff --git a/lib/static.ex b/lib/static.ex index fca559b..437f910 100644 --- a/lib/static.ex +++ b/lib/static.ex @@ -1,6 +1,5 @@ defmodule Sample.Static do use Plug.Router plug Plug.Static, from: {:sample, "priv/static"}, at: "/app" - match _ do send_resp(conn, 404, - "Please refer to https://n2o.dev for information about endpoints addresses.") end + match _ do send_resp(conn, 404, "Please refer to https://n2o.dev for more information.") end end diff --git a/lib/ws.ex b/lib/ws.ex index c9a13d0..f6acef0 100644 --- a/lib/ws.ex +++ b/lib/ws.ex @@ -1,37 +1,25 @@ defmodule Sample.WS do use Plug.Router + require N2O plug :match plug :dispatch - get "/" do send_resp(conn, 200, "NONE") end - get "/ws/app/index.htm" do conn |> WebSockAdapter.upgrade(Sample.WS, [], timeout: 60_000) |> halt() end - get "/ws/app/login.htm" do conn |> WebSockAdapter.upgrade(Sample.WS, [], timeout: 60_000) |> halt() end + def extract("index" <> __), do: Sample.Index + def extract("login" <> __), do: Sample.Login - def init(args) do {:ok, []} end + get "/", do: send_resp(conn, 200, "NONE") + get "/ws/app/:mod", do: conn |> WebSockAdapter.upgrade(Sample.WS, [module: extract(mod)], timeout: 60_000) |> halt() - def handle_in({"N2O," <> key = message, options}, state) do - {_,m,r,s} = :n2o_proto.stream(message,[],state) - :io.format 'N2O: ~p~n', [key] - {:reply, :ok, m, s} - end - - def handle_in({"PING", options}, state) do - :io.format 'PING~n' - {:reply, :ok, {:text, "PONG"}, state} - end - - def handle_in({message, options}, state) do - :io.format 'Binary: ~p~n', [message] - {x,m,r,s} = :n2o_proto.stream(message,[],state) - :io.format 'Processed: ~p~n', [{x,m,r,s}] - response({x,m,r,s}) - end + def init(args), do: {:ok, N2O.cx(module: Keyword.get(args, :module)) } + def handle_in({"N2O," <> _ = message, _}, state), do: response(:n2o_proto.stream({:text,message},[],state)) + def handle_in({"PING", _}, state), do: {:reply, :ok, {:text, "PONG"}, state} + def handle_in({message, _}, state), do: response(:n2o_proto.stream(message,[],state)) def response({:reply,{:binary,rep},_,s}), do: {:reply,:ok,{:binary,rep},s} - def response({:reply,{:text,rep},_,s}), do: {:reply,:ok,{:text,rep},s} - def response({:reply,{:bert,rep},_,s}), do: {:reply,:ok,{:binary,:n2o_bert.encode(rep)},s} - def response({:reply,{:json,rep},_,s}), do: {:reply,:ok,{:binary,:n2o_json.encode(rep)},s} + def response({:reply,{:text,rep},_,s}), do: {:reply,:ok,{:text,rep},s} + def response({:reply,{:bert,rep},_,s}), do: {:reply,:ok,{:binary,:n2o_bert.encode(rep)},s} + def response({:reply,{:json,rep},_,s}), do: {:reply,:ok,{:binary,:n2o_json.encode(rep)},s} def response({:reply,{encoder,rep},_,s}), do: {:reply,:ok,{:binary,encoder.encode(rep)},s} match _ do send_resp(conn, 404, "Please refer to https://n2o.dev for more information.") end diff --git a/priv/static/index.htm b/priv/static/index.htm index f4d589f..809eb6f 100644 --- a/priv/static/index.htm +++ b/priv/static/index.htm @@ -24,7 +24,7 @@

room

- + diff --git a/priv/static/login.htm b/priv/static/login.htm index 8665ea5..9f3c7cd 100644 --- a/priv/static/login.htm +++ b/priv/static/login.htm @@ -25,7 +25,7 @@ - +