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.
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,
# Gemfile gem 'rack-showme'
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.
# config/enviroments/staging.rb ... config.middleware.use "Rack::Showme" Rack::Showme::Options.message = "Staging"
Don’t forget to also create an staging section in your database.yml file
# config/database.yml staging: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000
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.
RAILS_ENV=staging rails s
And this is what it will looks like,
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,
# config/enviroments/staging.rb ... config.middleware.use "Rack::Showme" Rack::Showme::Options.message = "Staging" Rack::Showme::Options.colorscheme= "red" # Other options are: yellow and green
This is what it looks like with the red colorscheme,
And this is with the green colorscheme,
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.