Skip to content

Instantly share code, notes, and snippets.

@tastywheat
Last active April 8, 2016 21:57
Show Gist options
  • Save tastywheat/877e9307527226ad398e74baacfaa88a to your computer and use it in GitHub Desktop.
Save tastywheat/877e9307527226ad398e74baacfaa88a to your computer and use it in GitHub Desktop.
elixir connecting nodes
# iex --sname b1 --cookie monster -S mix
# iex --sname b2 --cookie monster -S mix
# Node.connect(:"b1@localhost")
# http://www.brianstorti.com/getting-started-with-plug-elixir/
defmodule HelloPlug.Worker do
use GenServer
def start_link do
Plug.Adapters.Cowboy.http(MyPipeline, %{})
GenServer.start_link(__MODULE__, [], name: :worker)
end
end
defmodule MyPipeline do
# We use Plug.Builder to have access to the plug/2 macro.
# This macro can receive a function or a module plug and an
# optional parameter that will be passed unchanged to the
# given plug.
use Plug.Builder
plug Plug.Logger
plug :extract_name
plug :greet, %{my_option: "Hello"}
def extract_name(%Plug.Conn{request_path: "/" <> name} = conn, opts) do
assign(conn, :name, name)
end
def greet(conn, opts) do
msg = HelloPlug.Bot.get_messages
conn
|> send_resp(200, "#{opts[:my_option]}, #{msg}")
end
end
defmodule HelloPlug.Bot do
use GenServer
def start_link do
GenServer.start_link(__MODULE__, 123, name: {:global, "bot"})
end
defp via_tuple(room_name) do
{:via, :gproc, {:n, :g, {:chat_room, room_name}}}
end
# Client
def get_messages do
GenServer.call(:global.whereis_name("bot"), :get_messages)
end
# Server
def handle_call(:get_messages, _from, state) do
{:reply, state, state}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment