Class: Yarrow::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/yarrow/server.rb,
lib/yarrow/server/livereload.rb

Overview

Little web server for browsing local files.

Defined Under Namespace

Modules: Livereload Classes: DirectoryIndex

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance_config) ⇒ Server

Returns a new instance of Server.



9
10
11
12
13
14
15
# File 'lib/yarrow/server.rb', line 9

def initialize(instance_config)
  if instance_config.server.nil?
    raise ConfigurationError.new("Missing server entry")
  end

  @config = instance_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/yarrow/server.rb', line 7

def config
  @config
end

Instance Method Details

#appYarrow::Server::StaticFiles

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.

Returns:

  • (Yarrow::Server::StaticFiles)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/yarrow/server.rb', line 50

def app
  app = Rack::Builder.new

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

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

  app_args = [docroot, {}].tap { |args| args.push(default_type) if default_type }
  static_app = Rack::File.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

#runObject

Starts the server.

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/yarrow/server.rb', line 82

def run
  if live_reload?
    reactor = Livereload::Reactor.new
    reactor.start
  end

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

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

  handler.run(app, run_options)
end