Class: Yarrow::Server
- Inherits:
-
Object
- Object
- Yarrow::Server
- Includes:
- Configurable
- 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 Method Summary collapse
-
#app ⇒ Yarrow::Server::StaticFiles
Builds a Rack application to serve files in the output directory.
-
#initialize ⇒ Server
constructor
A new instance of Server.
-
#run ⇒ Object
Starts the server.
Methods included from Configurable
Constructor Details
#initialize ⇒ Server
Returns a new instance of Server.
9 10 11 12 13 |
# File 'lib/yarrow/server.rb', line 9 def initialize if config.server.nil? raise ConfigurationError.new('Missing server entry') end end |
Instance Method Details
#app ⇒ Yarrow::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.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/yarrow/server.rb', line 47 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 |
#run ⇒ Object
Starts the server.
Listens on ‘localhost:4000` unless `server.host` and `server.port` are provided in the config.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/yarrow/server.rb', line 79 def run if live_reload? reactor = Livereload::Reactor.new reactor.start end handler = Rack::Handler.get([:server]) trap(:INT) do handler.shutdown if handler.respond_to?(:shutdown) reactor.stop end handler.run(app, ) end |