Class: Sidekiq::Web

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/web.rb,
lib/sidekiq/web/csrf_protection.rb

Defined Under Namespace

Classes: CsrfProtection

Constant Summary collapse

ROOT =
File.expand_path("#{File.dirname(__FILE__)}/../../web")
VIEWS =
"#{ROOT}/views"
LOCALES =
["#{ROOT}/locales"]
LAYOUT =
"#{VIEWS}/layout.erb"
ASSETS =
"#{ROOT}/assets"
DEFAULT_TABS =
{
  "Dashboard" => "",
  "Busy" => "busy",
  "Queues" => "queues",
  "Retries" => "retries",
  "Scheduled" => "scheduled",
  "Dead" => "morgue",
  "Metrics" => "metrics"
}
CONTENT_LANGUAGE =
"content-language"
CONTENT_SECURITY_POLICY =
"content-security-policy"
LOCATION =
"location"
X_CASCADE =
"x-cascade"
X_CONTENT_TYPE_OPTIONS =
"x-content-type-options"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.app_urlObject

Returns the value of attribute app_url.



98
99
100
# File 'lib/sidekiq/web.rb', line 98

def app_url
  @app_url
end

.localesObject



70
71
72
# File 'lib/sidekiq/web.rb', line 70

def locales
  @locales ||= LOCALES
end

.redis_poolObject

Returns the value of attribute redis_pool.



98
99
100
# File 'lib/sidekiq/web.rb', line 98

def redis_pool
  @redis_pool
end

.viewsObject



74
75
76
# File 'lib/sidekiq/web.rb', line 74

def views
  @views ||= VIEWS
end

Class Method Details

.call(env) ⇒ Object



124
125
126
127
# File 'lib/sidekiq/web.rb', line 124

def self.call(env)
  @app ||= new
  @app.call(env)
end

.custom_job_info_rowsObject



66
67
68
# File 'lib/sidekiq/web.rb', line 66

def custom_job_info_rows
  @custom_job_info_rows ||= []
end

.custom_tabsObject Also known as: tabs



61
62
63
# File 'lib/sidekiq/web.rb', line 61

def custom_tabs
  @custom_tabs ||= {}
end

.default_tabsObject



57
58
59
# File 'lib/sidekiq/web.rb', line 57

def default_tabs
  DEFAULT_TABS
end

.disable(*opts) ⇒ Object



82
83
84
# File 'lib/sidekiq/web.rb', line 82

def disable(*opts)
  opts.each { |key| set(key, false) }
end

.enable(*opts) ⇒ Object



78
79
80
# File 'lib/sidekiq/web.rb', line 78

def enable(*opts)
  opts.each { |key| set(key, true) }
end

.inherited(child) ⇒ Object



102
103
104
105
# File 'lib/sidekiq/web.rb', line 102

def self.inherited(child)
  child.app_url = app_url
  child.redis_pool = redis_pool
end

.middlewaresObject



86
87
88
# File 'lib/sidekiq/web.rb', line 86

def middlewares
  @middlewares ||= []
end

.register(extension, name: nil, tab: nil, index: nil, root_dir: nil, cache_for: 86400, asset_paths: nil) {|_self| ... } ⇒ Object

Register a class as a Sidekiq Web UI extension. The class should provide one or more tabs which map to an index route. Options:

TODO name, tab and index will be mandatory in 8.0

Web extensions will have a root ‘web/` directory with `locales/`, `assets/` and `views/` subdirectories.

Parameters:

  • extension (Class)

    Class which contains the HTTP actions, required

  • name (String) (defaults to: nil)

    the name of the extension, used to namespace assets

  • tab (String | Array) (defaults to: nil)

    labels(s) of the UI tabs

  • index (String | Array) (defaults to: nil)

    index route(s) for each tab

  • root_dir (String) (defaults to: nil)

    directory location to find assets, locales and views, typically ‘web/` within the gemfile

  • asset_paths (Array) (defaults to: nil)

    one or more directories under Top Level Namespace/assets/name to be publicly served, e.g. [“js”, “css”, “img”]

  • cache_for (Integer) (defaults to: 86400)

    amount of time to cache assets, default one day

Yields:

  • (_self)

Yield Parameters:

  • _self (Sidekiq::Web)

    the object that the method was called on



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/sidekiq/web.rb', line 160

def self.register(extension, name: nil, tab: nil, index: nil, root_dir: nil, cache_for: 86400, asset_paths: nil)
  tab = Array(tab)
  index = Array(index)
  tab.zip(index).each do |tab, index|
    tabs[tab] = index
  end
  if root_dir
    locdir = File.join(root_dir, "locales")
    locales << locdir if File.directory?(locdir)

    if asset_paths && name
      # if you have {root}/assets/{name}/js/scripts.js
      # and {root}/assets/{name}/css/styles.css
      # you would pass in:
      #   asset_paths: ["js", "css"]
      # See script_tag and style_tag in web/helpers.rb
      assdir = File.join(root_dir, "assets")
      assurls = Array(asset_paths).map { |x| "/#{name}/#{x}" }
      assetprops = {
        urls: assurls,
        root: assdir,
        cascade: true
      }
      assetprops[:header_rules] = [[:all, {Rack::CACHE_CONTROL => "private, max-age=#{cache_for.to_i}"}]] if cache_for
      middlewares << [[Rack::Static, assetprops], nil]
    end
  end

  yield self if block_given?
  extension.registered(WebApplication)
end

.set(attribute, value) ⇒ Object



94
95
96
# File 'lib/sidekiq/web.rb', line 94

def set(attribute, value)
  send(:"#{attribute}=", value)
end

.settingsObject



53
54
55
# File 'lib/sidekiq/web.rb', line 53

def settings
  self
end

.use(*args, &block) ⇒ Object



90
91
92
# File 'lib/sidekiq/web.rb', line 90

def use(*args, &block)
  middlewares << [args, block]
end

Instance Method Details

#appObject



129
130
131
# File 'lib/sidekiq/web.rb', line 129

def app
  @app ||= build
end

#call(env) ⇒ Object



119
120
121
122
# File 'lib/sidekiq/web.rb', line 119

def call(env)
  env[:csp_nonce] = SecureRandom.base64(16)
  app.call(env)
end

#disable(*opts) ⇒ Object



137
138
139
# File 'lib/sidekiq/web.rb', line 137

def disable(*opts)
  opts.each { |key| set(key, false) }
end

#enable(*opts) ⇒ Object



133
134
135
# File 'lib/sidekiq/web.rb', line 133

def enable(*opts)
  opts.each { |key| set(key, true) }
end

#middlewaresObject



111
112
113
# File 'lib/sidekiq/web.rb', line 111

def middlewares
  @middlewares ||= self.class.middlewares
end

#set(attribute, value) ⇒ Object



141
142
143
# File 'lib/sidekiq/web.rb', line 141

def set(attribute, value)
  send(:"#{attribute}=", value)
end

#settingsObject



107
108
109
# File 'lib/sidekiq/web.rb', line 107

def settings
  self.class.settings
end

#use(*args, &block) ⇒ Object



115
116
117
# File 'lib/sidekiq/web.rb', line 115

def use(*args, &block)
  middlewares << [args, block]
end