Sha256: 16328649c0b6587b3a28d7c7baf6b4325ffd568d06fcba8e41af7dd0cba8e705

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

require 'rack'
require 'rack/rewrite'

module Jobim::Server

  def self.start(opts)
    app = Rack::Builder.new do
      use Rack::Rewrite do
        rewrite %r{(.*)}, -> (match, env) do
          request_path = env["REQUEST_PATH"]

          return match[1] if opts[:Prefix].length > request_path.length

          local_path = File.join(opts[:Dir], request_path[opts[:Prefix].length..-1])

          if File.directory?(local_path) and
              File.exists?(File.join(local_path, "index.html"))
            File.join(request_path, "index.html")
          else
            match[1]
          end
        end
      end

      use Rack::CommonLogger, STDOUT

      map opts[:Prefix] do
        run Rack::Directory.new(opts[:Dir])
      end
    end

    puts ">>> Serving #{opts[:Dir]}"

    Rack::Handler::Thin.run(app, opts) do |server|
      if opts[:Daemonize]
        server.pid_file = 'jobim.pid'
        server.log_file = 'jobim.log'
        server.daemonize
      end

      Thin::Logging.silent = opts[:Quiet]
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jobim-0.4.6 lib/jobim/server.rb
jobim-0.4.5 lib/jobim/server.rb