How to setup URLs for static site with Ruby Rack on Heroku

╄→尐↘猪︶ㄣ 提交于 2019-11-29 02:49:00

问题


My site is here.

It used to be a Django-powered blog. However I no longer update it so I just wanted to make it a static HTML site. I wget'ed it and moved it to Heroku with Ruby Rack.

However every URL resolves to the home page. This is because of my config.ru file:

use Rack::Static, 
  :urls => ["/media/images", "/media/js", "/media/css"],
  :root => "public"

run lambda { |env|
  [
    200, 
    {
      'Content-Type'  => 'text/html', 
      'Cache-Control' => 'public, max-age=86400' 
    },
    File.open('public/index.html', File::RDONLY)
  ]
}

Question: Is there a way to map multiple URLs? e.g. foo.com/about maps to public/about/index.html, foo.com/posts/2012/oct/21/blog-postmaps to public/posts/2012/oct/21/blog-post/index.html

At this point I'd even be fine typing each one by hand.

Thanks for your help.


回答1:


For now I found the best answer to be:

use Rack::Static, 
  :urls => ["/media/images", "/media/js", "/media/css"],
  :root => "public"

map "/" do
  run lambda { |env|
  [
    200, 
    {
      'Content-Type'  => 'text/html', 
      'Cache-Control' => 'public, max-age=86400' 
    },
    File.open('public/index.html', File::RDONLY)
  ]
}
end

map "/portfolio" do
  run lambda { |env|
  [
    200, 
    {
      'Content-Type'  => 'text/html', 
      'Cache-Control' => 'public, max-age=86400' 
    },
    File.open('public/portfolio/index.html', File::RDONLY)
  ]
}
end

And map every URL to its respective file. Tedious, but works. See also the answer to this question regarding URL variables. Couldn't get it to work for me though.




回答2:


Why do you need the run statement? Maybe this works for you:

use Rack::Static, 
  :urls => ["/media/images", "/media/js", "/media/css"],
  :root => "public",
  :index => "index.html",
  :header_rules => [
    [:all, {'Cache-Control' => 'public, max-age=86400'}]
  ]

run lambda{ |env| [ 404, { 'Content-Type'  => 'text/html' }, ['404 - page not found'] ] }


来源:https://stackoverflow.com/questions/13206289/how-to-setup-urls-for-static-site-with-ruby-rack-on-heroku

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!