Sinatra 机架中间件劫持“/"根 URL [英] Sinatra rack middleware hijacks '/' root url

查看:43
本文介绍了Sinatra 机架中间件劫持“/"根 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Rails 应用中使用 Sinatra 应用作为中间件.

I'm trying to use a Sinatra app as middleware in my Rails app.

我已经在 Rails 应用程序的 /lib 文件夹中测试了一个基本的 Sinatra 应用程序,used 中间件并设置了一个路由.效果很好.

I've tested a basic Sinatra app in the /lib folder of the Rails app, used the middleware and set a route. That worked fine.

我想要做的是提取 Sinatra 应用程序并将其作为 gem 包含在内.这样我就可以独立运行 Sinatra 应用程序,或者在多个 Rails 应用程序中使用它.

What I want to be able to do is extract the Sinatra app and include it as a gem. That way I can run the Sinatra app independently, or use it in multiple Rails apps.

Sinatra 应用

# myrackapp/lib/myrackapp.rb
module Myrackapp
  class Application < Sinatra::Base

    set :root, File.dirname(__FILE__)

    get "/" do
      "Rack Home"
    end

    get '/rackroute' do
      "Hello, Rack Page"
    end

    end
end

Myrackapp 也有一个 gemspec - 没有什么有趣的,但如果需要我可以发布.

Myrackapp also has a gemspec – nothing interesting there, but I can post if necessary.

Rails 应用

# Gemfile
gem 'myrackapp', path: "/Users/gareth/Code/myrackapp"

-

# config/application.rb
module Myrailsapp
  class Application < Rails::Application
    ...
    config.middleware.use "Myrackapp::Application"
  end
end

-

# config.routes.rb
root :to => 'pages#show', :id => 'home'
mount Myrackapp::Application => "/rackapp"

这是我的 rake 中间件输出:

    rake middleware
    use ActionDispatch::Static
    use Rack::Lock
    use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x141ded4>
    use Rack::Runtime
    use Rack::MethodOverride
    use ActionDispatch::RequestId
    use Rails::Rack::Logger
    use ActionDispatch::ShowExceptions
    use ActionDispatch::DebugExceptions
    use ActionDispatch::RemoteIp
    use ActionDispatch::Reloader
    use ActionDispatch::Callbacks
    use ActiveRecord::ConnectionAdapters::ConnectionManagement
    use ActiveRecord::QueryCache
    use ActionDispatch::Cookies
    use ActionDispatch::Session::CookieStore
    use ActionDispatch::Flash
    use ActionDispatch::ParamsParser
    use ActionDispatch::Head
    use Rack::ConditionalGet
    use Rack::ETag
    use ActionDispatch::BestStandardsSupport
    use Myrackapp::Application
    run Myrailsapp::Application.routes

当我去 http://myapp.dev/rackapp 我得到 Myrackapp 的根路径 - 正确的行为

When I go to http://myapp.dev/rackapp I get Myrackapp's root path - correct behaviour

当我去 http://myapp.dev/rackapp/rackroute 我得到 Myrackapp/rackroute 路径 - 再次,正确的行为

When I go to http://myapp.dev/rackapp/rackroute I get Myrackapp's /rackroute path - again, correct behaviour

当我在浏览器中访问 http://myapp.dev 时,我会被定向到 Myrackapp 的根路径.

When I go to http://myapp.dev in the browser I get directed to the Myrackapp's root path.

当我将 Sinatra 应用程序直接包含在我的 Rails 应用程序中时,访问 http://myapp.dev 呈现了正确的 pages#show 操作.

When I included the Sinatra app directly in my Rails app visiting http://myapp.dev rendered the correct pages#show action.

我该怎么做才能让 Sinatra 应用不劫持 Rails 的根路径?

What can I do to get the Sinatra app to not hijack the root path of Rails?

推荐答案

您实际上并不需要将 Sinatra 应用程序作为中间件来执行您想要的操作.

You don't actually need to include the Sinatra app as middleware to do what you want.

将它包含为中间件意味着所有请求都通过它路由,您不希望/不需要它来提供路由.

Including it as middleware will mean that all requests are routed through it, which you don't want/need in order to make it supply the routes.

如果您想在 Rails 应用程序中包含 gem 时自动添加路由,您可以添加一个向应用程序添加路由的 railtie.我一时想不起来那是什么样子,但应该很简单.

If you want to add the routes automatically when you include the gem in a rails app you could add a railtie that adds routes to the application. I can't remember off the top of my head what that looks like, but it should be pretty straightforward.

这篇关于Sinatra 机架中间件劫持“/"根 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