Skip to content

Instantly share code, notes, and snippets.

@jokester
Created April 25, 2015 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jokester/d546aafd926e94597d60 to your computer and use it in GitHub Desktop.
Save jokester/d546aafd926e94597d60 to your computer and use it in GitHub Desktop.
a simplehttpserver in ruby
#!/usr/bin/env ruby
require 'rack'
require 'socket'
require 'optparse'
ENV['RACK_ENV'] = "production"
# Options :
# --root (mandatory)
# --port (optional)
# --extension ?
handler_options = { :Port => 10080 }
app_options = { :root => "." }
OptionParser.new do |parser|
parser.on "-p", "--port PORT" do |port|
handler_options[ :Port ] = port.to_i
end
parser.on "-r", "--root ROOT_DIR" do |root_dir|
app_options[ :root ] = root_dir
end
end.parse!
puts "Root directory:\t#{File.absolute_path app_options[:root] }"
puts "Server port:\t#{handler_options[:Port]}"
Socket::getaddrinfo(Socket.gethostname,"http", nil, :STREAM).each do |family, port, hostname, ip, *_|
puts "Server IP:\t#{ip[/^[^%]*/]}"
end
app = Rack::Directory.new app_options[:root]
handler = Rack::Handler.pick %w/ thin webrick /
handler.run app, handler_options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment