Class: SiteDiff::Webserver::ResultServer

Inherits:
SiteDiff::Webserver show all
Defined in:
lib/sitediff/webserver/resultserver.rb

Defined Under Namespace

Classes: CacheServlet, RunServlet, SideBySideServlet

Constant Summary

Constants inherited from SiteDiff::Webserver

DEFAULT_PORT

Instance Attribute Summary

Attributes inherited from SiteDiff::Webserver

#ports

Instance Method Summary collapse

Methods inherited from SiteDiff::Webserver

#kill, #uris, #wait

Constructor Details

#initialize(port, dir, opts = {}) ⇒ ResultServer

Returns a new instance of ResultServer.



85
86
87
88
89
90
91
92
93
94
# File 'lib/sitediff/webserver/resultserver.rb', line 85

def initialize(port, dir, opts = {})
  unless File.exist?(File.join(dir, SiteDiff::SETTINGS_FILE))
    raise SiteDiffException,
          "Please run 'sitediff diff' before running 'sitediff serve'"
  end

  @settings = YAML.load_file(File.join(dir, SiteDiff::SETTINGS_FILE))
  @cache = opts[:cache]
  super(port, [dir], opts)
end

Instance Method Details

#open_in_browser(url) ⇒ Object



123
124
125
126
127
128
# File 'lib/sitediff/webserver/resultserver.rb', line 123

def open_in_browser(url)
  commands = %w[xdg-open open]
  cmd = commands.find { |c| which(c) }
  system(cmd, url) if cmd
  cmd
end

#server(opts) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sitediff/webserver/resultserver.rb', line 96

def server(opts)
  dir = opts.delete(:DocumentRoot)
  srv = super(opts)
  srv.mount_proc('/') do |req, res|
    if req.path == '/'
      res.set_redirect(WEBrick::HTTPStatus::Found,
                       "/files/#{SiteDiff::REPORT_FILE}")
    else
      res.set_redirect(WEBrick::HTTPStatus::TemporaryRedirect,
                       "#{@settings['after']}#{req.path}")
    end
  end

  srv.mount('/files', WEBrick::HTTPServlet::FileHandler, dir, true)
  srv.mount('/cache', CacheServlet, @cache)
  srv.mount('/sidebyside', SideBySideServlet, @cache, @settings)
  srv.mount('/run', RunServlet, dir)
  srv
end

#setupObject



116
117
118
119
120
121
# File 'lib/sitediff/webserver/resultserver.rb', line 116

def setup
  super
  root = uris.first
  puts "Serving at #{root}"
  open_in_browser(root) if @opts[:browse]
end

#which(cmd) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/sitediff/webserver/resultserver.rb', line 130

def which(cmd)
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    file = File.join(path, cmd)
    return file if File.executable?(file)
  end
  nil
end