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: 'unsafe-inline'",
  "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



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



372
373
374
# File 'lib/sidekiq/web/application.rb', line 372

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

.aftersObject



393
394
395
# File 'lib/sidekiq/web/application.rb', line 393

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

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



368
369
370
# File 'lib/sidekiq/web/application.rb', line 368

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

.beforesObject



389
390
391
# File 'lib/sidekiq/web/application.rb', line 389

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

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



360
361
362
363
364
365
366
# File 'lib/sidekiq/web/application.rb', line 360

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

.run_afters(app, action) ⇒ Object



380
381
382
# File 'lib/sidekiq/web/application.rb', line 380

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

.run_befores(app, action) ⇒ Object



376
377
378
# File 'lib/sidekiq/web/application.rb', line 376

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

.run_hooks(hooks, app, action) ⇒ Object



384
385
386
387
# File 'lib/sidekiq/web/application.rb', line 384

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



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/sidekiq/web/application.rb', line 331

def call(env)
  action = self.class.match(env)
  return [404, {"content-type" => "text/plain", "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 = {
      "content-type" => "text/html",
      "cache-control" => "private, no-store",
      "content-language" => action.locale,
      "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