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, I followed similar instructions from the toto wiki:

  1. cd your_sinatra_app
  2. mkdir blog
  3. cd blog
  4. git clone 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
    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,, in your Sinatra root, if it doesn’t already exist
  9. Open in your favorite text editor and add the following:
    require 'toto'
    require './app'
    use Rack::ShowExceptions
    use Rack::CommonLogger
    #run the toto application
    toto = 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 #{} %Y") }
      set :summary,   :max => 500
      set :root, "blog"
      set :url, ""
    #create a rack app
    app = do
      use Rack::CommonLogger
      #map requests to /blog to toto
      map '/blog' do
      	run toto
      #map all the other requests to sinatra
      map '/' do
        run SinatraApp
    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"

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.

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. 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.