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.



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

def app_url
  @app_url
end

.localesObject



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

def locales
  @locales ||= LOCALES
end

.redis_poolObject

Returns the value of attribute redis_pool.



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

def redis_pool
  @redis_pool
end

.viewsObject



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

def views
  @views ||= VIEWS
end

Class Method Details

.call(env) ⇒ Object



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

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

.configure {|_self| ... } ⇒ Object

Forward compatibility with 8.0

Yields:

  • (_self)

Yield Parameters:

  • _self (Sidekiq::Web)

    the object that the method was called on



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

def configure
  yield self
end

.custom_job_info_rowsObject



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

def custom_job_info_rows
  @custom_job_info_rows ||= []
end

.custom_tabsObject Also known as: tabs



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

def custom_tabs
  @custom_tabs ||= {}
end

.default_tabsObject



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

def default_tabs
  DEFAULT_TABS
end

.disable(*opts) ⇒ Object



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

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

.enable(*opts) ⇒ Object



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

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

.inherited(child) ⇒ Object



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

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

.middlewaresObject



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

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



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
191
192
193
194
195
# File 'lib/sidekiq/web.rb', line 165

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



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

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

.settingsObject



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

def settings
  self
end

.use(*args, &block) ⇒ Object



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

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

Instance Method Details

#appObject



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

def app
  @app ||= build
end

#call(env) ⇒ Object



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

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

#disable(*opts) ⇒ Object



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

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

#enable(*opts) ⇒ Object



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

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

#middlewaresObject



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

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

#set(attribute, value) ⇒ Object



146
147
148
# File 'lib/sidekiq/web.rb', line 146

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

#settingsObject



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

def settings
  self.class.settings
end

#use(*args, &block) ⇒ Object



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

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