require 'sinatra/base'
# The controller for the home page.
class HomeController < ::Sinatra::Base
get '/' do # <-- (1) slash
"This is the home controller."
end
end
# The controller for the documents section.
class DocumentsController < ::Sinatra::Base
get '/' do # <-- (2) no slash
"This is the documents controller."
end
end
# The main application. It aggregates the different controllers using Rack::URLMap.
class Application
def initialize
@app = ::Rack::URLMap.new(
'/' => HomeController.new,
'/documents' => DocumentsController.new
)
end
def call(env)
@app.call(env)
end
end
run Application.new
# 1. Load http://localhost:9292/
# You’ll see the home controller message.
#
# 2. Load http://localhost:9292/documents
# You’ll see the documents controller message.
#
# 3. Change "get ''" to "get '/'" and load http://localhost:9292/documents
# You’ll get a 404 error.