----------------------------------- DtY Sat May 30, 2009 4:28 pm 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? ----------------------------------- rdrake Sat May 30, 2009 4:36 pm 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. ----------------------------------- wtd Sat May 30, 2009 7:58 pm 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. ----------------------------------- DtY Sat May 30, 2009 7:58 pm 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) ----------------------------------- rdrake Sat May 30, 2009 10:51 pm Re: RE:Ruby as CGI Script ----------------------------------- If I did go with plain ruby on just CGI, would it generally run as fast as PHP?Likely not, especially with plain CGI. 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. 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. 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. ----------------------------------- gianni Sat Jun 13, 2009 6:32 pm RE:Ruby as CGI Script ----------------------------------- You may want to check out some cool little ruby frameworks like [url=http://www.sinatrarb.com/]sinatra. Which you could run with an Apache/Nginx + Passenger setup. Ruby will be more than fast enough for anything you want to do. ----------------------------------- rizzix Thu Jun 18, 2009 12:04 am RE:Ruby as CGI Script ----------------------------------- There are faster alternatives to Ruby: [url=http://jruby.codehaus.org/]JRuby It's proven to be faster; however I don't think you can use JRuby in a CGI environment. ----------------------------------- gianni Thu Jun 18, 2009 12:05 am Re: RE:Ruby as CGI Script ----------------------------------- There are faster alternative to Ruby: JRuby still has some compatibility issues too (e.g. gems, etc...). ----------------------------------- rdrake Thu Jun 18, 2009 9:37 am 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. ----------------------------------- DtY Thu Jun 18, 2009 9:54 am Re: RE:Ruby as CGI Script ----------------------------------- You may want to check out some cool little ruby frameworks like Sinatra's looking pretty cool, I think that's what I'll use. Thanks for the help everyone :) rdrake: Thanks, I probably wont set it up on apache for a while, but I'll post if I can't figure it out ----------------------------------- DtY Thu Jun 18, 2009 5:05 pm 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?) ----------------------------------- gianni Thu Jun 18, 2009 5:41 pm 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 ;). 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 [url=http://www.sinatrarb.com/intro.html]the docs. ----------------------------------- DtY Thu Jun 18, 2009 8:13 pm 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] ... ... ... [/code] ----------------------------------- gianni Thu Jun 18, 2009 9:31 pm 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: # 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 } ----------------------------------- DtY Thu Jun 18, 2009 9:57 pm 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 ----------------------------------- DtY Fri Jun 19, 2009 10:09 pm 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 :) ) 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. get '/protected/path' do #session ----------------------------------- rdrake Sat Jun 20, 2009 1:32 pm RE:Ruby as CGI Script ----------------------------------- There are a ton of Sinatra extensions. For example, there's the #somewhere in the murky depths of your sinatra app class User def peasant? self.permission_level == 0 end end ----------------------------------- DtY Sat Jun 20, 2009 3:28 pm 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. ----------------------------------- rdrake Sat Jun 20, 2009 9:47 pm 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 [url=http://github.com/search?q=Sinatra&x=0&y=0]GitHub. I have no idea why, but Ruby coders love Git.