2010-10-05 60 views
16

我試圖從本教程獲得一些信息的簡單Web服務器:http://m.onkey.org/2008/11/18/ruby-on-rack-2-rack-builder紅寶石機架 - 安裝讀取index.html作爲默認

基本上我想有一個文件config.ru告訴機架讀取當前目錄,所以我可以像訪問簡單的apache服務器一樣訪問所有文件,並使用index.html文件讀取默認的根目錄......有什麼辦法可以做到嗎?

我目前config.ru看起來是這樣的:

run Rack::Directory.new('') 
#this would read the directory but it doesn't set the root to index.html 


map '/' do 
    file = File.read('index.html') 
    run Proc.new {|env| [200, {'Content-Type' => 'text/html'}, file] } 
end 
#using this reads the index.html mapped as the root but ignores the other files in the directory 

所以我不知道如何從這裏出發......

我也試過這下面的教程的例子,但沒有按thin」 t正常啓動。

builder = Rack::Builder.new do 

    run Rack::Directory.new('') 

    map '/' do 
    file = File.read('index.html') 
    run Proc.new {|env| [200, {'Content-Type' => 'text/html'}, file] } 
    end 

end 

Rack::Handler::Thin.run builder, :port => 3000 

在此先感謝

+0

FWIW,':port => 3000'應該是':Port => 3000' - 注意Port中的大寫字母「P」。 – briangonzalez 2013-10-12 16:43:51

回答

36

我認爲你缺少的rackup命令。以下是它的使用方法:

rackup config.ru 

這將使用webrick在端口9292上運行您的機架應用程序。您可以閱讀「rackup --help」獲取更多信息,瞭解如何更改這些默認設置。

關於您要創建的應用程序。這裏是我認爲它應該是這樣的:

# This is the root of our app 
@root = File.expand_path(File.dirname(__FILE__)) 

run Proc.new { |env| 
    # Extract the requested path from the request 
    path = Rack::Utils.unescape(env['PATH_INFO']) 
    index_file = @root + "#{path}/index.html" 

    if File.exists?(index_file) 
    # Return the index 
    [200, {'Content-Type' => 'text/html'}, File.read(index_file)] 
    # NOTE: using Ruby >= 1.9, third argument needs to respond to :each 
    # [200, {'Content-Type' => 'text/html'}, [File.read(index_file)]] 
    else 
    # Pass the request to the directory app 
    Rack::Directory.new(@root).call(env) 
    end 
} 
+0

非常好,這正是我想要達到的目標,我想我可以從這裏獲得更多信息並瞭解更多信息。非常感謝valo – zanona 2010-10-14 15:02:52

+1

偶然發現了這一點,並認爲我會添加它:[REQUEST_PATH是一個遺留變量,在Rack中完全未定義。](http://groups.google.com/group/rack-devel/ browse_thread/thread/0a41ecc3ce2db50d) – 2011-02-12 09:17:02

+0

正是我需要的。謝謝! – Mischa 2011-07-20 02:49:12

8

你可以做到這一點使用機架::靜態

map "/foo" do 
    use Rack::Static, 
    :urls => [""], :root => File.expand_path('bar'), :index => 'index.html' 
    run lambda {|*|} 
end 
+0

這解決了我在Sinatra下遇到的一個大問題。非常感謝! – Rots 2013-09-18 11:02:08

1

對於我來說,使用Ruby 2.0和機架1.5.2,sinm solution曾服務於索引頁(既作爲根默認頁和顯式加載的),但對於其他的文件我得到與以下錯誤:

Rack::Lint::LintError: Status must be >=100 seen as integer 

我與此SO answer和叔組合sinm溶液他發現代碼片段上Heroku documentation以獲得所需的行爲(假設整個網站的文件夾中包含名爲public):

2

我這樣做下面的例子完全一樣:

module Rack 
    class DirectoryIndex 
    def initialize(app) 
     @app = app 
    end 
    def call(env) 
     index_path = ::File.join($documentRoot, Rack::Request.new(env).path.split('/'), 'index.html') 
     if ::File.exists?(index_path) 
     return [200, {"Content-Type" => "text/html"}, [::File.read(index_path)]] 
     else 
     @app.call(env) 
     end 
    end 
    end 
end 

require 'rack_directory_index.rb' 

$documentRoot = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'build')) 

Capybara.app = Rack::Builder.new do |builder| 
puts "Creating static rack server serving #{$documentRoot}" 
use Rack::DirectoryIndex 
    run Rack::Directory.new($documentRoot) 
end 

Capybara.configure do |config| 
    config.run_server = true 
end 

該解決方案主要是一個副本,並從不同的答案,但粘貼它工作正常。你可以找到它作爲gist here aswell,祝你好運

+0

請使用鏈接中的一些信息來增強您的答案。答案不應該只包含一個鏈接 – user1781290 2014-09-30 12:05:48

+0

更新了答案,謝謝你的反饋 – 2014-09-30 12:32:56