我正在尝试构建一个使用机架中间件本身的机架中间件GEM (RackWired)。
我有一个现有的应用程序,它的config.ru使用Rack::Builder。在这个块(Rack::Builder)中,我想指定我的中间件,当它被调用时,在我自己的内部使用第三方中间件( middleware )来做一些事情。搞不懂我知道。
问题是rack::Builder的上下文在config.ru中,因此我的中间件(RackWired)无法访问它来“使用”第三方中间件( access )。
我努力的目的是这里
在中间件中有使用中间件的方法吗?
谢谢
发布于 2016-07-15 11:04:53
好吧,我不太清楚你想做什么。但你能做到的
class CorsWired
def initialize(app)
@app = app
end
def call(env)
cors = Rack::Cors.new(@app, {}) do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :put, :options, :delete], :credentials => false
end
end
cors.call(env)
end
end不过,您的config.ru应该有use CorsWired,而不是use CorsWired.new
这是我认为您要求的,但我认为您忽略了中间件的要点。您应该将您的config.ru更改为在中间件之前/之后使用after,这取决于您想要做什么。
require 'rack'
require 'rack/cors'
require './cors_wired'
app = Rack::Builder.new do
use Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :put, :options, :delete], :credentials => false
end
end
use CorsWired
run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['OK']] }
end
run apphttps://stackoverflow.com/questions/38391245
复制相似问题