Class: Yarrow::Server
- Inherits:
-
Object
- Object
- Yarrow::Server
- 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
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
Instance Method Summary collapse
-
#app ⇒ Yarrow::Server::StaticFiles
Builds a Rack application to serve files in the output directory.
-
#initialize(instance_config) ⇒ Server
constructor
A new instance of Server.
-
#run ⇒ Object
Starts the server.
Constructor Details
#initialize(instance_config) ⇒ Server
Returns a new instance of Server.
13 14 15 16 17 18 19 |
# File 'lib/yarrow/server.rb', line 13 def initialize(instance_config) if instance_config.server.nil? raise ConfigurationError.missing_section(:server) end @config = instance_config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/yarrow/server.rb', line 7 def config @config end |
Class Method Details
.default ⇒ Object
9 10 11 |
# File 'lib/yarrow/server.rb', line 9 def self.default new(Yarrow::Configuration.load_defaults) 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.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/yarrow/server.rb', line 54 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.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/yarrow/server.rb', line 87 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 if live_reload? end handler.run(app, ) end |