CGI Programming + Ruby - ruby

Is there a framework for Ruby for CGI that provides similar functionality as Ruby on Rails (mvc)?
Also, The server where the app shall be used on does not support FCGI, only plain old CGI.

Ruby comes with a CGI module, but it isn't a MVC at all. It makes it easy to extract parameters from a HTTP request passed to the app, encode and decode the query params, etc. It relies on a web server to handle routing the request to the right page, so there's quite a gap between a MVC and a CGI.
There are alternate MVCs for Ruby. Sinatra is very easy to use, and Padrino is built on Sinatra, putting it between Sinatra and Rails. I like using Sinatra at work because it's good for fast prototyping and in-house loads are nowhere close to what we'd get on an internet facing app.
As far as the server not supporting FCGI, a MVC doesn't really care. Put its server on a different port, then reference that port when you want something to talk to Sinatra. For instance, if you tell Sinatra to use 8088, your URLs for Sinatra served pages would be something like: http://host.com:8808/url/path/to/object. Load your Sinatra based app on the web server and start it up. It'll run concurrently with the normal web server.

Related

Front end separated from back end project using Sinatra

I am planning to create a website that has front end and back end. I was wondering how the FE communicates with the BE.
I saw a project that uses Xampp to run the front end and sinatra for the back end. It needs to start apache, then the backend is fired using the rackup command. I assume the backend runs under Webrick.
Can someone explain how these two ends communicate with each other? If there is a good tutorial for this, I will appreciate it.
Sinatra is a popular option for API-only backend applications. We use Grape with Sinatra or just Sinatra without any dependency.
I have two app examples that can help you:
https://github.com/katgironpe/sinatra-grape
https://github.com/katgironpe/simple-sinatra-mvc
Webrick or Puma can be used with Sinatra, but it's not impossible to run a Ruby app on XAMPP. I did that several years ago. The front-end is probably just consuming the Sinatra API.
You can use Ember CLI project. It can get complex with other options like Angular.js and React.js. Or if you like, just use jQuery.

Can I create a simple website to accept input and display, without Rails, database or external web server?

I am trying to see if I can create a simple website, like a blog, using only Ruby. No Rails or a database or outside web servers. I plan to store the data in a file for persistence.
I wanted to use TCPServer, CGI, and Net::HTTP.
Is there an easier way I can use?
There are a lot of moving parts when designing a website.
Depending on the purpose of the exercise, you might want to consider using a very simple web framework like Camping, Sinatra, or Ramaze. This is probably the best solution if you're trying to get a top level understanding of web programming because it only has exactly what you need (Camping is less than 4k!) and handles stuff like routing.
Building a web server is more an exercise in HTTP parsing. You might want to omit the framework and try to build something on top of Rake (an API for lots of popular web servers) and a simple web server like Webrick or Thin.
You could also try Espresso
It is easy to learn and fast to run.
And offers all the liberty you need for creation process.
Also it has no hidden "fees", everything is transparent.

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

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.

Ruby: client-side or server-side?

Is Ruby a client- or server-side language?
Both?
After all, there are Ruby programs which are not used as part of a client-server architecture.
If you are talking about Ruby on Rails, then it's typically only used on the server side.
Ruby is an all-purpose script/programming language which can be executed on both client and server environments.
As client-side, you can use it to create a GUI application (or CLI one) to interact with data, communicate with a server, play with media/game, etc. Some framework examples on this level would beShoes, MacRuby, etc.
As server-side, you can use it to store and save data, validate and execute transactions, etc. It's where frameworks like Rails, Merb, Sinatra and others take place, and its -arguably- it's most known mode of operation.
As the previous poster said, on the context of a server/client web application arquitecture, Ruby would be run on the server side. If I'm not mistaken, there have been some advances for running Ruby through the browser (like JS does), but probably not something to be considered for production ready needs.
Ruby does not (typically) execute in the browser, so if you are asking this in the context of a web server/client browser, then Ruby is server-side.
You can of course also execute stand-alone Ruby code on any machine with a Ruby interpreter. It is not confined to web applications.

Resources