Class: SiteDiff::Webserver::ResultServer::RunServlet

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/sitediff/webserver/resultserver.rb

Overview

Run sitediff command from browser. Probably dangerous in general.

Instance Method Summary collapse

Constructor Details

#initialize(_server, dir) ⇒ RunServlet

Returns a new instance of RunServlet.



59
60
61
# File 'lib/sitediff/webserver/resultserver.rb', line 59

def initialize(_server, dir)
  @dir = dir
end

Instance Method Details

#do_GET(req, res) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sitediff/webserver/resultserver.rb', line 63

def do_GET(req, res)
  path = req.path_info
  if path != '/diff'
    res['content-type'] = 'text/plain'
    res.body = 'ERROR: Only /run/diff is supported by the /run API at the moment'
    return
  end
  # Thor assumes only one command is called and some values like
  # `options` are share across all SiteDiff::Cli instances so
  # we can't just call SiteDiff::Cli.new().diff
  # This is likely to go very wrong depending on how `sitediff serve`
  # was actually called
  cmd = "#{$PROGRAM_NAME} diff -C #{@dir} --cached=all"
  system(cmd)

  # Could also add a message to indicate success/failure
  # But for the moment, all our files are static
  res.set_redirect(WEBrick::HTTPStatus::Found,
                   "/files/#{SiteDiff::REPORT_FILE}")
end