Sha256: 20e1327fab10f5b8fed70ff866f68018802ab1d5c3a27e24a29faa9063322dbb
Contents?: true
Size: 1.3 KB
Versions: 3
Compression:
Stored size: 1.3 KB
Contents
# encoding: UTF-8 require "mime/types" module Bunch class Server def initialize(config) Bunch.load_default_config_if_possible @reader = config.fetch(:reader) do proc { FileTree.from_path(config.fetch(:root)) } end @pipeline = config.fetch(:pipeline) do Pipeline.for_environment config end @headers = config.fetch(:headers) do { "Cache-Control" => "private, max-age=0, must-revalidate", "Pragma" => "no-cache", "Expires" => "Thu, 01 Dec 1994 16:00:00 GMT" } end end def call(env) path = env["PATH_INFO"].sub(/^\//, '') type = mime_type_for_path(path) tree = @pipeline.process(@reader.call) file = tree.get(path) if file.is_a?(File) [200, headers_for_type(type), [file.content]] else [404, headers_for_type("text/plain"), ["Not Found"]] end rescue => e [500, headers_for_type("text/plain"), [error_message(e)]] end private def mime_type_for_path(path) MIME::Types.type_for(path).first || "text/plain" end def headers_for_type(mime_type) @headers.merge("Content-Type" => mime_type.to_s) end def error_message(e) "#{e.class}: #{e.message}\n #{e.backtrace.join("\n ")}" end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
bunch-1.0.0pre3 | lib/bunch/server.rb |
bunch-1.0.0pre2 | lib/bunch/server.rb |
bunch-1.0.0pre1 | lib/bunch/server.rb |