Possible to read request's host in config.ru?

84 views
Skip to first unread message

armanx

unread,
Sep 22, 2012, 5:58:00 AM9/22/12
to rack-...@googlegroups.com
Is it possible to detect the incoming request's domain name (host) in config.ru? I'm trying to use URLMap to map different domains to different apps, all running on the same IP. Theoretically, I'm trying to achieve this effect:

map 'http://example1.com' do
run MyApp::Example1
end
 
 
map 'http://example2.com' do
run MyApp::Example2
end


For example, if it was possible to read rack's SERVER_NAME in config.ru, I would do something like:

if SERVER_NAME == 'example1.com'
map '/' do
run MyApp::Example1
end
end
 

Magnus Holm

unread,
Sep 22, 2012, 6:04:03 AM9/22/12
to rack-...@googlegroups.com
It's already supported, you just need the trailing slash:

map 'http://example1.com/' do
run MyApp::Example1
end

// Magnus Holm

Arman

unread,
Sep 22, 2012, 6:32:30 AM9/22/12
to rack-...@googlegroups.com

Thanks for the quick answer. This definitely works.

Our of curiosity, is it possible to see the host and other request information in config.ru?

Magnus Holm

unread,
Sep 22, 2012, 6:42:11 AM9/22/12
to rack-...@googlegroups.com
No, but this is what middleware are for:

class ApplicationChooser
def initialize(apps)
@apps = apps
end

def call(env)
@apps.each do |proc, app|
return app.call(env) if proc.call(env)
end
end

run ApplicationChooser.new(
proc { |env| env["SERVER_NAME"] == "example.com" } => App1,
proc { |env| env["QUERY_STRING"] == "admin" } => App2,
proc { |env| true } => Default
)

// Magnus Holm

armanx

unread,
Sep 24, 2012, 8:37:55 PM9/24/12
to rack-...@googlegroups.com
Thank you so much for prompt and useful explanation.
Reply all
Reply to author
Forward
0 new messages