class Fsws::CommandLine

Public Instance Methods

start() click to toggle source
# 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
version() click to toggle source
# File lib/fsws/cli.rb, line 75
def version
  puts "fsws #{Fsws::VERSION}"
end

Private Instance Methods

normalize(path) click to toggle source
# File lib/fsws/cli.rb, line 81
def normalize(path)
  path.gsub(File::SEPARATOR, File::ALT_SEPARATOR || File::SEPARATOR)
end