-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.rb
68 lines (45 loc) · 1.15 KB
/
server.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require 'pathname'
require 'webrick'
include WEBrick
class Server
def initialize port, document_root, verbose = false
document_root = File.dirname(__FILE__) unless document_root
document_root = Pathname.new(document_root)#.parent.parent
if verbose
options = {:Port => port}
else
options = {:Port => port, :AccessLog => []}
end
options[:DocumentRoot] = document_root.to_s
puts "Root directory: #{options[:DocumentRoot]}"
@server = HTTPServer.new(options)
end
def start
@server.start
end
def shutdown
@server.shutdown
end
end
def startup_error port, message = nil
$stderr.puts message if message
$stderr.puts "Usage: #{$0} [port] [root-dir]"
$stderr.puts " port defaults #{port}"
exit
end
root = nil
port = 80
if ARGV[0] =~ /\d+/
port = ARGV[0].to_i
root = ARGV[1] if ARGV.length == 2
elsif ARGV.length > 0
root = ARGV[0]
end
if root
startup_error(port, "Invalid root directory") unless File.directory?(root)
end
server = Server.new(port, root)
['TERM', 'INT'].each do |signal|
trap(signal){ server.shutdown }
end
server.start