Skip to content

Commit

Permalink
serving static files
Browse files Browse the repository at this point in the history
  • Loading branch information
blake.mizerany@gmail.com committed Sep 17, 2007
1 parent 95ab366 commit 036c3aa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/sinatra/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ def helpers(&block)
Sinatra::EventContext.class_eval &block
end

def static(path, root)
Sinatra::StaticEvent.new(path, File.join(File.dirname($0) + root))
end

end
34 changes: 34 additions & 0 deletions lib/sinatra/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,38 @@ def run_through_after_filters(context)

end

class StaticEvent < Event

def initialize(path, root, register = true)
super(:get, path, register)
@root = File.expand_path(root)
end

def recognize(path)
canserve = File.dirname(path) == @path
@filename = File.join(@root, path.gsub(/^#{@path}/, ''))
canserve && File.exists?(@filename)
end

def attend(request)
puts 'attend ' + self.inspect
@body = self
end

def status; 200; end

def headers; {}; end

def body; @body; end

def each
File.open(@filename, "rb") { |file|
while part = file.read(8192)
yield part
end
}
end

end

end

0 comments on commit 036c3aa

Please sign in to comment.