Class: Sidekiq::CleanerApplication

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

Constant Summary collapse

CONTENT_LENGTH =
"Content-Length"
CONTENT_TYPE =
"Content-Type"
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

Constants included from CleanerRouter

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CleanerRouter

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

Constructor Details

#initialize(klass) ⇒ CleanerApplication

Returns a new instance of CleanerApplication.



26
27
28
# File 'lib/sidekiq/cleaner/application.rb', line 26

def initialize(klass)
  @klass = klass
end

Class Method Details

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



360
361
362
# File 'lib/sidekiq/cleaner/application.rb', line 360

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

.aftersObject



381
382
383
# File 'lib/sidekiq/cleaner/application.rb', line 381

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

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



356
357
358
# File 'lib/sidekiq/cleaner/application.rb', line 356

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

.beforesObject



377
378
379
# File 'lib/sidekiq/cleaner/application.rb', line 377

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

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



348
349
350
351
352
353
354
# File 'lib/sidekiq/cleaner/application.rb', line 348

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

.run_afters(app, action) ⇒ Object



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

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

.run_befores(app, action) ⇒ Object



364
365
366
# File 'lib/sidekiq/cleaner/application.rb', line 364

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

.run_hooks(hooks, app, action) ⇒ Object



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

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

.set(key, val) ⇒ Object



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

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

.settingsObject



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

def self.settings
  Sidekiq::Cleaner.settings
end

.tabsObject



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

def self.tabs
  Sidekiq::Cleaner.tabs
end

Instance Method Details

#call(env) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/sidekiq/cleaner/application.rb', line 311

def call(env)
  action = self.class.match(env)
  return [404, {"Content-Type" => "text/plain", "X-Cascade" => "pass" }, ["Not Found"]] unless action

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

    resp
  end

  resp = case resp
  when Array
    resp
  else
    headers = {
      "Content-Type" => "text/html",
      "Cache-Control" => "no-cache",
      "Content-Language" => action.locale,
      "Content-Security-Policy" => CSP_HEADER
    }

    [200, headers, [resp]]
  end

  resp[1] = resp[1].dup

  resp[1][CONTENT_LENGTH] = resp[2].inject(0) { |l, p| l + p.bytesize }.to_s

  resp
end

#settingsObject



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

def settings
  @klass.settings
end