Applying a VIM macro to a set of lines

  1. Start a macro typing q + macro name (in that case was q as well)
  2. Record the commands. I was using ds' which uses the surround plugin to delete the quotes.
  3. Finish the recording with q again.
  4. Select the lines with V
  5. Apply the macro to the select lines with :norm! @q

Reference:

  • http://stackoverflow.com/questions/390174/in-vim-how-do-i-apply-a-macro-to-a-set-of-lines
  • https://github.com/tpope/vim-surround

Suspend vagrant before Mac sleep

Lately I was getting a lot of problems with my VM and even with the bluetooth on Mac. Just because the vmware VM was not recovering well after my macbook waking up.

I use a VM everyday and I always forget to suspend/halt that before sleep my macbook. So I find out is possible to configure scripts to be executed on wakeup/sleep the machine through SleepWatcher.

This blog post Mac OS X: Automating Tasks on Sleep by Kodiak explain really well how to setup the sleepwatcher.

Summarizing:

# 1. install
brew install sleepwatcher

# 2. create a script "~/.sleep" to be executed before the machine sleep
# in my case was "cd <vm-directory> && vagrant suspend"

# 3. set full permissions to the owner
chmod 700 ~/.sleep

# 4. start the watcher then put your machine to sleep to test the script is working
/usr/local/sbin/sleepwatcher --verbose --sleep ~/.sleep

# configure the deamon
sudo chown root:wheel /usr/local/Cellar/sleepwatcher/2.2/de.bernhard-baehr.sleepwatcher-20compatibility-localuser.plist

ln -sfv /usr/local/Cellar/sleepwatcher/2.2/de.bernhard-baehr.sleepwatcher-20compatibility-localuser.plist ~/Library/LaunchAgents/

launchctl load ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher-20compatibility-localuser.plist

Knextancy - Node JS Multi Tenancy

At Bravi every project has multi tenant support since the beginning of the development. This because we have built a solution that is so easy to use that is almost imperceptible we are developing a multi tenant application. The only thing we have to care about is:

  • pass ahead in the HTTP header the tenant id for every consumed API/application
  • use a prefix in the table on executing SQL queries select * from "$_mytable"

And that is it. You are already building a multi tenant application.

How does that work?

Well. The library knextancy takes care of everything for us.

Knextancy is built on top of knex and implements multi tenant using table prefixes. So all the clients share the same database but each one has it own table. For example:

Every time a request like this one below is done:

curl -X GET -H "x-client-id: 11" "http://localhost:3000/"

Knextancy will identify the tenant associated to that request based in the x-client-id. In case is the first request for that server, knextancy also will execute the migration and seed tasks. So there is no manual configuration required for bringing up a database for a new client.

You may ask yourself. How is that secure if the tenant is based in HTTP header? Then the user could change manually the tenant he is accessing.

Well. This is not a problem for services running inside a private network. For the public app/services, we run everything behind a proxy, that injects the x-client-id based in the domain.

After that knextancy injects in the request an instance of knex with the context of the current client (e.g. req.knex).

Then to execute any SQL query with multi tenant support is just required to use the req.knex and any table must have prefix $_ to allow knextancy resolve the table for the current client.

Simple example of how to build a multi tenant REST API. Using knextancy with express and PostgreSQL database. https://github.com/bravi-software/knextancy-example

Understanding a Phoenix application and it's lifecycle

It is a post for beginners in Elixir and Phoenix. Has intention to help understanding how a Phoenix application works and a bit of its lifecycle.

I started the day thinking in write an chat application to apply what I have learned so far of Elixir. But even after reading the whole Elixir - Getting Started and Phoenix - Up And Running. I was not really feeling prepared to write an application on my own.

P.S. I didn’t read the Phoenix framework documentation before start the application. I’m sure if I had read the docs, everything would makes sense. But I was just too excited to start coding :D.

So instead of writing a chat app from scratch. What we gonna do is understand how the chat app built by Chris McCord works.

