Created
October 3, 2012 12:20
-
-
Save slevine/3826647 to your computer and use it in GitHub Desktop.
Simple Ruby Script that starts a WebServer using the current directory as the document root.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'rubygems' | |
require 'webrick' | |
require 'optparse' | |
options = {} | |
# Parse options | |
optparse = OptionParser.new do|opts| | |
opts.banner = "Usage: websrv.rb [options]" | |
options[:port] = 3000 | |
opts.on( '-p', '--port PORT', 'Port Number (default 3000)' ) do |port| | |
options[:port] = port | |
end | |
options[:docroot] = Dir.pwd | |
opts.on( '-d', '--doc_root PATH', 'Document Root (default current dir)' ) do |docroot| | |
puts docroot | |
options[:docroot] = docroot | |
end | |
options[:bind] = "localhost" | |
opts.on( '-b', '--bind HOST', 'Address to bind to (default localhost)' ) do |bind| | |
options[:bind] = bind | |
end | |
opts.on( '-h', '--help', 'Display this screen' ) do | |
puts opts | |
exit | |
end | |
end | |
optparse.parse! | |
# Initialize and start WEBrick | |
WEBrick::HTTPUtils::DefaultMimeTypes.store('rhtml', 'text/html') | |
server = WEBrick::HTTPServer.new( | |
:BindAddress => options[:bind], | |
:Port => options[:port], | |
:DocumentRoot => options[:docroot] ) | |
trap "INT" do server.shutdown end | |
server.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment