How do I run a Sinatra application on G-WAN? - ruby

I'm trying to write a Ruby web application and I want to use the functionality offered by Sinatra.
I used this code:
require 'rubygems'
require 'sinatra'
get '/' do
'Hello, world!'
end
I typed localhost:8080/?hello.rb in my browser and I received an error message that the script is unable to find sinatra.
I also tried to run the sinatra app using this command:
ruby hello.rb
and this time it worked!
So I suspect the problem is not in my code, but in the way I'm using G-WAN to serve the Ruby script.

Sinatra expects to be run by a rack-compatible server, which G-Wan is apparently not. When you run script manually it is being run by a WEBrick server that comes bundled with Ruby. Examples of compatible servers are thin, unicorn or apache/nginx with phusion passenger

Ruby (like Java or PHP) probably uses configuration files and/or environment variables to specify where to find packages, modules and libraries.
While G-WAN received similar requests for asm, C, C++, D, Objective-C, Java and C# support, Ruby users did not manifest interest in this area - so far.
If you point to us the relevant Ruby documentation, we will try to add this Ruby feature (just like it was done for the other languages).

Related

How does Pow/Phusion Passenger/Webrick work?

I come from a PHP and .NET world where I understand the environment fairly well. However I can't find a newbie explanation of how the Ruby / Ruby on Rails stack actually work with these web server.
Are they more close to the PHP model, where all classes of an application are loader for each request and there is no default shared memory, or is it like an application server where an active app sits in the memory and handles requests?
How is it with reloading when a file changes? Does the app in an application server has to be restarted? How does it know? Does it monitor file system?
I have seen that both the Ruby Version Manager (rvm) and the newer rbenv from 37signals shomewhat shuffles with the ruby command on OS X / Linux. This seems like a total magic to me. Does a webserver just runs ruby command and does not care where the interpreter is resolved in the $PATH?
Webrick is the default server of Rails and is usually used for developing and testing.
Rails is session based like PHP is. If you want to run in production you will generally use Phusion Passenger on Apache or Nginx, dont worry about that for now.
If you run in Development or Test environment you can edit your application files(views, controllers and models) and they will be reloaded on each request (even if they are not edited).
Have a look at generating a project and scafolds with Rails to get you started.
http://guides.rubyonrails.org/getting_started.html

Ruby Frameworks - Request Entry Point

I'm learning ruby and looking at his frameworks. One things that i can't understand is how frameworks handles requests in ruby world. Digging deeper I found that there's a middleware called Rack that does the job.
So my questions are:
How does Rack handles HTTP request?
Coming from PHP where there's the famous "index.php" file as a entry-point, which is the corrispective in the ruby world?
Thanks in advance.
From the rack specification:
A Rack application is an Ruby object (not a class) that responds to call. It takes exactly one argument, the environment and returns an Array of exactly three values: The status, the headers, and the body.
In pratice, the common way that a rack application is started is defined in a config.ru file. If you look in the base directory of a rails app you will see it. In rails, it includes config/environment.rb, which includes config/application.rb, which includes boot.rb, which includes gems and whatnot. From that point the framework starts to do its thing.
The general idea with rails is that a dispatcher takes any request and decides what needs to be done with it. The dispatcher can be seen as an equivalent to the index.php you mentioned.
How the config.ru file gets accessed (or how the rack app is started) is dependent on the way your application is deployed. Phusion Passenger, a popular module for apache and nginx, will look for config.ru in the root directory of any application you have added to the server config file.
It really comes down to the deployment option. Ruby apps can be run via apache/nginx modules, directly via web servers written in ruby, and via CGI.
Here is a description of the different ways web apps are deployed, from the passenger docs

Apache, Ruby, no framework

What is the best way to run Ruby scripts using Apache and no framework (just like PHP scripts)?
Bonus points if the script can access the HTTP request stuff (URL, host, GET/POST params and cookie). If a framework is absolutely required, which is the most lightweight which would use Apache?
Use Apache with mod_rack and the Rack interface to communicate with the server.
Try Sinatra and you can write something like this:
require 'sinatra'
get '/hi' do
"Hello World!"
end
It is actually a framework, but the smallest application on Sinatra is MUCH smaller than smallest application on Rails.

Ruby (off the Rails) Hosting

Many people have asked about Rails hosting on this site, but I'm not familiar enough with the back end of things to know if there's a difference.
I want to host some Ruby CGI 'webservices', basically just ruby methods that take parameters from a POST request, access a MySQL db and return data.
I've looked at RoR and it seems like overkill for this, from what I can tell it's for speeding up the development of data baesd CRUD sites, which is not at all what I'm doing.
So my question is, does this affect the hosting provider I choose? Does anyone recommend a good Ruby host for CGI operations? I'm not familiar with FastCGI, mod_ruby, Passenger, Mongrel etc. and what they mean for performance, scalability etc. I just want to host my ruby scripts with reasonably good performance, and all the info out there(and here) seems to be focused on rails.
First, if you want lightweight, Sinatra is usually my first pick. Pair it up with rack and Passenger for best results. It's not CGI, but realistically speaking, CGI is rarely a good match-up with Ruby.
Here's the "Hello World!" Sinatra app from the main page:
require 'rubygems'
require 'sinatra'
get '/hi' do
"Hello World!"
end
Hard to get more lightweight than that.
As for providers, anybody that supports Passenger (mod_rack) should be able to handle Sinatra. I'm a big fan of Slicehost personally, but they're a VPS host, which means you need to be able to install and manage the entire stack yourself. If you don't mind paying a tiny bit extra for the infrastructure, Heroku makes installation and deployment dead simple, so long as your needs don't exceed what they provide (sounds like they won't). In the unlikely event that you're only using 5MB or if you're using an external storage mechanism like Amazon RDS, Heroku may actually be free for you.
Update:
Passenger is an Apache module that allows Rack applications to be run inside of Apache.
Rack is a middleware layer that separates the web server and the web framework from each other. This allows web frameworks to run on any web server for which there is an adapter.
Sinatra is a lightweight web framework that runs on top of Rack.
Once Passenger and Rack are installed (gem install rack, gem install passenger) you just need to edit the Apache vhost to point at the config.ru file for your Sinatra app and create the required directories as per the Passenger docs and you'll be good to go.
I think you might want to look into Rack. It allows you to do the kinds of things you're talking about and shrugs off the weight of frameworks like Rails or Merb. Rack applications can be hosted at a place like Heroku.

JRuby / Rack deployment

I know this is pretty exotic, but I want to deploy a Ruby web application (not Rails, but Rack based, so it plugs into most Ruby servers just fine) using JRuby. Google and friends give me a few success stories, but mostly rails related and if not, no details on the deployment are provided. The framework I'm using is Ramaze, if it matters.
Any help would be greatly appreciated.
In my opinion, running a Rack based application with a rackup script is the real Ruby way. And I wanted to apply the same for JRuby too. That is why I've written jetty-rackup http://github.com/geekq/jetty-rackup
We are using it for deploying a Sinatra web application. No Java specific configuration needed. A typical, small config.ru is enough. Embedded jetty web server is used in place of Webrick then.
This is the "just works" gem for me: https://github.com/matadon/mizuno

Resources