-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.rb
executable file
·36 lines (31 loc) · 907 Bytes
/
web.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env ruby
require 'webrick'
require 'optparse'
include WEBrick # let's import the namespace so
# I don't have to keep typing
# WEBrick:: in this documentation.
options = {:Port=>8080, :DocumentRoot=> './'}
optparser=OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename(__FILE__)} [options]"
opts.on("-p", "--port port_num", "Specify Port Number") do |v|
options[:Port]=v
end
opts.on("-d", "--docroot path", "Specify Document Root Folder") do |v|
options[:DocumentRoot]=v
end
end
def start_webrick(config = {})
# always listen on port 8080
server = HTTPServer.new(config)
yield server if block_given?
['INT', 'TERM'].each {|signal|
trap(signal) {server.shutdown}
}
server.start
end
begin
optparser.parse!(ARGV)
start_webrick(options)
rescue Exception=>e
puts optparser.to_s
end