Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a web app in Ruby without a framework [closed]

I want to build a web app in Ruby but I don't know if it's possible to do so without having to use a framework. I don't know why most Ruby developers use a framework like Rails or Sinatra.

How do I set up a Ruby web app that's not based on an existing framework?

like image 645
Sing Sandibar Avatar asked Apr 08 '13 09:04

Sing Sandibar


1 Answers

Is it possible to make a web app in ruby without using a framework?

Too long; Didn't read

Yes, it is definitely possible. Most of the Ruby frameworks are built with pure Ruby on top of other middleware libraries such as web server interfaces.

Ruby and the Web

Ruby is a general purpose language; therefore it is not specifically designed for web development. PHP, for example, is a language that was written to create web applications. In Ruby you are going to need some libraries to properly handle HTTP headers and web specific elements.

In Python, for example, (another generic programming language) we have a standard web server interface specification called WSGI (Web Server Gateway Interface). Every server that implements the WSGI specification is called WSGI-compatible. And any WSGI-compatible server can run the same WSGI Python script in the same way.

Why am I telling you this when speaking about Ruby? Because Ruby has a very similar concept to WSGI, with the possible exception that it is not yet a standard. Its name is Rack, and it provides an interface to deal with common low-level HTTP/Server stuff you don't want to deal yourself so that we can use Ruby as if we were using PHP.

Ruby, Rack and Apache

Let's take a real life example: Apache. Apache is one of the most common web servers out there. How does PHP work with Apache? With mod_php. How do Python WSGI-compatible applications works with Apache? With mod_wsgi. How do Ruby Rack-compatible applications work with Apache? With mod_rack. Can you see the pattern here? The web server needs to know how to properly link your application to the request/response web context.

Rack example

Without dealing furthermore in this abstract talk, let's focus on an example:

class HelloWorld
    def call(env)
        [200, {"Content-Type" => "text/plain"}, ["Hello world!"]]
    end
end

This example is provided by the Rack website and it explains how a Rack-compatible script runs:

  • You install rack on your web server (you'll find plenty of tutorials on Google specific to your web server)
  • You create a config.ru file in the root folder (.ru is mostly Ruby)
  • You paste that script
  • You run it with the run method

Et voilà, we have a web server interface. The env hash contains the environment variable from the current request and the array you are returning contains 3 components:

  1. The response status. 200 stands for "Everything is ok". 404 stands for "Not found". And so on.
  2. The HTTP header. These describe the response body. Its length. Its content. And so on.
  3. The response body. This contains the actual output of your application. HTML, JSON, XML, HXML, simple text... whatever.

In PHP for example, all this is done automagically. When you do echo "Hello"; the response status and headers are generated for you by the PHP interpreter.

A note on alternatives

You can dig in this field all you want but most of the technologies listed below are either deprecated or their use is highly discouraged by the community.

In the first years of the web there was a common interface used to run Perl, Python, Ruby, C scrips in the server side: CGI or Common Gateway Interface. This is an interface that can be used by pretty much any programming language out there, but it is generally considered slow.

Some thought to revive this interface by making it faster. And from that, guess what, arose FCGI, or FastCGI. This technology is used more often than you might think. Some PHP scripts are converted to FCGI scripts sometimes to make them run faster. I don't want to go further on this topic, as there are many other references out there.

And finally, you could create your own web server. You are actually not forced to create the web server with Ruby in order to use Ruby. Theoretically speaking, a web server is as simple as:

  1. Listen to a port (80 most of the time) until a request comes in
  2. Deal with the request
  3. Output the response
  4. goto 1

In the real life environment, you don't want to implement your web server yourself for production websites. So I definitely discourage that.

And if yes, why are frameworks chosen by most ruby web developers?

The pros

Frameworks serve the purpose of making your development fast. If you have deadlines, you don't want to deal with low level stuff and you'd love a framework build -blog command that manages as many boring things for you as you can and let you focus on the real application design.

Frameworks are usually open source and have big communities which helps in making the framework better quickly. You can easily understand that a bug in code seen by 10.000 pair of eyes is found 10.000 times faster than code you write for yourself.

The cons

Some frameworks might be too big for your needs and other might be too small. In the Ruby context there's the huge Rails and its little brother Sinatra. One is huge and the other is very small and really gets out of your way. Sometimes you want something in between.

Frameworks are usually very generic. This means that you have to configure things that seems obvious for your context.

Frameworks contain more code than you need. This is a fact that you can deduce by yourself. This usually means more complexity and directly more bugs (even though this is compensated for by the huge community around them).

like image 63
Shoe Avatar answered Sep 24 '22 02:09

Shoe