<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog - ShiftEleven &#187; cgi</title>
	<atom:link href="http://shifteleven.com/articles/tag/cgi/feed" rel="self" type="application/rss+xml" />
	<link>http://shifteleven.com</link>
	<description></description>
	<lastBuildDate>Mon, 09 Jan 2012 04:04:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Forget About Little Old CGI</title>
		<link>http://shifteleven.com/articles/2008/03/17/forget-about-little-old-cgi?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=forget-about-little-old-cgi</link>
		<comments>http://shifteleven.com/articles/2008/03/17/forget-about-little-old-cgi#comments</comments>
		<pubDate>Tue, 18 Mar 2008 00:53:31 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Ruby off Rails]]></category>
		<category><![CDATA[cgi]]></category>
		<category><![CDATA[rack]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://shifteleven.com/?p=41</guid>
		<description><![CDATA[Maybe someone may have told you to use ruby and CGI together, but I think that person has a screw loose. I mean, come on. Everyone knows that what you really need to use is Rack Why Rack Well for &#8230; <a href="http://shifteleven.com/articles/2008/03/17/forget-about-little-old-cgi">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Maybe someone may have told you to use <a href="http://shifteleven.com/articles/2008/03/13/dont-forget-about-little-old-cgi">ruby and CGI</a> together, but I think that person has a screw loose.  I mean, come on.  Everyone knows that what you really need to use is <a href="http://rack.rubyforge.org/">Rack</a></p>
<p><span id="more-41"></span></p>
<h3>Why Rack</h3>
<p>Well for one, it&#8217;s light weight.  So if you want to deploy your ruby script using CGI, you can do that with Rack.</p>
<p>But let&#8217;s say, you want to your script to Mongrel, or WEBRick, or FCGI; you can do that too, and with very few changes to your script.  That&#8217;s because Rack abstracts all the boilerplate code you would need to work with those servers and bundles it up into a nice interface for you to use.</p>
<p>Oh, and the code to use Rack is very easy.  Here&#8217;s an example of a Rack app for you:</p>
<h3>Examples</h3>
<pre class="ruby" title="code">%w(rubygems rack haml).each { |dep| require dep }

class IndexPage
  def call(env)
    haml_engine = Haml::Engine.new(File.read('../templates/index.haml'))
    [
      200,
      { "Content-Type" =&gt; "text/html" },
      haml_engine.render(Object.new, {})
    ]
  end
end

Rack::Handler::CGI.run(IndexPage.new)</pre>
<p>The secret behind Rack is that it will run any ruby object that has a <code>call</code> method and pass in the environment variables to it.  In that environment variable, you will find things like HTTP_HOST, or QUERY_STRING, and the like.</p>
<p>If you wanted to have this script now work with WEBRick, no problem.</p>
<pre class="ruby" title="code"># Same code as above, with just a little tweak to the last line

Rack::Handler::WEBRick.run(IndexPage.new, :Port =&gt; 3000)</pre>
<p>Tada!  So now you can run a test server without worrying about setting up Apache or Lighttpd.</p>
<h3>More Goodness</h3>
<p>To make your life easier, Rack also comes with a set of middleware for you to use.  Say you have an error in your application.  Perhaps you would like to see that error formatted nicely in your browser.  You can enable <code>Rack::ShowExceptions</code>.  Perhaps you would like the code you have to be reloaded from the server whenever it changes, like when you&#8217;re developing your application, then you can use <code>Rack::Reloader</code>.</p>
<p>To use those options, you can write your Rack handler like</p>
<pre class="ruby" title="code">app = Rack::Builder.new do
  use Rack::CommonLogger  # apache-like logger
  use Rack::ShowExceptions
  use Rack::Reloader
  use Rack::ShowStatus  # create default pages for common HTTP error codes
  use Rack::Lint  #ensures all applications behave validly
  run Index.new
end
Rack::Handler::WEBRick.run(app, :Port =&gt; 3000)</pre>
<p>There&#8217;s plenty of magic going on with Rack; A magic that does not sacrifice speed and/or flexibility.  I would highly recommend giving Rack a look into the next time you want to write a simple web page.</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2008/03/17/forget-about-little-old-cgi/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t Forget About Little Old CGI</title>
		<link>http://shifteleven.com/articles/2008/03/13/dont-forget-about-little-old-cgi?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dont-forget-about-little-old-cgi</link>
		<comments>http://shifteleven.com/articles/2008/03/13/dont-forget-about-little-old-cgi#comments</comments>
		<pubDate>Fri, 14 Mar 2008 03:36:46 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Ruby off Rails]]></category>
		<category><![CDATA[cgi]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://shifteleven.com/?p=40</guid>
		<description><![CDATA[Rails is great and all, but sometimes it can be just a little too much and you just need to set up a few pages, not the next big app. Fear not, there is still tool in the ruby tool &#8230; <a href="http://shifteleven.com/articles/2008/03/13/dont-forget-about-little-old-cgi">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Rails is great and all, but sometimes it can be just a little too much and you just need to set up a few pages, not the next big app.  Fear not, there is still tool in the ruby tool box at your disposal: <a href="http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/index.html.">the CGI library</a> CGI is fast and lean and still can be used will all of your favorite friends, like <a href="http://haml.hamptoncatlin.com/">HAML</a></p>
<p><span id="more-40"></span></p>
<p>Since you&#8217;re using CGI, you don&#8217;t _have_ to worry about setting up FCGI, or Mongrel.  You can use <a href="http://www.modruby.net/en/">mod_ruby</a> if you&#8217;re using Apache or cgi with Lighttpd.</p>
<p>So now that servers are out of the way, time for some code.</p>
<p>I mentioned that you don&#8217;t have to give up HAML. So for that, I propose this little directory structure.</p>
<pre title="code">mysite/
   |- templates/
   `- public/</pre>
<p>Hopefully you guessed that <code>templates</code> contains your HAML templates and <code>public</code> is the webroot for your site.  Now in your <code>public</code> directory, create an <code>index.rb</code> file, and perhaps it could somewhat look like:</p>
<pre class="ruby" title="code">require 'rubygems'
require 'haml'
require 'cgi'

# A Hash for all of the variables we want to send to HAML
model = {}

# Why not get the time
model[:time] = Time.now

cgi = CGI.new

# Set up the template
haml_engine = Haml::Engine.new(File.read('../templates/index.haml'))

# Render out the HTML
cgi.out { haml_engine.render(Object.new, model) }</pre>
<p>And there&#8217;s the tedious code.  There are some interesting pieces to notice.  For one, when calling cgi.out, that sends out the proper headers out to the browser *(very important)*.  Also, this could probably be wrapped up into a nice function to reduce the strain of repeating one&#8217;s self.</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2008/03/13/dont-forget-about-little-old-cgi/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

