Class: Rack::Maintenance

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

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Maintenance

Returns a new instance of Maintenance.



3
4
5
6
# File 'lib/rack/maintenance.rb', line 3

def initialize(app, options={})
  @app = app
  @domain = options[:domain]
end

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rack/maintenance.rb', line 8

def call(env)
  case env["PATH_INFO"]
  when '/maintenance'
    env["MAINTENANCE"] = true
    @app.call(env)
  when "/private/#{ENV['MAINTENANCE_CODE']}"
    headers = {}
    Rack::Utils.set_cookie_header!(headers, :maintenance, {
      value: ENV['MAINTENANCE_CODE'],
      expires: 1.week.from_now,
      path: '/',
      domain: ".#{@domain}"
    })
    [302, headers.merge('Location' => '/'), ['Redirected to /']]
  else
    request = ActionDispatch::Request.new(env)
    if request.cookie_jar[:maintenance] == ENV['MAINTENANCE_CODE'] || env['HTTP_HOST'] == "admin.#{@domain}" || env['HTTP_HOST'] == "api.#{@domain}" || env['REQUEST_METHOD'] != 'GET'
      @app.call(env)
    else
      [302, {'Location' => '/maintenance'}, ['Redirected to /maintenance']]
    end
  end
end