Skip to content

Instantly share code, notes, and snippets.

@stereobooster
Last active May 26, 2023 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stereobooster/985cdbd494688e7b50551d2d62aaae1b to your computer and use it in GitHub Desktop.
Save stereobooster/985cdbd494688e7b50551d2d62aaae1b to your computer and use it in GitHub Desktop.
Sinatra namespace example
bundle exec rackup config.ru
require 'sinatra/base'
class Application < Sinatra::Base
get "/" do
"Hello World!"
end
end
require_relative 'root'
run Root
# frozen_string_literal: true
source "https://rubygems.org"
gem "sinatra", "~> 1.4"
gem "thin"
gem "sinatra-router"
gem "sinatra-contrib"
GEM
remote: https://rubygems.org/
specs:
backports (3.8.0)
daemons (1.2.4)
eventmachine (1.2.3)
multi_json (1.12.1)
rack (1.6.8)
rack-protection (1.5.3)
rack
rack-test (0.6.3)
rack (>= 1.0)
sinatra (1.4.8)
rack (~> 1.5)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
sinatra-contrib (1.4.7)
backports (>= 2.0)
multi_json
rack-protection
rack-test
sinatra (~> 1.4.0)
tilt (>= 1.3, < 3)
sinatra-router (0.2.4)
sinatra (>= 1.4, < 3.0)
thin (1.7.0)
daemons (~> 1.0, >= 1.0.9)
eventmachine (~> 1.0, >= 1.0.4)
rack (>= 1, < 3)
tilt (2.0.7)
PLATFORMS
ruby
DEPENDENCIES
sinatra (~> 1.4)
sinatra-contrib
sinatra-router
thin
BUNDLED WITH
1.14.6
require 'sinatra/base'
require 'sinatra/namespace'
class NamespaceApp1 < Sinatra::Base
register Sinatra::Namespace
namespace "/v1" do
get "/" do
"Hello World!"
end
end
end
require 'sinatra/base'
require 'sinatra/namespace'
class NamespaceApp2 < Sinatra::Base
register Sinatra::Namespace
namespace "/v2" do
get "/" do
"Hello World!"
end
end
end
require_relative 'application'
require_relative 'namespace_app1'
require_relative 'namespace_app2'
require 'sinatra/router'
require 'sinatra/base'
class Root < ::Sinatra::Base
use ::Sinatra::Router do
mount NamespaceApp1
mount NamespaceApp2
mount Application
end
end
# Also can do it this way
# Root = Sinatra::Router.new do
# mount NamespaceApp1
# mount NamespaceApp2
# mount Application
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment