Integrating Toto and a Sinatra app in 10 steps

Integrating a Sinatra and toto application is a little tricky for those unfamiliar with hosting multiple Rack applications in a single Rack instance. To integrate these two for AutoMapper.org, I followed similar instructions from the toto wiki:

  1. cd your_sinatra_app
  2. mkdir blog
  3. cd blog
  4. git clone git://github.com/cloudhead/dorothy.git .
  5. merge the contents of the dorothy .gems file with the .gems file in the root of your Sinatra application
  6. remove the files needed for a standalone toto install
    1. rm README
    2. rm Rakefile (you may want to move the toto rakefile to the root directory to take advantage of the “new” task for creating new posts.)
    3. rm config.ru
    4. rm .gems
    5. rm -rf public
    6. rm -rf .git
  7. cd .. (back to the root of your Sinatra app)
  8. create a Rack config file, config.ru, in your Sinatra root, if it doesn’t already exist
  9. Open config.ru in your favorite text editor and add the following:
    require 'toto'
    require './app'
    
    use Rack::ShowExceptions
    
    use Rack::CommonLogger
    
    #run the toto application
    
    toto = Toto::Server.new do
    
      #override the default location for the toto directories
      Toto::Paths = {
        :templates => "blog/templates",
        :pages => "blog/templates/pages",
        :articles => "blog/articles"
      }
    
      #set your config variables here
      set :title, "Your Blog Title"
      set :date, lambda {|now| now.strftime("%B #{now.day.ordinal} %Y") }
      set :summary,   :max => 500
      set :root, "blog"
      set :url, "http://your-blog.heroku.com/blog/"
    
    end
    
    #create a rack app
    
    app = Rack::Builder.new do
    
      use Rack::CommonLogger
    
      #map requests to /blog to toto
      map '/blog' do
      	run toto
      end
    
      #map all the other requests to sinatra
      map '/' do
        run SinatraApp
      end
    end.to_app
    
    run app
  10. Open your Sinatra app and wrap any Sinatra code with a class “SinatraApp” that inherits from Sinatra::Base:
    require 'rubygems'
    require 'sinatra'
    
    class SinatraApp < Sinatra::Base
      get '/' do
        "I love lamp"
      end
    end

Now test your code with thin or any other rack-enabled test web server and push! A full working application can be found at my github.

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Jimmy Bogard

I'm a technical architect with Headspring in Austin, TX. I focus on DDD, distributed systems, and any other acronym-centric design/architecture/methodology. I created AutoMapper and am a co-author of the ASP.NET MVC in Action books.
This entry was posted in Ruby, Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Jason Hooten

    Seeing how easy it was to set up made me switch my blog from mvc3 based to ruby. Very interesting series.