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
69
70
71
72
|
# File 'lib/fsws/cli.rb', line 17
def start
if options[:version]
return version
end
dir = options[:dir]
browser = !options[:'no-browser']
port = options[:port]
interface = options[:interface]
if interface == '*'
interface = '0.0.0.0'
end
if dir
path = File.absolute_path(dir)
if Dir.exist?(path)
Dir.chdir(path)
else
$stderr.puts "Directory does not exist: #{normalize(path)}."
exit 1
end
end
if Signal.list.key? 'QUIT'
Signal.trap('QUIT') do
puts 'Stopping...'
Rack::Handler::WEBrick.shutdown
end
end
if Signal.list.key? 'INT'
Signal.trap('INT') do
puts 'Stopping...'
Rack::Handler::WEBrick.shutdown
end
end
puts ">> Serving #{normalize(Dir.pwd)}"
puts ">> Listening on #{interface}:#{port}"
puts ">> Ctrl+C to stop"
rd, wr = IO.pipe
opts = {
BindAddress: interface,
Port: port,
Logger: WEBrick::Log.new(wr),
AccessLog: []
}
Rack::Handler::WEBrick.run(Fsws::server, opts) do
if browser
Launchy.open("http://localhost:#{port}")
end
end
end
|