Class: Sidekiq::Web::Application

Inherits:
Object
  • Object
show all
Extended by:
Router
Includes:
Router
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 'none'",
  "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' 'nonce-!placeholder!'",
  "worker-src 'self'",
  "base-uri 'self'"
].join("; ").freeze
METRICS_PERIODS =
{
  "1h" => {minutes: 60},
  "2h" => {minutes: 120},
  "4h" => {minutes: 240},
  "8h" => {minutes: 480},
  "24h" => {hours: 24},
  "48h" => {hours: 48},
  "72h" => {hours: 72}
}
QUEUE_NAME =
/\A[a-z_:.\-0-9]+\z/i

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Router

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

Constructor Details

#initialize(inst) ⇒ Application

Returns a new instance of Application.



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

def initialize(inst)
  @app = inst
end

Class Method Details

.helpers(mod) ⇒ Object

Used by extensions to add helper methods accessible to any defined endpoints in Application. Careful with generic method naming as there’s no namespacing so collisions are possible.



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

def self.helpers(mod)
  Sidekiq::Web::Action.send(:include, mod)
end

Instance Method Details

#call(env) ⇒ Object



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
437
438
439
440
# File 'lib/sidekiq/web/application.rb', line 412

def call(env)
  action = match(env)
  return [404, {"content-type" => "text/plain", "x-cascade" => "pass"}, ["Not Found"]] unless action

  headers = {
    "content-type" => "text/html",
    "cache-control" => "private, no-store",
    "content-language" => action.locale,
    "content-security-policy" => process_csp(env, CSP_HEADER_TEMPLATE),
    "x-content-type-options" => "nosniff"
  }
  env["response_headers"] = headers
  resp = catch(:halt) do
    Thread.current[:sidekiq_redis_pool] = env[:redis_pool]
    action.instance_exec env, &action.block
  ensure
    Thread.current[:sidekiq_redis_pool] = nil
  end

  case resp
  when Array
    # redirects go here
    resp
  else
    # rendered content goes here
    # we'll let Rack calculate Content-Length for us.
    [200, env["response_headers"], [resp]]
  end
end

#process_csp(env, input) ⇒ Object



442
443
444
# File 'lib/sidekiq/web/application.rb', line 442

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

#redisObject



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

def redis(&)
  Thread.current[:sidekiq_redis_pool].with(&)
end