Question

I've been trying to consolidate two tutorials for hosting static sites:

Basically I want to be able to do what you do in the old bamboo tutorial on the cedar stack. I can get the cedar tutorial to work, but when I try modify it to serve files I get an error.

I have had a look at the following post, but the github repo doesn't seem to be up any longer and I can't quite figure out where I've gone wrong.

When I try run the site locally as per the first tutorial, I get the following error:

martin@crunchbang:~/code/martinrichards.me$ rackup -p 9292 config.ru &
[2] 4065
martin@crunchbang:~/code/martinrichards.me$ /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/builder.rb:40:in `eval':     /home/martin/code/martinrichards.me/config.ru:2: syntax error, unexpected tIDENTIFIER, expecting '}' (SyntaxError)
200, ... |env|"
...

I've put what I've got so far on Github, any help will be much appreciated.

Was it helpful?

Solution

Well, first, your config.ru is almost completely empty. Looks like you're following the same site structure as the Heroku tutorial, so start with a config.ru like this:

use Rack::Static, 
  :urls => ["/stylesheets", "/images"],
  :root => "public"

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

Since you are on Cedar, it is helpful to use a Procfile to start up your processes. So add a file called Procfile (no extension) to your root, and put the following inside it:

web: bundle exec rackup config.ru -p $PORT

That should do it.

If you want to use Sinatra, Heroku has a step-by-step tuturioal for Ruby sites on Cedar: https://devcenter.heroku.com/articles/ruby

EDIT:

As it turns out, you're having line-endings problems in your config.ru. That's why GitHub is not displaying the file correctly. Your line endings are ^M, which are DOS/Windows/Mac OS 9 line-endings. That's why Ruby is throwing an error on line 2 - it's the first line break. Not sure what text-editor you are using, but it probably supports changing line endings. Switch them Unix, and all should work fine. If you're looking for a text editor that can do this, check out Sublime Text 2. The line-endings functionality is in the "View" menu.

Since you are building a pure Rack app, you actually don't need a Procfile, since the default Heroku Cedar buildpack will detect the config.ru for you. However, the Procfile comes in handy once you start using other frameworks (like Sinatra). Plus, if you are on a Mac, you can use Foreman to simulate Heroku's spin-up process. Note that Profile is without an extension and with a capital "P".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top