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
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



24
25
26
# File 'lib/sidekiq/web/application.rb', line 24

def initialize(klass)
  @klass = klass
end

Class Method Details

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



341
342
343
# File 'lib/sidekiq/web/application.rb', line 341

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

.aftersObject



362
363
364
# File 'lib/sidekiq/web/application.rb', line 362

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

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



337
338
339
# File 'lib/sidekiq/web/application.rb', line 337

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

.beforesObject



358
359
360
# File 'lib/sidekiq/web/application.rb', line 358

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

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



329
330
331
332
333
334
335
# File 'lib/sidekiq/web/application.rb', line 329

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

.run_afters(app, action) ⇒ Object



349
350
351
# File 'lib/sidekiq/web/application.rb', line 349

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

.run_befores(app, action) ⇒ Object



345
346
347
# File 'lib/sidekiq/web/application.rb', line 345

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

.run_hooks(hooks, app, action) ⇒ Object



353
354
355
356
# File 'lib/sidekiq/web/application.rb', line 353

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



40
41
42
# File 'lib/sidekiq/web/application.rb', line 40

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

.settingsObject



32
33
34
# File 'lib/sidekiq/web/application.rb', line 32

def self.settings
  Sidekiq::Web.settings
end

.tabsObject



36
37
38
# File 'lib/sidekiq/web/application.rb', line 36

def self.tabs
  Sidekiq::Web.tabs
end

Instance Method Details

#call(env) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/sidekiq/web/application.rb', line 300

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



28
29
30
# File 'lib/sidekiq/web/application.rb', line 28

def settings
  @klass.settings
end