Where am I?

Uh-oh: Where Am I?

May 30, 2013, by

OMG, did I just do that in Production? or am I in Staging?  I had just deleted a random user and it was only after I’ve committed the change it that I asked myself the question as I panicked and gasped for air.  This was my motivation for creating rack-showme, middleware to automatically label pages with the name of the environment at all times

Using a Staging server is a common software development practice. The idea behind Staging is to be able to test and review new code in a Production-like, sandbox environment.  Changes, improvements, and screw ups done in the Staging environment don’t affect users.

A couple of weeks ago I had been working on a bug fix that was related to deleting a User, so to test the fix I opened my browser and went to the page pointing to the Staging environment and deleted a random user.  Simple, right?  However, just a millisecond after I clicked on the confirmation button, I realized that I was not certain whether I had done that in the Staging or the Production site.  In a panic, I looked up at the URL bar and after a moment I could breathe again when I confirmed that I had deleted the user in the Staging site.

I’ve had these moments many times before, but this once convinced me to do something about it and that’s when I came up with the idea of rack-showme.

Rack Showme

This is a Rack middleware that inserts a message in all HTTP responses. This way we can set a message that makes it easier to know if we’re in Staging or Production.

Using with Rails

As middleware, rack-showme can easily integrate with any Rails app.

The first step is, as always, is to add the gem to our Gemfile,

<span class="c1"># Gemfile</span>

  <span class="n">gem</span> <span class="s1">'rack-showme'</span>

and run

bundle install

In order to know when we’re in Staging we want to see the message “_staging_” whenever we’re running in Staging mode. So we need to add and configure rack-showme in the Staging environment file.

<span class="c1"># config/enviroments/staging.rb</span>
  <span class="o">.</span><span class="n">.</span><span class="o">.</span>
  <span class="n">config</span><span class="o">.</span><span class="n">middleware</span><span class="o">.</span><span class="n">use</span> <span class="s2">"Rack::Showme"</span>
  <span class="ss">Rack</span><span class="p">:</span><span class="ss">:Showme</span><span class="o">::</span><span class="no">Options</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="s2">"Staging"</span>

Don’t forget to also create an staging section in your database.yml file

<span class="c1"># config/database.yml</span>
  <span class="ss">staging</span><span class="p">:</span>
  <span class="ss">adapter</span><span class="p">:</span> <span class="n">sqlite3</span>
  <span class="ss">database</span><span class="p">:</span> <span class="n">db</span><span class="o">/</span><span class="n">development</span><span class="o">.</span><span class="n">sqlite3</span>
  <span class="ss">pool</span><span class="p">:</span> <span class="mi">5</span>
  <span class="ss">timeout</span><span class="p">:</span> <span class="mi">5000</span>

In this case we use the Staging environment file, but you can use Development or even the Production environment file.

Now we can run our Rails app.

<span class="nv">RAILS_ENV</span><span class="o">=</span>staging rails s

And this is what it will looks like,

staging

If you want something more showy that the yellow background, you can choose between the colorschemes yellow, green, and red. You can select your preferred colorscheme with the colorscheme option,

<span class="c1"># config/enviroments/staging.rb</span>
<span class="o">.</span><span class="n">.</span><span class="o">.</span>
    <span class="n">config</span><span class="o">.</span><span class="n">middleware</span><span class="o">.</span><span class="n">use</span> <span class="s2">"Rack::Showme"</span>
    <span class="ss">Rack</span><span class="p">:</span><span class="ss">:Showme</span><span class="o">::</span><span class="no">Options</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="s2">"Staging"</span>
    <span class="ss">Rack</span><span class="p">:</span><span class="ss">:Showme</span><span class="o">::</span><span class="no">Options</span><span class="o">.</span><span class="n">colorscheme</span><span class="o">=</span> <span class="s2">"red"</span> <span class="c1"># Other options are: yellow and green</span>

This is what it looks like with the red colorscheme,

staging

And this is with the green colorscheme,

staging

Do you need more colorschemes? Do you have an idea to improve this gem?  Please, feel free to fork the project on GitHub and send a pull request here.

Happy hacking!

video-outsorcing-local-time

Outsourcing Software Development in Local Time

How working with Nearsoft compares to offshore.

Send your resume!

Allowed formats are PDF, DOC and DOCX.