2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2013-05-16 11:27:10 -04:00
|
|
|
|
2019-04-01 12:20:41 -04:00
|
|
|
require "erb"
|
2012-07-18 01:14:15 -04:00
|
|
|
|
2019-04-01 12:20:41 -04:00
|
|
|
require "sidekiq"
|
|
|
|
require "sidekiq/api"
|
|
|
|
require "sidekiq/paginator"
|
|
|
|
require "sidekiq/web/helpers"
|
2016-07-26 10:03:17 -04:00
|
|
|
|
2019-04-01 12:20:41 -04:00
|
|
|
require "sidekiq/web/router"
|
|
|
|
require "sidekiq/web/action"
|
|
|
|
require "sidekiq/web/application"
|
2020-06-03 19:06:36 -04:00
|
|
|
require "sidekiq/web/csrf_protection"
|
2012-07-18 01:14:15 -04:00
|
|
|
|
2020-04-22 17:03:50 -04:00
|
|
|
require "rack/content_length"
|
2019-04-01 12:20:41 -04:00
|
|
|
|
|
|
|
require "rack/builder"
|
2020-01-13 18:35:35 -05:00
|
|
|
require "rack/file"
|
2019-04-01 12:20:41 -04:00
|
|
|
require "rack/session/cookie"
|
2015-07-06 15:51:15 -04:00
|
|
|
|
2016-07-26 10:03:17 -04:00
|
|
|
module Sidekiq
|
|
|
|
class Web
|
2016-07-27 15:18:52 -04:00
|
|
|
ROOT = File.expand_path("#{File.dirname(__FILE__)}/../../web")
|
2018-02-16 16:11:54 -05:00
|
|
|
VIEWS = "#{ROOT}/views"
|
|
|
|
LOCALES = ["#{ROOT}/locales"]
|
|
|
|
LAYOUT = "#{VIEWS}/layout.erb"
|
|
|
|
ASSETS = "#{ROOT}/assets"
|
2012-03-06 23:17:42 -05:00
|
|
|
|
2013-10-13 18:56:17 -04:00
|
|
|
DEFAULT_TABS = {
|
2019-04-01 12:20:41 -04:00
|
|
|
"Dashboard" => "",
|
|
|
|
"Busy" => "busy",
|
|
|
|
"Queues" => "queues",
|
|
|
|
"Retries" => "retries",
|
|
|
|
"Scheduled" => "scheduled",
|
2020-03-17 16:38:48 -04:00
|
|
|
"Dead" => "morgue"
|
2013-10-13 18:56:17 -04:00
|
|
|
}
|
2013-05-30 00:07:49 -04:00
|
|
|
|
2013-10-13 18:56:17 -04:00
|
|
|
class << self
|
2016-07-28 22:36:21 -04:00
|
|
|
def settings
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2016-07-30 10:12:08 -04:00
|
|
|
def middlewares
|
|
|
|
@middlewares ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def use(*middleware_args, &block)
|
|
|
|
middlewares << [middleware_args, block]
|
|
|
|
end
|
|
|
|
|
2013-10-13 18:56:17 -04:00
|
|
|
def default_tabs
|
|
|
|
DEFAULT_TABS
|
2013-04-15 13:43:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def custom_tabs
|
2013-10-13 18:56:17 -04:00
|
|
|
@custom_tabs ||= {}
|
2013-08-22 03:32:42 -04:00
|
|
|
end
|
2013-10-13 18:56:17 -04:00
|
|
|
alias_method :tabs, :custom_tabs
|
2014-08-01 19:40:42 -04:00
|
|
|
|
2016-07-26 10:03:17 -04:00
|
|
|
def locales
|
|
|
|
@locales ||= LOCALES
|
2014-05-16 00:12:44 -04:00
|
|
|
end
|
2012-05-02 23:19:59 -04:00
|
|
|
|
2016-07-28 22:36:21 -04:00
|
|
|
def views
|
|
|
|
@views ||= VIEWS
|
|
|
|
end
|
|
|
|
|
2016-10-14 12:51:13 -04:00
|
|
|
def enable(*opts)
|
2019-05-30 13:41:47 -04:00
|
|
|
opts.each { |key| set(key, true) }
|
2016-10-14 12:51:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def disable(*opts)
|
2019-05-30 13:41:47 -04:00
|
|
|
opts.each { |key| set(key, false) }
|
2016-10-14 12:51:13 -04:00
|
|
|
end
|
|
|
|
|
2016-09-15 10:19:46 -04:00
|
|
|
# Helper for the Sinatra syntax: Sidekiq::Web.set(:session_secret, Rails.application.secrets...)
|
|
|
|
def set(attribute, value)
|
|
|
|
send(:"#{attribute}=", value)
|
|
|
|
end
|
|
|
|
|
2016-10-14 12:51:13 -04:00
|
|
|
attr_accessor :app_url, :session_secret, :redis_pool, :sessions
|
2016-07-28 22:36:21 -04:00
|
|
|
attr_writer :locales, :views
|
|
|
|
end
|
|
|
|
|
2016-10-17 14:16:26 -04:00
|
|
|
def self.inherited(child)
|
2019-04-01 12:20:41 -04:00
|
|
|
child.app_url = app_url
|
|
|
|
child.session_secret = session_secret
|
|
|
|
child.redis_pool = redis_pool
|
|
|
|
child.sessions = sessions
|
2016-10-17 14:16:26 -04:00
|
|
|
end
|
|
|
|
|
2016-07-28 22:36:21 -04:00
|
|
|
def settings
|
|
|
|
self.class.settings
|
2014-02-09 17:56:01 -05:00
|
|
|
end
|
|
|
|
|
2016-07-30 16:09:26 -04:00
|
|
|
def use(*middleware_args, &block)
|
|
|
|
middlewares << [middleware_args, block]
|
|
|
|
end
|
|
|
|
|
|
|
|
def middlewares
|
2016-08-01 19:28:17 -04:00
|
|
|
@middlewares ||= Web.middlewares.dup
|
2016-07-30 16:09:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
app.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.call(env)
|
|
|
|
@app ||= new
|
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def app
|
|
|
|
@app ||= build
|
|
|
|
end
|
|
|
|
|
2016-10-14 12:51:13 -04:00
|
|
|
def enable(*opts)
|
2019-05-30 13:41:47 -04:00
|
|
|
opts.each { |key| set(key, true) }
|
2016-10-14 12:51:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def disable(*opts)
|
2019-05-30 13:41:47 -04:00
|
|
|
opts.each { |key| set(key, false) }
|
2016-10-14 12:51:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def set(attribute, value)
|
|
|
|
send(:"#{attribute}=", value)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Default values
|
|
|
|
set :sessions, true
|
|
|
|
|
|
|
|
attr_writer :sessions
|
|
|
|
|
|
|
|
def sessions
|
|
|
|
unless instance_variable_defined?("@sessions")
|
|
|
|
@sessions = self.class.sessions
|
|
|
|
@sessions = @sessions.to_hash.dup if @sessions.respond_to?(:to_hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
@sessions
|
|
|
|
end
|
|
|
|
|
2016-07-30 16:09:26 -04:00
|
|
|
def self.register(extension)
|
|
|
|
extension.registered(WebApplication)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-08-01 19:29:22 -04:00
|
|
|
def using?(middleware)
|
2019-04-01 12:20:41 -04:00
|
|
|
middlewares.any? do |(m, _)|
|
|
|
|
m.is_a?(Array) && (m[0] == middleware || m[0].is_a?(middleware))
|
2016-08-01 19:29:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-14 12:51:13 -04:00
|
|
|
def build_sessions
|
2016-08-01 21:29:09 -04:00
|
|
|
middlewares = self.middlewares
|
|
|
|
|
2020-06-04 01:11:53 -04:00
|
|
|
s = sessions
|
|
|
|
|
|
|
|
# turn on CSRF protection if sessions are enabled and this is not the test env
|
|
|
|
if s && !using?(CsrfProtection) && ENV["RACK_ENV"] != "test"
|
2020-06-03 19:06:36 -04:00
|
|
|
middlewares.unshift [[CsrfProtection], nil]
|
2016-08-01 21:29:09 -04:00
|
|
|
end
|
|
|
|
|
2020-06-03 13:23:15 -04:00
|
|
|
if s && !using?(::Rack::Session::Cookie)
|
2019-04-01 12:20:41 -04:00
|
|
|
unless (secret = Web.session_secret)
|
|
|
|
require "securerandom"
|
2016-08-01 19:29:22 -04:00
|
|
|
secret = SecureRandom.hex(64)
|
|
|
|
end
|
|
|
|
|
2019-04-01 12:20:41 -04:00
|
|
|
options = {secret: secret}
|
2016-10-14 13:01:44 -04:00
|
|
|
options = options.merge(s.to_hash) if s.respond_to? :to_hash
|
2016-10-14 12:51:13 -04:00
|
|
|
|
|
|
|
middlewares.unshift [[::Rack::Session::Cookie, options], nil]
|
2016-08-01 19:29:22 -04:00
|
|
|
end
|
2020-04-22 17:03:50 -04:00
|
|
|
|
|
|
|
# Since Sidekiq::WebApplication no longer calculates its own
|
|
|
|
# Content-Length response header, we must ensure that the Rack middleware
|
|
|
|
# that does this is loaded
|
|
|
|
unless using? ::Rack::ContentLength
|
|
|
|
middlewares.unshift [[::Rack::ContentLength], nil]
|
|
|
|
end
|
2016-10-14 12:51:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def build
|
|
|
|
build_sessions
|
|
|
|
|
|
|
|
middlewares = self.middlewares
|
|
|
|
klass = self.class
|
2016-08-01 19:29:22 -04:00
|
|
|
|
2016-07-30 16:09:26 -04:00
|
|
|
::Rack::Builder.new do
|
2019-04-01 12:20:41 -04:00
|
|
|
%w[stylesheets javascripts images].each do |asset_dir|
|
2016-07-26 10:03:17 -04:00
|
|
|
map "/#{asset_dir}" do
|
2020-01-13 18:35:35 -05:00
|
|
|
run ::Rack::File.new("#{ASSETS}/#{asset_dir}", {"Cache-Control" => "public, max-age=86400"})
|
2016-07-26 10:03:17 -04:00
|
|
|
end
|
|
|
|
end
|
2013-07-27 12:49:21 -04:00
|
|
|
|
2019-05-30 13:41:47 -04:00
|
|
|
middlewares.each { |middleware, block| use(*middleware, &block) }
|
2016-07-30 10:12:08 -04:00
|
|
|
|
2016-07-29 15:09:00 -04:00
|
|
|
run WebApplication.new(klass)
|
2012-11-26 14:53:22 -05:00
|
|
|
end
|
2012-03-29 23:55:16 -04:00
|
|
|
end
|
2012-03-04 15:58:16 -05:00
|
|
|
end
|
2016-07-27 15:18:52 -04:00
|
|
|
|
|
|
|
Sidekiq::WebApplication.helpers WebHelpers
|
|
|
|
Sidekiq::WebApplication.helpers Sidekiq::Paginator
|
|
|
|
|
2019-04-01 12:20:41 -04:00
|
|
|
Sidekiq::WebAction.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
|
|
def _render
|
|
|
|
#{ERB.new(File.read(Web::LAYOUT)).src}
|
|
|
|
end
|
|
|
|
RUBY
|
2012-03-04 15:58:16 -05:00
|
|
|
end
|