ok,
I wrote something that is about 80% there.
idea 1:
script/generate controller serve_static index
2:
copy all the static content here:
app/views/serve_static
3:
add a line to the END of routes.rb
map.connect ‘*anything’, :controller => “serve_static” , :action => ‘index’
-
Edit index()
inside of
app/controllers/serve_static_controller.rb
All the info I need is inside of request.request_uri
Just two lines gets me 50% there:
def index
the_file_name = ‘serve_static’ + request.request_uri
render(:file => the_file_name, :layout => true, :use_full_path =>true, :content_type => “text/html”)
end
-
Get more elaborate:
-look at file suffixes to guess about content_type: jpg, txt, html, xml…
-rescue if I make a bad assumption about file existence
Result:
class ServeStaticController < ApplicationController
def index
logger.info 'request.request_uri is ’
logger.info request.request_uri.inspect
# get the file name from the request_uri
the_file_name = 'serve_static' + request.request_uri
[logger.info](http://logger.info) 'the_file_name is '
[logger.info](http://logger.info) the_file_name
# Look for something like this:
# /some/path//gna.rl-e_y#%jnk/-/file.whatever
# We want to know if whatever exists and if so, what it is.
regexp = /(^\/.+)(\.\w+$)/
the_match = regexp.match request.request_uri
[logger.info](http://logger.info) 'the_match.to_a.inspect is '
[logger.info](http://logger.info) the_match.to_a.inspect
case
when the_match.to_a[2] == nil
# No file extension so maybe we have index.html there
begin
render(:file => the_file_name + '/index.html' , :layout => true, :use_full_path =>true)
rescue
# If nothing there, just render index.rhtml
render
end
else
[logger.info](http://logger.info) 'the suffix is ' + the_match.to_a[2]
begin
case
when the_match.to_a[2] == '.jpg'
render(:file => the_file_name, :layout => false, :use_full_path =>true, :content_type => "image/jpg")
when the_match.to_a[2] == '.gif'
render(:file => the_file_name, :layout => false, :use_full_path =>true, :content_type => "image/gif")
when the_match.to_a[2] == '.png'
render(:file => the_file_name, :layout => false, :use_full_path =>true, :content_type => "image/png")
when the_match.to_a[2] == '.txt'
render(:file => the_file_name, :layout => true, :use_full_path =>true, :content_type => "text/text")
when the_match.to_a[2] == '.xml'
render(:file => the_file_name, :layout => true, :use_full_path =>true, :content_type => "text/xml")
else
render(:file => the_file_name, :layout => true, :use_full_path =>true, :content_type => "text/html")
end
rescue
# If nothing there, just render index.rhtml
render
end
end
end
end