Skip to main content

Benjamin Oakes

Photo of Ben Oakes

Hi, I'm Ben Oakes and this is my geek blog. Currently, I'm a Ruby/JavaScript Developer at Liaison. Previously, I was a Developer at Continuity and Hedgeye, a Research Assistant in the Early Social Cognition Lab at Yale University and a student at the University of Iowa. I also organize TechCorridor.io, ICRuby, OpenHack Iowa City, and previously organized NewHaven.rb. I have an amazing wife named Danielle Oakes.

Ruby Simple HTTP Server, minimalist Rake

by Ben

I use a really simple HTTP server all the time. It happens to be written in Python:

python -m SimpleHTTPServer 5000

That serves all the files in the current directory over HTTP on port 5000. Honestly, it works just fine, but I’ve always wondered if Ruby had an equivalent.

Here it is:

ruby -run -e httpd . -p 5000

(from Aaron Patterson’s tweet found via Zach Morek)

It’s pretty much the same, except it’s written in Ruby. More often than not, that’s not a big difference — except I can understand the code behind it.

#
# = un.rb
#
# Copyright (c) 2003 WATANABE Hirofumi <eban@ruby-lang.org>
#
# This program is free software.
# You can distribute/modify this program under the same terms of Ruby.

# [...]

##
# Run WEBrick HTTP server.
#
# ruby -run -e httpd -- [OPTION] DocumentRoot
#
# --bind-address=ADDR address to bind
# --port=NUM listening port number
# --max-clients=MAX max number of simultaneous clients
# --temp-dir=DIR temporary directory
# --do-not-reverse-lookup disable reverse lookup
# --request-timeout=SECOND request timeout in seconds
# --http-version=VERSION HTTP version
# -v verbose
#

def httpd
  setup("", "BindAddress=ADDR", "Port=PORT", "MaxClients=NUM", "TempDir=DIR",
        "DoNotReverseLookup", "RequestTimeout=SECOND", "HTTPVersion=VERSION") do
    |argv, options|
    require 'webrick'
    opt = options[:RequestTimeout] and options[:RequestTimeout] = opt.to_i
    [:Port, :MaxClients].each do |name|
      opt = options[name] and (options[name] = Integer(opt)) rescue nil
    end
    unless argv.size == 1
      raise ArgumentError, "DocumentRoot is mandatory"
    end
    options[:DocumentRoot] = argv.shift
    s = WEBrick::HTTPServer.new(options)
    shut = proc {s.shutdown}
    siglist = %w"TERM QUIT"
    siglist.concat(%w"HUP INT") if STDIN.tty?
    siglist &= Signal.list.keys
    siglist.each do |sig|
      Signal.trap(sig, shut)
    end
    s.start
  end
end

So how does it work? It’s actually a little surprising. Here’s the command again for reference:

ruby -run -e httpd . -p 5000

In order:

While that code is probably too clever, it’s nice to have a simple HTTP server wherever I have Ruby.

Even more, the concept is reusable:

# File: ake.rb
# Minimalist rake.  :)
def greet
  puts "Hello, #{ ARGV[0] }!"
end

Here’s the output

$ ruby -I . -rake -e greet Ben
Hello, Ben!
$ ruby -r ./ake -e greet Ben
Hello, Ben!

That could be a nice minimalist way to write some helper scripts without Rake or Thor.

Comments

You can leave a comment, or trackback from your site. Article comments

Tim says:

Interesting read, thanks. That `un.rb` bit is odd!

Denny says:

Nice write-up, Ben. I stumbled across this trying to remind myself how to quickly start a server. It’s always fun to read something from a friend.

Ben says:

Happy to hear it helped!

Leave a comment