Class: Sidekiq::WebApplication

Inherits:
Object
  • Object
show all
Extended by:
WebRouter
Defined in:
lib/sidekiq/web/application.rb

Constant Summary collapse

REDIS_KEYS =
%w[redis_version uptime_in_days connected_clients used_memory_human used_memory_peak_human]
CSP_HEADER =
[
  "default-src 'self' https: http:",
  "child-src 'self'",
  "connect-src 'self' https: http: wss: ws:",
  "font-src 'self' https: http:",
  "frame-src 'self'",
  "img-src 'self' https: http: data:",
  "manifest-src 'self'",
  "media-src 'self'",
  "object-src 'none'",
  "script-src 'self' https: http:",
  "style-src 'self' https: http: 'unsafe-inline'",
  "worker-src 'self'",
  "base-uri 'self'"
].join("; ").freeze
METRICS_PERIODS =
{
  "1h" => 60,
  "2h" => 120,
  "4h" => 240,
  "8h" => 480
}
QUEUE_NAME =
/\A[a-z_:.\-0-9]+\z/i

Constants included from WebRouter

Sidekiq::WebRouter::DELETE, Sidekiq::WebRouter::GET, Sidekiq::WebRouter::HEAD, Sidekiq::WebRouter::PATCH, Sidekiq::WebRouter::PATH_INFO, Sidekiq::WebRouter::POST, Sidekiq::WebRouter::PUT, Sidekiq::WebRouter::REQUEST_METHOD, Sidekiq::WebRouter::ROUTE_PARAMS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WebRouter

delete, get, head, match, patch, post, put, route

Constructor Details

#initialize(klass) ⇒ WebApplication

Returns a new instance of WebApplication.



30
31
32
# File 'lib/sidekiq/web/application.rb', line 30

def initialize(klass)
  @klass = klass
end

Class Method Details

.after(path = nil, &block) ⇒ Object



450
451
452
# File 'lib/sidekiq/web/application.rb', line 450

def self.after(path = nil, &block)
  afters << [path && Regexp.new("\\A#{path.gsub("*", ".*")}\\z"), block]
end

.aftersObject



471
472
473
# File 'lib/sidekiq/web/application.rb', line 471

def self.afters
  @afters ||= []
end

.before(path = nil, &block) ⇒ Object



446
447
448
# File 'lib/sidekiq/web/application.rb', line 446

def self.before(path = nil, &block)
  befores << [path && Regexp.new("\\A#{path.gsub("*", ".*")}\\z"), block]
end

.beforesObject



467
468
469
# File 'lib/sidekiq/web/application.rb', line 467

def self.befores
  @befores ||= []
end

.helpers(mod = nil, &block) ⇒ Object



438
439
440
441
442
443
444
# File 'lib/sidekiq/web/application.rb', line 438

def self.helpers(mod = nil, &block)
  if block
    WebAction.class_eval(&block)
  else
    WebAction.send(:include, mod)
  end
end

.run_afters(app, action) ⇒ Object



458
459
460
# File 'lib/sidekiq/web/application.rb', line 458

def self.run_afters(app, action)
  run_hooks(afters, app, action)
end

.run_befores(app, action) ⇒ Object



454
455
456
# File 'lib/sidekiq/web/application.rb', line 454

def self.run_befores(app, action)
  run_hooks(befores, app, action)
end

.run_hooks(hooks, app, action) ⇒ Object



462
463
464
465
# File 'lib/sidekiq/web/application.rb', line 462

def self.run_hooks(hooks, app, action)
  hooks.select { |p, _| !p || p =~ action.env[WebRouter::PATH_INFO] }
    .each { |_, b| action.instance_exec(action.env, app, &b) }
end

.set(key, val) ⇒ Object



46
47
48
# File 'lib/sidekiq/web/application.rb', line 46

def self.set(key, val)
  # nothing, backwards compatibility
end

.settingsObject



38
39
40
# File 'lib/sidekiq/web/application.rb', line 38

def self.settings
  Sidekiq::Web.settings
end

.tabsObject



42
43
44
# File 'lib/sidekiq/web/application.rb', line 42

def self.tabs
  Sidekiq::Web.tabs
end

Instance Method Details

#call(env) ⇒ Object



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/sidekiq/web/application.rb', line 409

def call(env)
  action = self.class.match(env)
  return [404, {Rack::CONTENT_TYPE => "text/plain", Web::X_CASCADE => "pass"}, ["Not Found"]] unless action

  app = @klass
  resp = catch(:halt) do
    self.class.run_befores(app, action)
    action.instance_exec env, &action.block
  ensure
    self.class.run_afters(app, action)
  end

  case resp
  when Array
    # redirects go here
    resp
  else
    # rendered content goes here
    headers = {
      Rack::CONTENT_TYPE => "text/html",
      Rack::CACHE_CONTROL => "private, no-store",
      Web::CONTENT_LANGUAGE => action.locale,
      Web::CONTENT_SECURITY_POLICY => CSP_HEADER
    }
    # we'll let Rack calculate Content-Length for us.
    [200, headers, [resp]]
  end
end

#settingsObject



34
35
36
# File 'lib/sidekiq/web/application.rb', line 34

def settings
  @klass.settings
end