Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGI language choice

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.

like image 804
LoveMeSomeCode Avatar asked Jun 09 '26 14:06

LoveMeSomeCode


2 Answers

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.

like image 89
johannes Avatar answered Jun 12 '26 02:06

johannes


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.

like image 41
cllpse Avatar answered Jun 12 '26 02:06

cllpse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!