marți, 21 mai 2013

On-the-fly reloading for Sinatra


Unlike Rails, Sinatra does not provide out of the box an option to reload the files on-the-fly (an option more than useful for the developer tinkering with the pages/files many many times before the final version).

The typical answer to this is "use Shotgun". That's all nice, but... not if you are (like I am) a happy user of a Windows box... Shotgun doesn't fly on these boxes, and in order to make it run one ought to fiddle quite a lot in Shotgun's source files.

The solution for us, Bill's loyalists, is Sinatra reloader. Initially an independent gem, it is now part of a mix of gems grouped under the title of "sinatra-contrib".

Knowing this, the steps to install/use the reloader are pretty simple:

1. gem install sinatra-contrib (in the command line)

2. require 'sinatra/reloader' (in the main app .rb file)

3. Simply run ruby filename.rb (forget about shotgun, you can simply uninstall it)

... and it will work like a charm, on the standard Sinatra port (4567), except now it will update when you modify the files, without restarting Sinatra...

As a side note, Sinatra reloader tends to work much better with thin (rather than WEBrick) as web server. Generally speaking, Sinatra was designed to run with thin... 

When gem install thin - then Sinatra will automatically start using it as web server. Now, of course, installing thin with windows raises other kind of headaches (it needs the development kit installed on the machine), but that's another story/post :-)

Niciun comentariu:

Trimiteți un comentariu