2010-10-20 69 views
3

我有一個nanoc網站(所以,所有靜態網頁),我想與獨角獸測試。 這個背後的想法是在heroku上託管這個網站。 該結構是一個機架應用程序。 我已經加入像config.ru文件:nanoc網站測試與獨角獸

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" 

(我所有的靜態資源位於輸出目錄)

當我運行的麒麟,我得到了以下錯誤消息:

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

我真的不明白我在這裏失蹤:(

任何想法?

感謝和問候,

呂克

回答

1

與此config.ru,它的工作原理:)

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)] } 

問候, 呂克

+0

不錯!我也很好奇,爲你的成功感到高興。同時,我發現[這](http://teachmetocode.com/screencasts/faking-sinatra-with-rack-and-metaprogramming/)是有益的,對我來說至少:) – kfl62 2010-10-21 21:26:00

+0

你好,這是一段代碼我在網上找到。感謝您的鏈接! – Luc 2010-10-21 21:38:48