Before understanding, lets see the application running:

  • Clone the repo chrismccord/phoenix_chat_example, then cd to the new directory
  • Install dependencies with mix deps.get
  • (optional) Install npm dependencies to customize the ES6 js/Sass npm install
  • Start Phoenix router with mix phoenix.server

Now you can visit localhost:4000 from your browser.

mix.exs

Contains the definition for all dependencies and configures the OTP application.

Check out elixir/Application to understand what is an OTP application. And the presentation OTP Has Done It - by Nick DeMonner also gives an idea about it.

defmodule Chat.Mixfile do
  use Mix.Project

  def project do
    [app: :chat,
     version: "0.0.1",
     elixir: "~> 1.0",
     elixirc_paths: ["lib", "web"],
     compilers: [:phoenix] ++ Mix.compilers,
     deps: deps]
  end

  # Configuration for the OTP application
  #
  # Type `mix help compile.app` for more information
  def application do
    [mod: {Chat, []},
     applications: [:phoenix, :phoenix_html, :cowboy, :logger]]
  end

  # Specifies your project dependencies
  #
  # Type `mix help deps` for examples and options
  defp deps do
    [{:phoenix, "~> 1.0.0"},
     {:phoenix_html, "~> 2.1"},
     {:phoenix_live_reload, "~> 1.0", only: :dev},
     {:phoenix_ecto, "~> 1.1"},
     {:postgrex, ">= 0.0.0"},
     {:cowboy, "~> 1.0"}]
  end
end

In the application function is defined the Chat as the startup module. And also is defined all applications your application depends on at runtime.

lib/chat.ex

defmodule Chat do
  use Application

  # See http://elixir-lang.org/docs/stable/elixir/Application.html
  # for more information on OTP Applications
  def start(_type, _args) do
    import Supervisor.Spec, warn: false

    children = [
      # Start the endpoint when the application starts
      supervisor(Chat.Endpoint, []),
      # Start the Ecto repository
      worker(Chat.Repo, []),
      # Here you could define other workers and supervisors as children
      # worker(Chat.Worker, [arg1, arg2, arg3]),
    ]

    # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: Chat.Supervisor]
    Supervisor.start_link(children, opts)
  end

  # Tell Phoenix to update the endpoint configuration
  # whenever the application is updated.
  def config_change(changed, _new, removed) do
    Chat.Endpoint.config_change(changed, removed)
    :ok
  end
end

In lib/chat.ex is defined the OTP Application. As we can see the Chat.Endpoint is started as supervisor. Which will start the endpoint when the application starts and it will be restarted in case it crashes.

And Chat.Repo is started as worker. Which will run the repository in a different process. Allowing this way to keep the state (e.g. connection pool) between different requests. Otherwise would be necessary establish a new DB connection for every request.

lib/chat/endpoint.ex

defmodule Chat.Endpoint do
  use Phoenix.Endpoint, otp_app: :chat

  # Requests coming at "/socket" path will be handled by
  # UserSocket (web/channels/user_socket.ex)
  socket "/socket", Chat.UserSocket


  # Serve at "/" the given assets from "priv/static" directory
  plug Plug.Static,
    at: "/", from: :chat,
    only: ~w(css images js favicon.ico robots.txt)

  # Code reloading will only work if the :code_reloader key of
  # the :phoenix application is set to true in your config file.
  if code_reloading? do
    socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
    plug Phoenix.CodeReloader
    plug Phoenix.LiveReloader
  end

  # Log the requests
  plug Plug.Logger

  # Configure parsers
  plug Plug.Parsers,
    parsers: [:urlencoded, :multipart, :json],
    pass: ["*/*"],
    json_decoder: Poison

  plug Plug.MethodOverride
  plug Plug.Head

  plug Plug.Session,
    store: :cookie,
    key: "_chat_key",
    signing_salt: "LH6XmqGb",
    encryption_salt: "CIPZg4Qo"

  # Only after passing through all the previous Plug
  # the request will be handled by the Chat.Router (web/router.ex)
  plug Chat.Router
end

