首页 > 解决方案 > 如何在 Sinatra 项目中添加额外的路由?

问题描述

我怎样才能prepend在另一个 gem 的 Sinatra 类中增加一条额外的路线?

我怎么样了

module StealthWebhook
  module Route
    post '/api/v1/webhook' do
      response_status, response_body = Webhook.send_request(request)

      status response_status
      body response_body
    end
  end
end


module Stealth
  class Server < Sinatra::Base
    prepend StealthWebhook::Route
  end
end

我需要在这个类中添加这条路线

但我收到此错误:

bundler: failed to load command: puma (/usr/local/bundle/bin/puma)
bot_1       | NoMethodError: undefined method `post' for StealthWebhook::Route:Module
bot_1       |   /bot/lib/stealth-webhook/lib/stealth-webhook/route.rb:6:in `<module:Route>'
bot_1       |   /bot/lib/stealth-webhook/lib/stealth-webhook/route.rb:4:in `<module:StealthWebhook>'
bot_1       |   /bot/lib/stealth-webhook/lib/stealth-webhook/route.rb:3:in `<top (required)>'
bot_1       |   /bot/lib/stealth-webhook/lib/stealth-webhook/base.rb:5:in `require_relative'
bot_1       |   /bot/lib/stealth-webhook/lib/stealth-webhook/base.rb:5:in `<top (required)>'
bot_1       |   /bot/lib/stealth-webhook/lib/stealth-webhook.rb:1:in `require_relative'
bot_1       |   /bot/lib/stealth-webhook/lib/stealth-webhook.rb:1:in `<top (required)>'
bot_1       |   /bot/lib/aggregator.rb:5:in `require_relative'
bot_1       |   /bot/lib/aggregator.rb:5:in `<top (required)>'
bot_1       |   /bot/config/initializers/overridings.rb:1:in `require_relative'
bot_1       |   /bot/config/initializers/overridings.rb:1:in `<top (required)>'
bot_1       |   /usr/local/bundle/gems/stealth-1.0.4/lib/stealth/base.rb:85:in `require_relative'
bot_1       |   /usr/local/bundle/gems/stealth-1.0.4/lib/stealth/base.rb:85:in `block in require_directory'
bot_1       |   /usr/local/bundle/gems/stealth-1.0.4/lib/stealth/base.rb:93:in `each'
bot_1       |   /usr/local/bundle/gems/stealth-1.0.4/lib/stealth/base.rb:93:in `for_each_file_in'
bot_1       |   /usr/local/bundle/gems/stealth-1.0.4/lib/stealth/base.rb:85:in `require_directory'
bot_1       |   /usr/local/bundle/gems/stealth-1.0.4/lib/stealth/base.rb:66:in `load_environment'
bot_1       |   /usr/local/bundle/gems/stealth-1.0.4/lib/stealth/base.rb:33:in `boot'
bot_1       |   /bot/config/boot.rb:6:in `<top (required)>'
bot_1       |   config.ru:2:in `require_relative'
bot_1       |   config.ru:2:in `block in <main>'
bot_1       |   /usr/local/bundle/gems/rack-2.0.5/lib/rack/builder.rb:55:in `instance_eval'
bot_1       |   /usr/local/bundle/gems/rack-2.0.5/lib/rack/builder.rb:55:in `initialize'
bot_1       |   config.ru:in `new'
bot_1       |   config.ru:in `<main>'
bot_1       |   /usr/local/bundle/gems/rack-2.0.5/lib/rack/builder.rb:49:in `eval'
bot_1       |   /usr/local/bundle/gems/rack-2.0.5/lib/rack/builder.rb:49:in `new_from_string'
bot_1       |   /usr/local/bundle/gems/rack-2.0.5/lib/rack/builder.rb:40:in `parse_file'
bot_1       |   /usr/local/bundle/gems/puma-3.12.0/lib/puma/configuration.rb:318:in `load_rackup'
bot_1       |   /usr/local/bundle/gems/puma-3.12.0/lib/puma/configuration.rb:243:in `app'
bot_1       |   /usr/local/bundle/gems/puma-3.12.0/lib/puma/runner.rb:145:in `load_and_bind'
bot_1       |   /usr/local/bundle/gems/puma-3.12.0/lib/puma/single.rb:96:in `run'
bot_1       |   /usr/local/bundle/gems/puma-3.12.0/lib/puma/launcher.rb:184:in `run'
bot_1       |   /usr/local/bundle/gems/puma-3.12.0/lib/puma/cli.rb:78:in `run'
bot_1       |   /usr/local/bundle/gems/puma-3.12.0/bin/puma:10:in `<top (required)>'
bot_1       |   /usr/local/bundle/bin/puma:29:in `load'
bot_1       |   /usr/local/bundle/bin/puma:29:in `<top (required)>'

编辑:

我解决了使用Stealth::Server.post. 这是最好的方法吗?

module StealthWebhook
  module Route
    Stealth::Server.post '/api/v1/webhook' do
      response_status, response_body = Webhook.send_request(request)

      status response_status
      body response_body
    end
  end
end


module Stealth
  class Server < Sinatra::Base
    prepend StealthWebhook::Route
  end
end

标签: rubysinatra

解决方案


推荐阅读