Search code examples
rubyrack

How to write config.ru for Rack static site?


My config.ru

require 'rack'
use Rack::Static, :root => '_site'

But when I run rackup I get an error

/usr/local/share/gems/gems/rack-1.5.2/lib/rack/builder.rb:133:in `to_app': missing run or map statement (RuntimeError)

I want to the host the files in the folder _site_ at the root URL


Solution

  • The problem with your config.ru is that it's missing a run command, and you always need one. As matt suggested, you could use a Rack::File middleware.

    But if you want to keep the Rack::Static capabilities, then you could do something like this that serves an index file and have the Rack::Static middleware in front of it, to serve any file from _site_.

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