tags:

views:

31

answers:

1

Hello,

I have a nanoc site (so, all static pages) that I'd like to test with unicorn. The idea behind this is to host this site on heroku then. The structure is then a rack application. I have added a config.ru file like:

require 'rubygems'
require 'rack'
require 'rack-rewrite'
require 'rack/contrib'
use Rack::Rewrite do
 rewrite '/','/output/index.html'
end  
use Rack::Static, :urls => ['/'], :root => "output"

(all my static resources are located in the output directory)

When I run unicorn I got the following error message:

NoMethodError at /output/index.html
undefined method `to_i' for #<Rack::Static:0x10165ee18>

I do not really understand what I am missing here :(

Any idea ?

Thanks and regards,

Luc

+1  A: 

Hello,

with this config.ru, it works :)

require 'rubygems'
require 'rack'
require 'rack/contrib'
require 'rack-rewrite'
require 'mime/types'

use Rack::Deflater
use Rack::ETag
module ::Rack
    class TryStatic < Static

        def initialize(app, options)
            super
            @try = ([''] + Array(options.delete(:try)) + [''])
        end

        def call(env)
            @next = 0
            while @next < @try.size && 404 == (resp = super(try_next(env)))[0]
                @next += 1
            end
            404 == resp[0] ? @app.call : resp
        end

        private
        def try_next(env)
            env.merge('PATH_INFO' => env['PATH_INFO'] + @try[@next])
        end
    end
end

use Rack::TryStatic,
    :root => "output", # static files root dir
    :urls => %w[/], # match all requests
    :try => ['.html', 'index.html', '/index.html'] # try these postfixes sequentially

 errorFile='output/404.html'
run lambda { [404, {
            "Last-Modified" => File.mtime(errorFile).httpdate,
            "Content-Type" => "text/html",
            "Content-Length" => File.size(errorFile).to_s
        }, File.read(errorFile)] }

Regards, Luc

Luc
Nice! I was curious too and glad for your succes. Meanwhile I find [this](http://teachmetocode.com/screencasts/faking-sinatra-with-rack-and-metaprogramming/) and was helpful, for me at least :)
kfl62
Hello, this is a piece of code I found on the net. Thanks for your link !!!
Luc