What is the differenence between ruby app.rb and rackup - ruby

What is the difference between running ruby app.rb and rackup? When running rackup, what script is actually running?

App.rb is Sinatra's front controller and can be call whatever you choose to call it
rackup is used to start rack based application servers it defaults to config.ru when no config file is supplied

Related

How does thin magically "take over" when included in the gemfile/required in the app?

I gem "thin" and require 'thin' are added to any (seems like) Rack app, it will automatically be chosen when the app is launched via rackup, instead of WEBrick.
How does rackup know to use thin? What if I use unicorn or Puma or something else?
Rack's source code states has three default servers in the following order: Thin, Puma and WEBrick.
Rack will attempt to 'mount' these three servers unless the PHP_FCGI_CHILDREN or RACK_HANDLER environment variables are set.
This behavior might change in the future, but for now only thin and puma will be recognized automatically when their gems are included.

Initializing an empty sinatra application

I am trying to create an empty Sinatra application with a config.ru file. Right now the only way I know how is to initialise it with cucumber but doing that creates extra files which I don't need including step definitions etc. When I run 'gem install Sinatra' in my working directory it says:
Successfully installed sinatra-1.4.5
Parsing documentation for sinatra-1.4.5
Done installing documentation for sinatra after 0 seconds
1 gem installed
Yet there is no config.ru file.
How do I initialise a Sinatra app so I have just the config.ru?
From the sinatra documentation
first create a file called app.rb in your working directory containing the following code:
require 'sinatra'
get '/' do
'Hello world!'
end
now create a file called config.ru with the following content:
require './app'
run Sinatra::Application
now run
rackup -p4567
to run the application

Command Line Arguments with Sinatra

I have a Sinatra program that I am creating, and I would like to be able to pass in command line arguments to this app when I launch it. The problem that I have is that I'm not sure how to do this. I've tried Trollop and looked at OptParser. Trollop doesn't appear to work with Sinatra because OptParser seems to be "default" parser with Sinatra. Is this true? If so, how can I customize the types of arguments accepted when I launch my app?
ruby app.rb hello
# app.rb
require 'sinatra'
get '/' do
ARGV[0]
end
Now when I visit localhost:4567 (where Thin hosts my sinatra app), I see a page that says hello.
Alternatively, you can use environment variables.
Example borrowed from here: https://gist.github.com/benlovell/351962
require 'rubygems'
require 'sinatra'
get '/' do
ENV['envvar']
end
Then run:
envvar=something ruby app.rb

Can I do Sinatra program without restart server?

When I modify the code and have to restart server to see results. Have any way out?
There are a few options, detailed in the Sinatra FAQ. The simplest appears to be to use shotgun, which you can install and invoke as follows:
$ sudo gem install shotgun
$ shotgun myapp.rb
or if you use define your app by inheriting from Sinatra::Base and use a config.ru file:
$ shotgun config.ru -p 4567
If you use Phusion Passenger, you can put this file in the application’s root folder
tmp/always_restart.txt
and it will restart on every request.
http://www.modrails.com/documentation/Users%20guide%20Apache.html ( section 8.7 )
Better way is to use reloader from sinatra-contrib gem (also from Sinatra FAQ): First install sinatra-contrib gem, then ensure your application .rb file starts with these lines:
require 'sinatra'
require 'sinatra/reloader' if development?
And then any modified config files will be reloaded (no need to restart server!)

How to get Sinatra to auto-reload the file after each change?

I am using
# my_app.rb
load 'index.rb'
and start the sever like this
ruby my_app.rb
but it never reload any changes I made in index page.
Did I miss anything here?
See the Sinatra FAQ,
"How do I make my Sinatra app reload on changes?"
First off, in-process code reloading in Ruby is hard and having a
solution that works for every scenario is technically impossible.
Which is why we recommend you to do out-of-process reloading.
First you need to install rerun if you haven’t already:
$ gem install rerun
Now if you start your Sinatra app like this:
$ ruby app.rb
All you have to do for reloading is instead do this:
$ rerun 'ruby app.rb'
If you are for instance using rackup, instead do
the following:
$ rerun 'rackup'
You get the idea.
If you still want in-process reloading, check out Sinatra::Reloader.
gem install sinatra-reloader
require 'sinatra'
require 'sinatra/reloader'
Note: it will reload only sinatra handlers (and, maybe some sinatra server configuration commands), but not custom files, which you have to reload manually.
UPD after 9 years: seems like it is already possible to reload other files using also_reload, dont_reload and after_reload -- https://github.com/sinatra/sinatra/pull/1150
You can use the rerun gem.
gem install rerun
rerun 'ruby app.rb'
OR if you are using rackup
rerun 'rackup'
gem install sinatra-reloader
require 'sinatra/base'
require "sinatra/reloader"
class MyApp < Sinatra::Base
register Sinatra::Reloader
get '/' do
"Hello Testing1!"
end
end
You may want to set environment variable to development and conditionally load the gem.
When you run the application with Passenger Standalone, just create a tmp/always_restart file:
$ touch tmp/always_restart.txt
See Passenger documentation for more info.
I like the Shotgun gem. If you're using a modular Sinatra app and have a config.ru file it's easy to run.
shotgun config.ru
Check the gem out here. It's fairly straight forward and no configuration needed.
On Windows, I am using my restart gem for this:
restart ruby my_app.rb
or, with rackup:
restart rackup
See here for more info, hope you find it useful.
You could use guard-rack. Lifted from an article at dblock.org:
Add this to your Gemfile:
group :development do
gem "guard"
gem "guard-bundler"
gem "guard-rack"
end
Then, create a Guardfile at the root of your project with this content:
guard 'bundler' do
watch('Gemfile')
end
guard 'rack' do
watch('Gemfile.lock')
watch(%r{^(config|app|api)/.*})
end
Lastly, run Guard, like so: bundle exec guard, and rackup will reload every time.
If you only change your templates sinatra will always rerender them if you set your environment to development:
ruby app.rb -e development

Resources