Computer Science Canada

Ruby as CGI Script

Author:  DtY [ Sat May 30, 2009 4:28 pm ]
Post subject:  Ruby as CGI Script

Is the speed decent? I've heard that Ruby is somewhat slow compared to other high level languages, and cgi causes problems because it needs to start a new process for each page request, but will it run at decent speeds for small sites, comparable to PHP?
Would CGI be better than SHTML with separate scripts generating each part of the page that need to be dynamic?

Author:  rdrake [ Sat May 30, 2009 4:36 pm ]
Post subject:  Re: Ruby as CGI Script

There's no getting around it, Ruby is ass slow compared to a lot of things out there. You can, however, use things like caching in order to improve performance by not rendering the page on every request.

You can...

  • Write CGI scripts in Ruby, it has built-in support for that.
  • Embed Ruby snippets in regular HTML, check out ERB.
  • Use FastCGI to host something like Mongrel so it doesn't need to spawn a new process every time.


Hopefully that gets you started.

Author:  wtd [ Sat May 30, 2009 7:58 pm ]
Post subject:  RE:Ruby as CGI Script

Also, keep in mind your actual speed requirements, and that you may well be able to develop your CGI app significantly faster in Ruby than in another language.

For small sites, check out framework-ish stuff like Camping.

Author:  DtY [ Sat May 30, 2009 7:58 pm ]
Post subject:  RE:Ruby as CGI Script

If I did go with plain ruby on just CGI, would it generally run as fast as PHP? People complain about php being too slow, however it's still used by virtually everyone on the internet, so it can't be a big deal.
I checked out fastCGI, but I didn't understand what it was doing differently than normal cgi, will it run the same cgi scripts, or do they need to be modified?

[edit] I'm checking out camping, that looks interesting, I haven't looked at all, but is hosting for camping easy to find? On the same note, is hosting that provides CGI and Ruby easily available (without renting/buying a virtual computer)

Author:  rdrake [ Sat May 30, 2009 10:51 pm ]
Post subject:  Re: RE:Ruby as CGI Script

DtY @ Sat May 30, 2009 7:58 pm wrote:
If I did go with plain ruby on just CGI, would it generally run as fast as PHP?
Likely not, especially with plain CGI.

DtY @ Sat May 30, 2009 7:58 pm wrote:
People complain about php being too slow, however it's still used by virtually everyone on the internet, so it can't be a big deal.
It's easy to use, that's why it's popular. The fact that it can easily be hosted off of every free OS known to mankind also means it's cheap for hosting companies to provide hosting for it. It's everywhere, it's easy, and it's dirty - three things people love.

DtY @ Sat May 30, 2009 7:58 pm wrote:
I checked out fastCGI, but I didn't understand what it was doing differently than normal cgi, will it run the same cgi scripts, or do they need to be modified?
CGI spawns a new process for every request, FastCGI does not. It keeps a pool of processes which do the work, bypassing much of the overhead regular CGI has.

DtY @ Sat May 30, 2009 7:58 pm wrote:
is hosting that provides CGI and Ruby easily available (without renting/buying a virtual computer)
A lot of hosts now support Ruby. Look for hosting for Ruby on Rails.

Author:  gianni [ Sat Jun 13, 2009 6:32 pm ]
Post subject:  RE:Ruby as CGI Script

You may want to check out some cool little ruby frameworks like sinatra. Which you could run with an Apache/Nginx + Passenger setup. Ruby will be more than fast enough for anything you want to do.

Author:  rizzix [ Thu Jun 18, 2009 12:04 am ]
Post subject:  RE:Ruby as CGI Script

There are faster alternatives to Ruby: JRuby

It's proven to be faster; however I don't think you can use JRuby in a CGI environment.

Author:  gianni [ Thu Jun 18, 2009 12:05 am ]
Post subject:  Re: RE:Ruby as CGI Script

rizzix @ Thu Jun 18, 2009 12:04 am wrote:
There are faster alternative to Ruby: JRuby

It's proven to be faster; however I don't think you can use JRuby in a CGI envorinment.


JRuby still has some compatibility issues too (e.g. gems, etc...).

Author:  rdrake [ Thu Jun 18, 2009 9:37 am ]
Post subject:  RE:Ruby as CGI Script

If you decide to use a Rack-compatible framework (Rails, Sinatra, etc.), you can also serve your scripts through Apache using mod_passenger.

If you need help setting it up just post back here.

Author:  DtY [ Thu Jun 18, 2009 9:54 am ]
Post subject:  Re: RE:Ruby as CGI Script

gianni @ Sat Jun 13, 2009 6:32 pm wrote:
You may want to check out some cool little ruby frameworks like sinatra. Which you could run with an Apache/Nginx + Passenger setup. Ruby will be more than fast enough for anything you want to do.

