Class: Rack::DomainRedirect

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/domain_redirect.rb

Overview

Automatically redirects to the configurable domain

If request comes from other than specified domains it redirects to the first domain from the list

Instance Method Summary collapse

Constructor Details

#initialize(app, hosts = []) ⇒ DomainRedirect

Returns a new instance of DomainRedirect.



9
10
11
12
# File 'lib/rack/domain_redirect.rb', line 9

def initialize(app, hosts = [])
  @app = app
  @hosts = hosts
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rack/domain_redirect.rb', line 14

def call(env)
  req = Rack::Request.new(env)
  
  if @hosts.nil? or @hosts.empty? or @hosts.include?(req.host)
    @app.call(env)
  else
    url = "http://#{@hosts[0]}"
    # url << ":#{req.port}" unless req.port == 80
    url << "#{req.path}"
    url << "?#{req.query_string}" unless req.query_string.empty?
    res = Rack::Response.new
    res.redirect(url)
    res.finish
  end
end