Created
March 14, 2012 20:25
-
-
Save seancribbs/2039255 to your computer and use it in GitHub Desktop.
Static/file resources in Webmachine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'bundler/setup' | |
require 'webmachine' | |
require 'webmachine/adapters/rack' | |
require 'pathname' | |
class StaticResource < Webmachine::Resource | |
def initialize | |
@pathname = Pathname(request.path_info[:root].to_s) + request.disp_path | |
end | |
def content_types_provided | |
[[Rack::Mime.mime_type(@pathname.extname, 'application/octet-stream'), :serve_file]] | |
end | |
def resource_exists? | |
@pathname.file? | |
end | |
def last_modified | |
@pathname.mtime | |
end | |
def serve_file | |
# Note this is probably not a good idea for big files. | |
# In the future, WM should be able to stream out IO objects. | |
@pathname.read | |
end | |
end | |
class HelloResource < Webmachine::Resource | |
def to_html | |
"<html><body><h1>Hello, world!</h1></body></html>" | |
end | |
end | |
Hello = Webmachine::Application.new do |app| | |
app.routes do | |
add [], HelloResource | |
add ['*'], StaticResource, :root => "static" | |
end | |
app.configure do |config| | |
config.adapter = :Rack | |
end | |
end | |
run Hello.adapter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source :rubygems | |
gem 'webmachine' | |
gem 'rack' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment