views:

188

answers:

6

Ok, I've asked a few related questions here and only ended up with more questions and I realized now it's because I don't have enough background info. So I'll make it more generic:

I need to make a simple web application. Static HTML/JQuery pages will send AJAX POST requests to some server side code, which will:

  • Read the POST variables passed in
  • Run some very simple logic
  • Hit a MySQL database for simple CRUD ops
  • Return a plain string of data to be consumed by the javascript on the page

I was assuming Ruby was a good choice for this as everyone is raving about how well it's designed, and I've been playing with it - not RoR, just Ruby for simple scripting tasks - and I kind of like it.

My question is, I'm hopelessly confused by the trillion helper libraries and frameworks out there. I don't know what these are and therefore if I need any/all of them: Rack, Sinatra, Camping, mod_ruby, FastCGI, etc.

Would it be easier to just learn PHP and us that? Or can I get away with just dropping my .rb files into the cgi-bin folder(I'm using Apache for hosting) and use the ruby cgi library to get my variables?

EDIT: As far as Rails, I'm just assuming that it's overkill for what I want but I might be wrong. I looked at it, and it seemed cool for generating data based web sites quickly, but that's not what I'm trying to do. I don't want any forms pages for the user. I don't want them entering data or viewing records. I don't even want to return any HTML. I just want a ruby script to sit on the server, get passed a few variables in a post request, and return a JSON string in response. I will need some basic cookie/session/state managment

This is a really easy thing to do in C# and ASP.NET with webservices, but it seems very confusing with the open source technologies.

+1  A: 

Sinatra is very simple to learn and use. It's also easy to deploy with the use of Phusion Passenger (which is like mod_php for ruby frameworks like Rails and Sinatra). Instructions here: http://blog.squarefour.net/2009/03/06/deploying-sinatra-on-passenger/

If you find that you need more than what Sinatra will give you, I recommend Rails. Setting up that with Passenger is even easier since hardly any configuration is required. (see modrails.com).

insane.dreamer
Ok, but I still don't understand. What is it that Sinatra and Passenger are giving me? I drop the ruby file into the cgi-bin on apache and it runs when i hit it from another webpage. What exactly are Rack/Passenger/Sinatra doing for me?
LoveMeSomeCode
A: 

I am curious about what I perceive to be your resistance to trying Rails. You say that you want "to spend more time on the scripting itself and less on configuration", and yet you seem to dismiss Rails out of hand. Rails is all about convention over configuration. If you take the time to learn how Rails does things, you can get an incredible amount of functionality "for free" just by following the conventions of the framework.

If you want to make a simple web app, Rails is really a very painless and good way to start. You can use a sqlite database and not even mess with MySQL (won't scale, but for learning or simple apps it's fine). It's true that there are simpler frameworks, but since you seem new to web programming, I'd recommend that you start with what will get you the most support in terms of documentation and knowledgeable folks. Follow the old adage: Get it working first, then optimize later.

The only sticking point I can see is the Apache integration... The consensus on Rails deployment these days seems to be focused on using lightweight httpds in place of Apache. There is a mod_fcgid which seems to be the best way to do it with Apache (mod_ruby is deprecated, buggy and slow, last I read) if you can do custom mods. Or there's Phusion Passenger, which seems to be the latest and greatest way to do it. Running Rails in a standard CGI environment will yield awful performance (but that goes for any CGI framework, really) due to the overhead of executing the interpreter + framework for every request. You'll get much better performance if you go with something that keeps the interpreter + framework in memory.

mjmac
I don't think mod_ruby is slow, it just isn't for building web applications. mod_ruby is for extending Apache with Ruby, this is true of all mod_* though. They're typically faster because of their architecture, but they blur concerns in a really bad fashion.
Evan Carroll
+1  A: 

PHP is very easy to use because it's designed specifically for this sort of thing. Want to read POST variables? They're in $_POST. Want to query MySQL? mysql_query("SELECT \something\ FROM \table\");. And if you ever need help, Google searches for "php what_you_need_to_do" almost always return results on php.net, which is very helpful.

And for what you're doing, you don't need any additional frameworks.

Brendan Long
yeah, i like the Ruby syntax, but it doesn't seem as well organized as PHP. Is session mgt. easy to do in PHP?
LoveMeSomeCode
PHP is really a poor language for everything. Ruby is much better organized than PHP, however logically Ruby's session management will be more difficult because sessions are really only for emulating state in REST... Ruby has other uses. Ruby is a much more rich language, PHP is like a phonebook of 1400 names with a grammar equally simple. ... PS, if you're using the "simplicity" of mysql_query don't forget to factor in the added complexity of explicitly having to use mysql_real_escape_string for all user input.
Evan Carroll
I'd agree that pretty much anything is a "better" language than PHP in general, but if you're going to make a page as simple as you're describing, any other language is overkill.The most important thing though is that PHP has incredibly good documentation.
Brendan Long
Oh and sessions are simple as calling session_start(); then accessing the $_SESSION variable ($_SESSION['x'] = 'some string'; or $x = $_SESSION['x']).
Brendan Long
+2  A: 

Use jQuery and PHP.

Both technologies are well documented, and you should be able to get an application up and running in a matter of hours. You sound like you know a thing or two - talking about CRUD operations and so on - so I won't bore you with examples. And as far as JSON goes, there are probably a million PHP libraries out there, for outputting JSON objects.

roosteronacid
A: 

I personally like Django. I had a problem with Ruby on Rails where I just got overwhelmed by everything when I just wanted to do something simple, which it sounds like you want to do (since you said ROR feels like overkill). The cool thing I found with Django is that if you WANT everything, then you can get everything by plugging it in...but if you want less then you just don't plug in that technology and it's that much more lightweight.

Take for example "views". Django, like ROR, uses MVC. But if you just want to return a string of data and don't need the view, then you don't need to plug in the view. But if later on you decide that it will be more organized in a view then you can easily plug it in at that time.

Here's their website: http://www.djangoproject.com/

Adam
BTW, I'd stay away from PHP because whenever I've dealt with it in the past it became very hard to keep organized as the project got larger. But you may be more successful with it.
Adam
+2  A: 

You don't want to use any feature from a fully blown framework so don't use one. Less code = less bugs = less security nightmares.

CGI

CGI has some performance drawbacks in comparison to other methods, but is still (in my opinion) the simplest and easiest to use one. This is how you use the builtin cgi library:

require "cgi"
cgi= CGI.new

answer= evaluate(cgi.params)

cgi.out do
    answer
end

rack

Another low tech easy to use variant would be rack. Rack is an abstraction layer which works for many webserver interfaces (cgi, fastcgi, webrick, …). It's simplicity can be compared to the one of only using cgi. Put the following into a file wich ends with .ru into your cgi directory.

#!/usr/bin/rackup
require "rack/request"

run (lambda do |env|
  request= Rack::Request(env)

  anwser= evaluate(request.params)

  return [200, {}, answer]
end)

This does not seem very different from cgi, but it gives you much more possibilities. If youst execute this file on your local machine rackup will start the webrick webserver. This webserver will deliver the webpages you described in your .ru file.

Other interfaces

fast-cgi

fast-cgi works almost like CGI. The difference is, in CGI your script get's started for every request it has to work on. With fast-cgi, your script only starts once for all requests. There is a library available to write fast-cgi script in ruby.

mod_ruby

mod_ruby is a builtin ruby interpreter for apache. It works analog to mod_php in apache.

mongrel

mongrel is a standalone webserver for ruby applications. This is a simple hello world example with it.

require 'mongrel'

class SimpleHandler < Mongrel::HttpHandler
   def process(request, response)
     response.start(200) do |head,out|
       head["Content-Type"] = "text/plain"
       out.write("hello world!\n")
     end
   end
end

h = Mongrel::HttpServer.new("0.0.0.0", "3000")
h.register("/hello", SimpleHandler.new)
h.run.join

Mongrel is often used for rails and other ruby frameworks. Most people use an apache or something else on port 80. This webserver than distributes the requests to several mongrel servers running on other ports. I think this is totaly overkill for your needs.

phusion passenger

passenger is also called mod_rails or mod_rack. It is a module for apache and nginx to host rails and rack applications. According to their websites rails with passenger uses 1/3 less ram than rails alone. If you write your software for rack, you could make it a little faster by using passenger, instead of cgi or fast-cgi.

johannes
thanks, it looks like im going the CGI route. Can you maybe expand on the difference between fcgi/passenger/mongrel? my app is very basic and just runs a few dozen lines of code, performs a few mysql crud statements and spits back some json strings. how should i host it?
LoveMeSomeCode
wow thanks for all the detail. as much as I love the whole LAMP idea, there really doesnt seem to be a good resource for detailing and comparing all these related technologies. The book I got on ruby only mentions Webrick. I think i'll go the Fast-CGI route for now because of the simplicity of setup. thanks again!
LoveMeSomeCode