Rack:你如何将 URL 存储为变量? [英] Rack: How do you store the URL as a variable?

查看:45
本文介绍了Rack:你如何将 URL 存储为变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的静态 Rack 应用程序.查看下面的 config.ru 代码:

I am writing a simple static Rack app. Check out the config.ru code below:

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

我想通过将 URL 存储为变量并创建一个显示

I'd like to simplify this by storing the URL as variable and creating one map section that says

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

如何正确设置这个 url 变量?

How can I correctly set this url variable?

推荐答案

你不应该需要地图部分.

You shouldn't need the map part.

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

这篇关于Rack:你如何将 URL 存储为变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