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, ResourceRewriter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance_config) ⇒ Server

Returns a new instance of Server.



14
15
16
17
18
19
20
# 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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Class Method Details

.defaultObject



10
11
12
# File 'lib/yarrow/server.rb', line 10

def self.default
  new(Yarrow::Configuration.load_defaults)
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)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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

#runObject

Starts the server.

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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