module Middleman::PreviewServer

def mount_instance(app)

Returns:
  • (void) -

Parameters:
  • app (Middleman::Application) --
def mount_instance(app)
  @app = app
  @webrick.mount "/", ::Rack::Handler::WEBrick, @app.class.to_rack_app
end

def needs_to_reload?(paths)

Returns:
  • (Boolean) - Whether the server needs to reload

Parameters:
  • paths (Array) -- Array of paths to check
def needs_to_reload?(paths)
  paths.any? do |path|
    path.match(%{^config\.rb}) || path.match(%r{^lib/^[^\.](.*)\.rb$}) || path.match(%r{^helpers/^[^\.](.*)_helper\.rb$})
  end
end

def register_signal_handlers

Returns:
  • (void) -
def register_signal_handlers
  trap("INT")  { shutdown }
  trap("TERM") { shutdown }
  trap("QUIT") { shutdown }
end

def reload

Returns:
  • (void) -
def reload
  stop
  start @last_options
end

def setup_webrick(host, port, is_logging)

Returns:
  • (void) -
def setup_webrick(host, port, is_logging)
  @host = host
  @port = port
  
  http_opts = {
    :BindAddress => @host,
    :Port        => @port,
    :AccessLog   => []
  }
  
  unless is_logging
    http_opts[:Logger] = ::WEBrick::Log::new(nil, 0)
  end

  ::WEBrick::HTTPServer.new(http_opts)
end

def shutdown

Returns:
  • (void) -
def shutdown
  stop
  @webrick.shutdown
end

def start(options={})

Returns:
  • (void) -
def start(options={})
  require "webrick"
  
  app = ::Middleman::Application.server.inst do
    if options[:environment]
      set :environment, options[:environment].to_sym
    end
    
    if options[:debug]
      set :logging, true
    end
  end
  puts "== The Middleman is standing watch on port #{options[:port]||4567}"
  @webrick ||= setup_webrick(
    options[:host]  || "0.0.0.0",
    options[:port]  || DEFAULT_PORT,
    options[:debug] || false
  )
  
  mount_instance(app)
  start_file_watcher unless options[:"disable-watcher"]
  
  @initialized ||= false
  unless @initialized
    @initialized = true
    
    register_signal_handlers unless ::Middleman::WINDOWS
    
    # Save the last-used options so it may be re-used when
    # reloading later on.
    @last_options = options
    
    @webrick.start
  end
end

def start_file_watcher

def start_file_watcher
  # Watcher Library
  require "listen"
  listener = Listen.to(Dir.pwd, :relative_paths => true)

  listener.change do |modified, added, removed|
    added_and_modified = (modified + added)
    if added_and_modified.length > 0
      # See if the changed file is config.rb or lib/*.rb
      if needs_to_reload?(added_and_modified)
        reload
        return listener.stop
      end
      # Otherwise forward to Middleman
      added_and_modified.each do |path|
        @app.files.did_change(@app.root_path + path)
      end
    end

    if removed.length > 0
      # See if the changed file is config.rb or lib/*.rb
      if needs_to_reload?(removed)
        reload
        return listener.stop
      end
      # Otherwise forward to Middleman
      removed.each do |path|
        @app.files.did_delete(@app.root_path + path)
      end
    end
  end
  # Don't block this thread
  listener.start(false)
end

def stop

Returns:
  • (void) -
def stop
  puts "== The Middleman is shutting down"
  unmount_instance
end

def unmount_instance

Returns:
  • (void) -
def unmount_instance
  @webrick.unmount "/"
  @app = nil
end