2012-11-05 29 views
5

Basit bir statik Raf uygulaması yazıyorum.Raf: URL'yi bir değişken olarak nasıl depolarsınız?

use Rack::Static, 
    :urls => ["/elements", "/img", "/pages", "/users", "/css", "/js"], 
    :root => "archive" 


map '/' do 
    run Proc.new { |env| 
    [ 
     200, 
     { 
     'Content-Type' => 'text/html', 
     'Cache-Control' => 'public, max-age=6400' 
     }, 
     File.open('archive/splash.html', File::RDONLY) 
    ] 
    } 
end 

map '/pages/search.html' do 
    run Proc.new { |env| 
    [ 
     200, 
     { 
     'Content-Type' => 'text/html', 
     'Cache-Control' => 'public, max-age=6400' 
     }, 
     File.open('archive/pages/search.html', File::RDONLY) 
    ] 
    } 
end 

map '/pages/user.html' do 
    run Proc.new { |env| 
    [ 
     200, 
     { 
     'Content-Type' => 'text/html', 
     'Cache-Control' => 'public, max-age=6400' 
     }, 
     File.open('archive/pages/user.html', File::RDONLY) 
    ] 
    } 
end 

# Each map section is repeated for each HTML page served 

Ben değişken olarak URL'yi depolamak ve bunu doğru url ayarlayabilirsiniz nasıl

map url do 
    run Proc.new { |env| 
    [ 
     200, 
     { 
     'Content-Type' => 'text/html', 
     'Cache-Control' => 'public, max-age=6400' 
     }, 
     File.open('archive' + url, File::RDONLY) 
    ] 
    } 
end 

diyor bir harita bölüm oluşturarak bu basitleştirmek istiyorum: Aşağıdaki config.ru koda bak değişken?

cevap

4

Harita parçasına gerek duymamalısınız.

run Proc.new { |env| 
    [ 
    200, 
    { 
     'Content-Type' => 'text/html', 
     'Cache-Control' => 'public, max-age=6400' 
    }, 
    File.open('archive' + env['PATH_INFO'], File::RDONLY) 
    ] 
} 
+0

, teşekkür bu içerik türünü ayarlamak edeceğini –

+0

not bulduğu her ne dosya için 'text/html', hatta 'robots.txt' veya 'için favicon.ico'. Ama sanırım her zaman bu çizgiden çıkıp tarayıcının tahmin etmesine izin verirsin. – rbp

5

ne dersiniz:

çalışır
static_page_mappings = { 
    '/'     => 'archive/splash.html', 
    '/pages/search.html' => 'archive/pages/search.html' 
    '/pages/user.html' => 'archive/pages/user.html', 
} 

static_page_mappings.each do |req, file| 
    map req do 
    run Proc.new { |env| 
     [ 
     200, 
     { 
      'Content-Type' => 'text/html', 
      'Cache-Control' => 'public, max-age=6400', 
     }, 
     File.open(file, File::RDONLY) 
     ] 
    } 
    end 
end 
+0

Bu öneriyi denemediniz ama işe yarayabilir, teşekkürler! –

+0

Eric'in gönderisinde yer alan yorumuma yeniden bakın İçerik türü – rbp

İlgili konular