How mix in routes in Sinatra for a better structure

匿名 (未验证) 提交于 2019-12-03 01:27:01

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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 


回答4:

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' 


回答5:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!