I found nothing about how I can mix-in routes from another module, like this:
module otherRoutes get "/route1" do end end class Server Is that possible?
I found nothing about how I can mix-in routes from another module, like this:
module otherRoutes get "/route1" do end end class Server Is that possible?
You don't do include with Sinatra. You use extensions together with register.
I.e. build your module in a separate file:
require 'sinatra/base' module Sinatra module OtherRoutes def self.registered(app) app.get "/route1" do ... end end end register OtherRoutes # for non modular apps, just include this file and it will register end And then register:
class Server It's not really clear from the docs that this is the way to go for non-basic Sinatra apps. Hope it helps others.
You could do this:
module OtherRoutes def self.included( app ) app.get "/route1" do ... end end end class Server Unlike Ramaze, Sinatra's routes are not methods, and so cannot use Ruby's method lookup chaining directly. Note that with this you can't later monkey-patch OtherRoutes and have the changes reflected in Server; this is just a one-time convenience for defining the routes.
Well you can also use the map method to map routes to your sinatra apps
map "/" do run Rack::Directory.new("./public") end map '/posts' do run PostsApp.new end map '/comments' do run CommentsApp.new end map '/users' do run UserssApp.new end Just my two cents:
my_app.rb:
require 'sinatra/base' class MyApp app/routes/health.rb:
MyApp.configure do |c| c.before do content_type "application/json" end c.get "/health" do { Ruby: "#{RUBY_VERSION}", Rack: "#{Rack::VERSION}", Sinatra: "#{Sinatra::VERSION}" }.to_json end end app/helpers/application.rb:
module App module Helpers def t(*args) ::I18n::t(*args) end def h(text) Rack::Utils.escape_html(text) end end end config.ru:
require './my_app.rb' I prefer the use of sinatra-contrib gem to extend sinatra for cleaner syntax and shared namespace
# Gemfile gem 'sinatra', '~> 1.4.7' gem 'sinatra-contrib', '~> 1.4.6', require: 'sinatra/extension' # other_routes.rb module Foo module OtherRoutes extend Sinatra::Extension get '/some-other-route' do 'some other route' end end end # app.rb module Foo class BaseRoutes sinata-contrib is maintained alongside the sinatra project