修改机架应用 [英] Modify Rack App
问题描述
对于我的一个ruby应用程序,我需要服务器根据子域来路由请求。有一些方法可以使用其他宝石来做到这一点,但我决定制作自己的中间件。此代码根据请求的运行位置来运行应用程序。
config.ru
require'./subdomain'
require'./www'
run Rack :: Subdomain.new([
{
:subdomain = >test,
:application => Sinatra :: Application
}
]);
subdomain.rb
模块机架
类子域
def初始化(app)
@app = app
结束
def call(env)
@ app.each do | app |
match ='test'.match(app [:subdomain])
if match b $ b return app [:application] .call(env)
end
end
end
end
end
我的问题是如何修改这个工作代码工作完全一样,但它看起来像这样的代码调用:
run Rack :: Subdomain do
map'www'do
示例:: WWW
结束
map'api'do
示例:: API
结束
结束
使用建议的代码:
config.ru
require'./subdomain'
require'./www'
运行Rack :: Subdomain.new do | x |
x.map'test'do
Sinatra :: Application
end
end
subdomain.rb
module Rack
class子域
def initialize(路线=零)
@路由=路由
产生自己
结束
def map(subdomain)
@routes<< {subdomain:subdomain,application:yield}
end
def call(env)
@ routes.each do | route |
match ='test'.match(route [:subdomain])
if match b $ b return route [:application] .call(env)
end
end
end
end
end
你调用上面的工作代码,但它似乎根本没有检测到子域,但将它连接到文字'测试'。无论如何,您可以通过创建一个地图方法来实现一个类似于您想要的模式,该方法将条目添加到您的子域名>应用程序路由列表中。我已将 @app
重命名为 @routes
,因为它是路由的哈希,而不是应用程序引用。 / b>
module Rack
class子域
def初始化(routes = [])
@routes =路线
产生自我,如果block_given?
end
def map(subdomain)
@routes<< {subdomain:subdomain,application:yield}
end
def call(env)
@ routes.each do | route |
match ='test'.match(route [:subdomain])
if match b $ b return route [:application] .call(env)
end
end
结束
结束
结束
rsd = Rack :: Subdomain.new do | x |
x.map'www'do
示例:: WWW
end
$ b x.map'api'do
示例:: API
end
end
run rsd
For one of my ruby applications i need the server to route requests based on the subdomain. There are ways to do this using other gems but i decided to make my own "middleware". This code runs applications based on where the request is going to.
config.ru
require './subdomain'
require './www'
run Rack::Subdomain.new([
{
:subdomain => "test",
:application => Sinatra::Application
}
]);
subdomain.rb
module Rack
class Subdomain
def initialize(app)
@app = app
end
def call(env)
@app.each do |app|
match = 'test'.match(app[:subdomain])
if match
return app[:application].call(env)
end
end
end
end
end
My question is how can i modify this working code to work exactly the same but have it called by code that looks like this:
run Rack::Subdomain do
map 'www' do
Example::WWW
end
map 'api' do
Example::API
end
end
With suggested code:
config.ru
require './subdomain'
require './www'
run Rack::Subdomain.new do |x|
x.map 'test' do
Sinatra::Application
end
end
subdomain.rb
module Rack
class Subdomain
def initialize(routes = nil)
@routes = routes
yield self
end
def map(subdomain)
@routes << { subdomain: subdomain, application: yield }
end
def call(env)
@routes.each do |route|
match = 'test'.match(route[:subdomain])
if match
return route[:application].call(env)
end
end
end
end
end
You call the above "working code" but it doesn't seem to detect the subdomain at all, but wires it to the literal 'test'. At any rate, you can implement a pattern similar to what you want by making a map method which adds entries to your list of subdomain->application routes. I've renamed your @app
to @routes
since it is a hash of routes, not an application reference.
module Rack
class Subdomain
def initialize(routes = [])
@routes = routes
yield self if block_given?
end
def map(subdomain)
@routes << { subdomain: subdomain, application: yield }
end
def call(env)
@routes.each do |route|
match = 'test'.match(route[:subdomain])
if match
return route[:application].call(env)
end
end
end
end
end
rsd = Rack::Subdomain.new do |x|
x.map 'www' do
Example::WWW
end
x.map 'api' do
Example::API
end
end
run rsd
这篇关于修改机架应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!