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_TEMPLATE =
[
  "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' 'nonce-!placeholder!'",
  "style-src 'self' https: http: 'unsafe-inline'", # TODO Nonce in 8.0
  "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



411
412
413
# File 'lib/sidekiq/web/application.rb', line 411

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

.aftersObject



432
433
434
# File 'lib/sidekiq/web/application.rb', line 432

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

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



407
408
409
# File 'lib/sidekiq/web/application.rb', line 407

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

.beforesObject



428
429
430
# File 'lib/sidekiq/web/application.rb', line 428

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

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



399
400
401
402
403
404
405
# File 'lib/sidekiq/web/application.rb', line 399

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

.run_afters(app, action) ⇒ Object



419
420
421
# File 'lib/sidekiq/web/application.rb', line 419

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

.run_befores(app, action) ⇒ Object



415
416
417
# File 'lib/sidekiq/web/application.rb', line 415

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

.run_hooks(hooks, app, action) ⇒ Object



423
424
425
426
# File 'lib/sidekiq/web/application.rb', line 423

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



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/sidekiq/web/application.rb', line 365

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 => process_csp(env, CSP_HEADER_TEMPLATE),
      Web::X_CONTENT_TYPE_OPTIONS => "nosniff"
    }
    # we'll let Rack calculate Content-Length for us.
    [200, headers, [resp]]
  end
end

#process_csp(env, input) ⇒ Object



395
396
397
# File 'lib/sidekiq/web/application.rb', line 395

def process_csp(env, input)
  input.gsub("!placeholder!", env[:csp_nonce])
end

#settingsObject



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

def settings
  @klass.settings
end