Sha256: 7a3ef00a70551d8e29bf316bb01cb7c8f1219d7a1c1ff76d09e5049515a49faf

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

require 'rack'
require 'webrick'
require 'rack/livereload'

module RevealCK
  module Commands
    # The idea of starting up a webserver to display slides locally.
    class StartWebServer
      attr_reader :doc_root, :port
      def initialize(doc_root, port)
        @doc_root = doc_root
        @port = port
      end

      def run
        Rack::Server.new(app: build_rack_app(doc_root),
                         Port: port,
                         Logger: server_log,
                         AccessLog: access_log).start
      end

      private

      def build_rack_app(doc_root)
        Rack::Builder.new do
          use Rack::LiveReload
          use Rack::Static, index: "#{doc_root}/index.html"
          run Rack::Directory.new(doc_root)
        end
      end

      def server_log
        WEBrick::BasicLog.new(temp_log('reveal-ck-server'))
      end

      def access_log
        access_log_file = File.open(temp_log('reveal-ck-access'), 'w')
        [[access_log_file, WEBrick::AccessLog::COMMON_LOG_FORMAT]]
      end

      def temp_log(name)
        require 'tempfile'
        file = Tempfile.new(["#{name}-", '.log'])
        file.path
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
reveal-ck-3.3.1 lib/reveal-ck/commands/start_web_server.rb
reveal-ck-3.3.0 lib/reveal-ck/commands/start_web_server.rb
reveal-ck-3.2.0 lib/reveal-ck/commands/start_web_server.rb
reveal-ck-3.1.0 lib/reveal-ck/commands/start_web_server.rb