How mix in routes in Sinatra for a better structure

前端 未结 5 873
庸人自扰
庸人自扰 2020-12-13 07:28

I found nothing about how I can mix-in routes from another module, like this:

module otherRoutes
  get \"/route1\" do

  end
end    

class Server < Sinat         


        
相关标签:
5条回答
  • 2020-12-13 07:30

    Just my two cents:

    my_app.rb:

    require 'sinatra/base'
    
    class MyApp < Sinatra::Base
      set :root, File.expand_path('../', __FILE__)
      set :app_file, __FILE__
      disable :run
    
      files_to_require = [
        "#{root}/app/helpers/**/*.{rb}",
        "#{root}/app/routes/**/*.{rb}"
      ]
    
      files_to_require.each {|path| Dir.glob(path, &method(:require))}
      helpers App::Helpers
    end
    

    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'
    
    0 讨论(0)
  • 2020-12-13 07:41

    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
    
    0 讨论(0)
  • 2020-12-13 07:47

    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 < Sinatra::Base
      register Sinatra::OtherRoutes
      ...
    end
    

    It's not really clear from the docs that this is the way to go for non-basic Sinatra apps. Hope it helps others.

    0 讨论(0)
  • 2020-12-13 07:49

    You could do this:

    module OtherRoutes
      def self.included( app )
        app.get "/route1" do
          ...
        end
      end
    end
    
    class Server < Sinatra::Base
      include OtherRoutes
      ...
    end
    

    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.

    0 讨论(0)
  • 2020-12-13 07:49

    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 < Sinatra::Base
          get '/' do
            'base route'
          end
    
          register OtherRoutes
        end
      end
    

    sinata-contrib is maintained alongside the sinatra project

    0 讨论(0)
提交回复
热议问题