Skip to content
Newer
Older
100644 42 lines (34 sloc) 1.33 KB
d50b94e Add the config.ru with TryStatic.
tomhuda authored Apr 23, 2012
1 # Modified version of TryStatic, from rack-contrib
2 # https://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/try_static.rb
3
4 # Serve static files under a `build` directory:
5 # - `/` will try to serve your `build/index.html` file
6 # - `/foo` will try to serve `build/foo` or `build/foo.html` in that order
7 # - missing files will try to serve build/404.html or a tiny default 404 page
8
9
10 module Rack
11
12 class TryStatic
13
14 def initialize(app, options)
15 @app = app
16 @try = ['', *options.delete(:try)]
17 @static = ::Rack::Static.new(lambda { [404, {}, []] }, options)
18 end
19
20 def call(env)
21 orig_path = env['PATH_INFO']
22 found = nil
23 @try.each do |path|
24 resp = @static.call(env.merge!({'PATH_INFO' => orig_path + path}))
25 break if 404 != resp[0] && found = resp
26 end
27 found or @app.call(env.merge!('PATH_INFO' => orig_path))
28 end
29 end
30 end
31
32 use Rack::TryStatic, :root => "build", :urls => %w[/], :try => ['.html', 'index.html', '/index.html']
33
34 # Run your own Rack app here or use this one to serve 404 messages:
35 run lambda{ |env|
36 not_found_page = File.expand_path("../build/404.html", __FILE__)
37 if File.exist?(not_found_page)
38 [ 404, { 'Content-Type' => 'text/html'}, [File.read(not_found_page)] ]
39 else
40 [ 404, { 'Content-Type' => 'text/html' }, ['404 - page not found'] ]
41 end
42 }
Something went wrong with that request. Please try again.