Sinatra's looking pretty cool, I think that's what I'll use.
Thanks for the help everyone Smile

rdrake: Thanks, I probably wont set it up on apache for a while, but I'll post if I can't figure it out

Author:  DtY [ Thu Jun 18, 2009 5:05 pm ]
Post subject:  RE:Ruby as CGI Script

Okay, I've got some more questions now (though not specifically concerning Sinatra)

1) Is it safe to use Mysql? Do I create a new connection object in each handler?
I know there are libraries that handle databases for you, but I don't like those, it's extra stuff to learn when I already know sql much better than I'd ever know a database wrapper.

2) Can I make ERB templates include other files? Like for a standard header and footer I want on every page. (I'm used to PHP, so maybe there's a completely better way?)

Author:  gianni [ Thu Jun 18, 2009 5:41 pm ]
Post subject:  RE:Ruby as CGI Script

I highly recommend using an ORM such as DataMapper to interface with your database. It will save you tons of time in the future (you have to resist your PHP temptations Wink.

A better solution would be to do it in reverse. Have your layout file (with header and footer) and render your template in the middle. That prevents you from having to include the header and footer in each action. See the section "Named Templates" in the docs.

Author:  DtY [ Thu Jun 18, 2009 8:13 pm ]
Post subject:  RE:Ruby as CGI Script

When I learned TurboGears (python framework) a few years ago, SQLObject looked like a good idea, but I never had any idea what I needed to do to get the stuff I need, whereas I knew exactly how to do it with an SQL Query. (I do know the security risks of a back door into an SQL query, but I do know how to prevent it (prepared expressions))

So do you mean something like this? (template contains the filename of the template to use):
code:
<!DOCTYPE...>
<html>
<head>
...
</head>
<body>
...
<% code to render template %>
...
</body>
</html>

Author:  gianni [ Thu Jun 18, 2009 9:31 pm ]
Post subject:  RE:Ruby as CGI Script

Yea, it explains how to do it in the "Named Templates" section of the docs.

The cool thing about ORM's, is that you don't need to even think about databases, SQL, or other such things. You are basically working with ruby objects that can be persisted. So it's much more natural to interact with these objects. You'll also see the magic once you start defining relationships between these objects. For example, without even thinking about sql, you could do something like the following:

Ruby:
# You have a user object loaded from your database
user

# You want to find all the blog posts for the given user
user.posts

# Now you want to find the total number of comments on all of the user's posts
user.posts.inject(0){|m, p| p.comments.count + m }

Author:  DtY [ Thu Jun 18, 2009 9:57 pm ]
Post subject:  RE:Ruby as CGI Script

Ah, I see, I'll see if I can do something similar with erb (I'm not liking haml)

Okay, I'll look for a DataMappre tutorial

Author:  DtY [ Fri Jun 19, 2009 10:09 pm ]
Post subject:  RE:Ruby as CGI Script

Okay, I've run into another problem. (I decided to go with DataMapper btw, thanks for talking me into not using MySQL Smile )

I have some Sinatra... things (Callbacks, hooks, or something?) and I want some to be protected, so that the user logged in needs to be at least a certain rank to view the page. If they aren't, redirect to he home page with a message.

I could do something like this for each, but I was wondering if there was some way to simplify it, make it DRYer.
Ruby:
get '/protected/path' do
    #session[:uid] is the user's id, nil means not logged in
    if (session[:uid] == nil) or (User[session[:uid]].level < 3) #User needs to be at least level three to get here
        session[:flash] = 'You do not have permission to view this'
        redirect '/'
    end
    #If we get here, the user is allowed. Continue on with page rendering, whatever.
end

Author:  rdrake [ Sat Jun 20, 2009 1:32 pm ]
Post subject:  RE:Ruby as CGI Script

There are a ton of Sinatra extensions. For example, there's the Maxjustus's Sinatra Authentication extension.

Documentation is near the bottom of the page.

Basically it looks as though you specify user levels, and you can add methods to the base User class in order to perform checks to see if the user is above a certain level.

Their example:
Ruby:
#somewhere in the murky depths of your sinatra app
class User
  def peasant?
    self.permission_level == 0
  end
end

Author:  DtY [ Sat Jun 20, 2009 3:28 pm ]
Post subject:  RE:Ruby as CGI Script

Ah, looks like I might be reworking all my current authentication stuff, since that seems to do everything I've done so far.

Thanks again.

Author:  rdrake [ Sat Jun 20, 2009 9:47 pm ]
Post subject:  RE:Ruby as CGI Script

If you're ever looking for an extension to do something for Sinatra and Google isn't helping out, search GitHub. I have no idea why, but Ruby coders love Git.


: