Car*_*tem 5 ruby rack middleware argv
我似乎无法在机架中间件的预处理部分中附加到 ARGV。有人知道当中间件存在时,机架如何处理 ARGV 吗?如何在我的中间件中修改 ARGV?必须有办法做到这一点,我完全不知所措。
ARGV << "--debug"
ARGV << "--host" << "localhost"
class Pre
def initialize(app)
@app = app
end
def call(env)
if some_env_related_logic
ARGV << "--test"
end
@app.call(env)
end
end
require 'Somecommand'
use Pre
run Somecommand
Run Code Online (Sandbox Code Playgroud)
现在,“--test”参数不会添加到 Somecommand 中可用的 ARGV 中。
更新:
看来 Somecommand 应用程序在初始化方法中使用了 ARGV。这发生在第一个中间件之前。所以现在的问题是:如何创建一个调用第二个机架应用程序的机架应用程序?因为我需要在秒机架应用程序实例化之前评估 ENV。
请尝试以下操作:
class Pre
def initialize(app)
@app = app
end
def call(env)
# To be safe, reset the ARGV and rebuild, add any other items if needed
ARGV.clear
ARGV << "--debug"
ARGV << "--host" << "localhost"
if some_env_related_logic
ARGV << "--test"
end
Somecommand.new.call(env)
end
end
require 'Somecommand'
# Note the change, Somecommand is no longer mentioned here
run Pre
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1207 次 |
| 最近记录: |