In lib/chat/endpoint.ex is used a lot Plug. It allows compose modules between web applications. With Plug is possible to change the request and response data through the connection lifecycle. It is comparable to a middleware in Node JS.

Check out understanding-plug and plug project.

web/router.ex

defmodule Chat.Router do
  use Phoenix.Router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/", Chat do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :index
  end
end

In Chat.Router we see the definition of pipeline. Which is a simple way to pipe a series of plug before passing the request ahead to a controller. That can be used for different type of requests. For example: an API request must be handled differently of a browser (page) request.

Once a request arrives at the Phoenix router, it performs a series of transformations through pipelines until the request is dispatched to a desired end-point.

Such transformations are defined via plugs, as defined in the Plug specification. Once a pipeline is defined, it can be piped through per scope.

http://hexdocs.pm/phoenix/Phoenix.Router.html

web/controllers/page_catroller.ex

defmodule Chat.PageController do
  use Chat.Web, :controller

  def index(conn, _params) do
    render conn, "index.html"
  end
end

After a request passing through all previous plug. It will be handled by the controller. For instance a GET / will respond with index.html page.

channels/user_socket.ex

As we saw before in the Chat.Endpoint the socket connections will be handled by the Chat.UserSocket.

defmodule Chat.UserSocket do
  use Phoenix.Socket

  channel "rooms:*", Chat.RoomChannel

  transport :websocket, Phoenix.Transports.WebSocket
  transport :longpoll, Phoenix.Transports.LongPoll

  def connect(_params, socket) do
    {:ok, socket}
  end

  def id(_socket), do: nil
end

Basically the Chat.UserSocket creates a channel for topics matching rooms:*. And add support for both web socket and log pool connections.

channels/room_socket.ex

defmodule Chat.RoomChannel do
  use Phoenix.Channel
  require Logger

  @doc """
  Authorize socket to subscribe and broadcast events on this channel & topic

  Possible Return Values

  `{:ok, socket}` to authorize subscription for channel for requested topic

  `:ignore` to deny subscription/broadcast on this channel
  for the requested topic
  """
  def join("rooms:lobby", message, socket) do
    # Exit signals arriving to a process are converted to {'EXIT', From, Reason} messages,
    # which can be received as ordinary messages
    Process.flag(:trap_exit, true)
    :timer.send_interval(5000, :ping)
    send(self, {:after_join, message})

    {:ok, socket}
  end

  def join("rooms:" <> _private_subtopic, _message, _socket) do
    {:error, %{reason: "unauthorized"}}
  end

  def handle_info({:after_join, msg}, socket) do
    broadcast! socket, "user:entered", %{user: msg["user"]}
    push socket, "join", %{status: "connected"}
    {:noreply, socket}
  end
  def handle_info(:ping, socket) do
    push socket, "new:msg", %{user: "SYSTEM", body: "ping"}
    {:noreply, socket}
  end

  def terminate(reason, _socket) do
    Logger.debug"> leave #{inspect reason}"
    :ok
  end

  def handle_in("new:msg", msg, socket) do
    broadcast! socket, "new:msg", %{user: msg["user"], body: msg["body"]}
    {:reply, {:ok, %{msg: msg["body"]}}, assign(socket, :user, msg["user"])}
  end
end

Pretty simple, it handles: * new users join the channel * broadcast new users in the chat * send ping messages * broadcast a user’s message

Check out Phoenix.Channel for a further explanation.

The rest is just a HTML page loading a CSS script (source in Sass) and a Javascript script (source in ES6) which consumes the socket provided by the Chat channel.

P.S. I have just started learning Elixir and Phoenix. Let me know if I had misunderstood something.

Running a simple web server from any directory

You just need Python installed to run a simple web server from any directory. On Mac Python is already installed. So you just need follow these steps below:

Run these commands in the terminal:

# access the directory
cd /path/to/directory

# start the web server in the port 8000
python -m SimpleHTTPServer 8000

# execute Ctrl+C to kill the web server

Open the browser and access the web server through http://localhost:8000.

Demo

See the demo in the original scale