Class: Nanoc::CLI::Commands::View

Inherits:
Nanoc::CLI::CommandRunner show all
Defined in:
lib/nanoc/cli/commands/view.rb

Constant Summary

DEFAULT_HANDLER_NAME =
:thin

Instance Method Summary (collapse)

Methods inherited from Nanoc::CLI::CommandRunner

#call, call, #debug?, #is_in_site_dir?, #load_site, #require_site, #site

Instance Method Details

- (Object) run



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
# File 'lib/nanoc/cli/commands/view.rb', line 21

def run
  load_adsf
  require 'rack'

  # Make sure we are in a nanoc site directory
  require_site

  # Set options
  options_for_rack = {
    :Port      => (options[:port] || 3000).to_i,
    :Host      => (options[:host] || '0.0.0.0')
  }

  # Get handler
  if options.key?(:handler)
    handler = Rack::Handler.get(options[:handler])
  else
    begin
      handler = Rack::Handler.get(DEFAULT_HANDLER_NAME)
    rescue LoadError
      handler = Rack::Handler::WEBrick
    end
  end

  # Build app
  site = self.site
  app = Rack::Builder.new do
    use Rack::CommonLogger
    use Rack::ShowExceptions
    use Rack::Lint
    use Rack::Head
    use Adsf::Rack::IndexFileFinder, :root => site.config[:output_dir]
    run Rack::File.new(site.config[:output_dir])
  end.to_app

  # Run autocompiler
  handler.run(app, options_for_rack)
end