How to Set Up a Service Oriented Architecture for Development

Posted by Danny Olson

Service oriented architectures can have many benefits, but there are trade-offs as well. One immediate annoyance is setting up multiple applications to do local development.

The current workflow looks like this:

cd app1
$ RACK_ENV=development rackup -p 3002

# open a new tab
$ cd app2
$ RACK_ENV=development rackup -p 4000

# open a new tab
$ cd app3
$ RACK_ENV=development rackup -p 5000

# etc.

It’s also convenient to label the tabs to know where to look for an application’s output, so we need to name each tab as well. This is boring and error prone as it’s very easy to forget to start an application that could prevent the entire system from working as expected.

Consular to the Rescue!

We just recently found out about Consular to automate terminal tasks and turn the above headache into a one command solution.

Setup is simple:

gem install consular
$ gem install consular-iterm
$ rbenv rehash # if appropriate
$ consular init

This generates ~/.consularc for customization, and ~/.config/consular for projects. If you’re using iTerm, you’ll need to require the necessary core.

# ~/.consularc
require 'consular/iterm'

# the rest of the file

Now generate a project:

$ consular edit soa-setup

Here is an example project that replaces the current workflow:

# ~/.config/consular/soa-setup.term
tab 'app1' do
  run 'cd ~/workspace/app1'
  run 'RACK_ENV=development rackup -p 3002'
end

tab 'app2' do
  run 'cd ~/workspace/app1'
  run 'RACK_ENV=development rackup -p 4000'
end

tab 'app3' do
  run 'cd ~/workspace/app3'
  run 'RACK_ENV=development rackup -p 5000'
end

Now all you need to do is run one command, consular start soa-setup.

Some Tweaks

It’s nice to not have to do this (minimal) song and dance for each development machine, so we packaged this up into some scripts to make the process easier.

# bin/setup
echo "Installing Consular..."

bundle install
rbenv rehash
consular init

# prepend require to file
echo "require 'consular/iterm'\n" > tmpfile
cat ~/.consularc >> tmpfile
cp tmpfile ~/.consularc
rm tmpfile

# copy project(s) to home directory to use anywhere
cp lib/default.term ~/.config/consular/

echo "\nRun bin/run to start all applications."
# bin/run
consular start default
# lib/default.term
tab 'app1' do
  run 'cd ~/workspace/app1'
  run 'RACK_ENV=development rackup -p 3002'
end

tab 'app2' do
  run 'cd ~/workspace/app2'
  run 'RACK_ENV=development rackup -p 4000'
end

tab 'app3' do
  run 'cd ~/workspace/app3'
  run 'RACK_ENV=development rackup -p 5000'
end
# Gemfile
source 'https://rubygems.org'

gem 'consular'
gem 'consular-iterm'

Finally, you can create a symlink to the run command:

ln -s ~/path/to/bin/run ~/launch-apps

Go forth and be lazy, I mean, virtuous!