class Yarrow::Server

Little web server for browsing local files.

Attributes

config[R]

Public Class Methods

default() click to toggle source
# File lib/yarrow/server.rb, line 10
def self.default
  new(Yarrow::Configuration.load_defaults)
end
new(instance_config) click to toggle source
# File lib/yarrow/server.rb, line 14
def initialize(instance_config)
  if instance_config.server.nil?
    raise ConfigurationError.missing_section(:server)
  end

  @config = instance_config
end

Public Instance Methods

app() click to toggle source

Builds a Rack application to serve files in the output directory.

If no output directory is specified, defaults to the current working directory.

‘auto_index` and `live_reload` middleware needs to run in the specific order which is hardcoded here.

@return [Yarrow::Server::StaticFiles]

# File lib/yarrow/server.rb, line 87
def app
  app = Rack::Builder.new

  middleware_stack.each do |middleware|
    app.use(middleware)
  end

  app.use(ResourceRewriter)
  app.use(DirectoryIndex, root: docroot, index: default_index)

  app_args = [docroot, {}].tap { |args| args.push(default_type) if default_type }

  static_app = Rack::Files.new(*app_args)

  if live_reload?
    require 'rack-livereload'
    app.use(Rack::LiveReload, no_swf: true)
  end

  if auto_index?
    app.run(Rack::Directory.new(docroot, static_app))
  else
    app.run(static_app)
  end

  app
end
run() click to toggle source

Starts the server.

Listens on ‘localhost:4000` unless `server.host` and `server.port` are provided in the config.

# File lib/yarrow/server.rb, line 121
def run
  if live_reload?
    reactor = Livereload::Reactor.new
    reactor.start
  end

  handler = Rackup::Handler.get(run_options[:server])

  trap(:INT) do
    handler.shutdown if handler.respond_to?(:shutdown)
    reactor.stop if live_reload?
  end

  handler.run(app, **run_options)
end

Private Instance Methods

auto_index?() click to toggle source

@return [TrueClass, FalseClass]

# File lib/yarrow/server.rb, line 156
def auto_index?
  return true if config.server.auto_index.nil?
  config.server.auto_index
end
default_index() click to toggle source

@return [String]

# File lib/yarrow/server.rb, line 150
def default_index
  config.server.default_index || 'index.html'
end
default_type() click to toggle source

@return [String]

# File lib/yarrow/server.rb, line 169
def default_type
  config.server.default_type
end
docroot() click to toggle source

Host directory of the mounted web server.

Fallback to ‘config.output_dir`.

@return [String]

# File lib/yarrow/server.rb, line 144
def docroot
  config.output_dir
end
live_reload?() click to toggle source

@return [TrueClass, FalseClass]

# File lib/yarrow/server.rb, line 163
def live_reload?
  if config.server.live_reload then true else false; end
end
middleware_stack() click to toggle source

@return [Array<Class>]

# File lib/yarrow/server.rb, line 175
def middleware_stack
  middleware = config.server.middleware || []
  middleware.map { |class_name| Kernel.const_get(class_name) }
end
run_options() click to toggle source

Provides options required by the Rack server on startup.

@return [Hash]

# File lib/yarrow/server.rb, line 184
def run_options
  {
    :Port => config.server.port,
    :Host => config.server.host,
    :server => config.server.handler.to_sym,
    :daemonize => false,
    :quiet => false,
    :warn => true,
    :debug => false,
  }
end