Created
January 9, 2014 04:06
-
-
Save mdeltito/8329273 to your computer and use it in GitHub Desktop.
httpdir: run a webrick webserver from any directory to serve anything.
This file contains hidden or 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/env ruby | |
require 'webrick' | |
require 'optparse' | |
include WEBrick | |
options = { | |
port: (12000 + (Dir::pwd.hash % 1000)), | |
dir: Dir::pwd | |
} | |
OptionParser.new do |opts| | |
opts.banner = "Usage: httpdir [options]" | |
opts.on("-p", "--port PORT", "Force server port [auto]") do |port| | |
options[:port] = port | |
end | |
opts.on("-d", "--directory DIR", "Directory (default: Dir::pwd)") do |dir| | |
options[:dir] = dir | |
end | |
end.parse! | |
puts "URL: http://#{Socket.gethostname}:#{options[:port]}" | |
begin | |
server = HTTPServer.new( | |
:Port => options[:port], | |
:DocumentRoot => options[:dir] | |
) | |
trap("INT"){ server.shutdown } | |
server.start | |
rescue Exception => e | |
puts "Unable to start server" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment