CommonThread

Displaying articles with tag

Mongrel web framework starter kit

Posted by anthony crumley, Tue Nov 06 07:24:00 UTC 2007

Mongrel is a sweet little Ruby based web server. A number of frameworks already run on top of it, Ruby on Rails, Merb, Og + Nitro, Camping, and IOWA. It is actually quite easy to get started once you figure out what to do. Unfortunately the documentation is a bit sparse. On the bright side, all the frameworks mentioned above are open source so there are plenty of examples. These examples are a bit complex so I will try to give you the minimum required to get started.

Put the following code in a file named server.rb.
require 'rubygems'
require 'mongrel'
require 'framework_handler'

config = Mongrel::Configurator.new :host => '127.0.0.1' do
  listener :port => 4000 do
    uri "/", :handler => FrameworkHandler.new
  end
  trap("INT") { stop }
  run
end
config.join

When server.rb is run it will set up a mongrel server at http://localhost:4000. Any url accessed on this server will simply invoke the framework handler described next.

Put the following code in a file named framework_handler.rb.
class FrameworkHandler < Mongrel::HttpHandler
  def process(request, response)
    response.start(200) do |head, out|
      head["Content-Type"] = "text/html" 
      out.write "Hello from " 
      out.write request.params[Mongrel::Const::REQUEST_URI]
    end
  end
end

When a url is accessed on the server this handler will display a message including the uri. If the address http://localhost:4000/ruby/land is accessed then the result will be “Hello from /ruby/land”

As with all starter kits, this just gets you started. Finishing is up to you. Have fun!!

0 comments | Filed Under: | Tags:

Where Did I Leave That Mongrel?

Posted by ben, Thu Jun 07 23:16:00 UTC 2007

If you have used mongrel long enough you may have noticed that mongrel can leave behind orphaned PID files. making it hard to restart mongrel until you clean up the PID files. Someone was nice enough to create a patch for this. it patches the mongrel_rails script to check for orphaned PID files and delete them so that mongrel can start properly.

To apply the patch …
  1. download mongrel_stale_pid_file.patch to the server
  2. cd to the mongrel folder … usually /usr/lib/ruby/gems/1.8/gems/mongrel-XXX (where XXX is the version)
  3. run …
    patch -p0 < /path/to/mongrel_stale_pid_file.patch

IF YOU ARE RUNNING SWIFTIPLY YOU NEED TO DO ONE MORE THING

If you are running swiftiply it runs it own version of the mongrel_rails executable. So, it will need to be patched as well. Simply repeat steps 2 & 3 from above but from the swiftiply folder … usually /usr/lib/ruby/gems/1.8/gems/swiftiply-XXX (where XXX is the version)

0 comments | Filed Under: | Tags:

Categories