Class: MaintenanceMiddleware

Inherits:
Object
  • Object
show all
Defined in:
app/middleware/maintenance_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ MaintenanceMiddleware

Returns a new instance of MaintenanceMiddleware.



2
3
4
# File 'app/middleware/maintenance_middleware.rb', line 2

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/middleware/maintenance_middleware.rb', line 6

def call(env)
  # Reload config to ensure instant mode change without server restart
  config = JSON.parse(File.read(File.join(APP_ROOT, 'config', 'features.json'))) rescue (defined?(FEATURES_CONFIG) ? FEATURES_CONFIG : {})
  
  if config['maintenance']
    path = env['PATH_INFO']
    
    # Allow access to login, static assets, and CMS dashboard (if admin)
    # This allows admins to turn off maintenance mode via the dashboard
    allowed_paths = ['/login', '/logout', '/images', '/css', '/js', '/favicon.ico']
    is_allowed = allowed_paths.any? { |p| path.start_with?(p) }
    
    # Also allow if already logged in (admin access)
    session = env['eks_cent.session'] || env['rack.session'] || {}
    is_admin = session['user_id'] || session[:user_id]
    
    if !is_allowed && !is_admin
      # Render maintenance page
      res = EksCent::Response.new
      res.render('maintenance', layout: false)
      return [503, res.headers, res.body]
    end
  end

  @app.call(env)
end