gpt4 book ai didi

ruby - 如何将 Auth::Basic 添加到简单的 Rack 应用程序

转载 作者:数据小太阳 更新时间:2023-10-29 08:59:33 25 4
gpt4 key购买 nike

我怎样才能添加这个

use Rack::Auth::Basic do |username, password|
username == 'pippo' && password == 'pluto'
end

对此

class HelloWorld
def call(env)
req = Rack::Request.new(env)
case req.path_info
when /badges/
[200, {"Content-Type" => "text/html"}, ['This is great !!!!']]
when /goodbye/
[500, {"Content-Type" => "text/html"}, ["Goodbye Cruel World!"]]
else
[404, {"Content-Type" => "text/html"}, ["I'm Lost!"]]
end
end
end


run HelloWorld.new

我有这个简单的 Rack 应用程序,我需要添加 Auth::Basic。

谢谢

最佳答案

您需要使用 Rack::Builder 来组合 Rack 应用程序。

例子:

# app.ru
require 'rack'

class HelloWorld
def call(env)
req = Rack::Request.new(env)
case req.path_info
when /badges/
[200, {"Content-Type" => "text/html"}, ['This is great !!!!']]
when /goodbye/
[500, {"Content-Type" => "text/html"}, ["Goodbye Cruel World!"]]
else
[404, {"Content-Type" => "text/html"}, ["I'm Lost!"]]
end
end
end

app = Rack::Builder.new do
use Rack::Auth::Basic do |username, password|
username == 'pippo' && password == 'pluto'
end

map '/' do
run HelloWorld.new
end
end

run app

启动它:

$ rackup app.ru

关于ruby - 如何将 Auth::Basic 添加到简单的 Rack 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39828876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com