2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2013-08-22 03:32:42 -04:00
|
|
|
require 'erb'
|
2013-05-16 11:27:10 -04:00
|
|
|
|
2013-04-29 17:56:05 -04:00
|
|
|
require 'sidekiq'
|
|
|
|
require 'sidekiq/api'
|
2012-07-18 01:14:15 -04:00
|
|
|
require 'sidekiq/paginator'
|
2016-07-27 15:18:52 -04:00
|
|
|
require 'sidekiq/web/helpers'
|
2012-07-18 01:14:15 -04:00
|
|
|
|
2016-07-26 10:03:17 -04:00
|
|
|
require 'sidekiq/web/router'
|
2016-07-27 15:18:52 -04:00
|
|
|
require 'sidekiq/web/action'
|
2016-07-26 10:03:17 -04:00
|
|
|
require 'sidekiq/web/application'
|
|
|
|
|
|
|
|
require 'rack/protection'
|
2012-07-18 01:14:15 -04:00
|
|
|
|
2016-07-26 10:03:17 -04:00
|
|
|
require 'rack/builder'
|
2016-07-27 15:18:52 -04:00
|
|
|
require 'rack/file'
|
2016-07-26 10:03:17 -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")
|
|
|
|
VIEWS = "#{ROOT}/views".freeze
|
|
|
|
LOCALES = ["#{ROOT}/locales".freeze]
|
|
|
|
LAYOUT = "#{VIEWS}/layout.erb".freeze
|
|
|
|
ASSETS = "#{ROOT}/assets".freeze
|
2012-03-06 23:17:42 -05:00
|
|
|
|
2013-10-13 18:56:17 -04:00
|
|
|
DEFAULT_TABS = {
|
|
|
|
"Dashboard" => '',
|
2014-03-07 00:30:11 -05:00
|
|
|
"Busy" => 'busy',
|
2013-10-13 18:56:17 -04:00
|
|
|
"Queues" => 'queues',
|
|
|
|
"Retries" => 'retries',
|
|
|
|
"Scheduled" => 'scheduled',
|
2014-02-09 17:56:01 -05: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)
|
|
|
|
opts.each {|key| set(key, true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable(*opts)
|
|
|
|
opts.each {|key| set(key, false) }
|
|
|
|
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
|
|
|
|
|
|
|
|
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)
|
|
|
|
opts.each {|key| set(key, true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable(*opts)
|
|
|
|
opts.each {|key| set(key, false) }
|
|
|
|
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)
|
|
|
|
middlewares.any? do |(m,_)|
|
|
|
|
m.kind_of?(Array) && (m[0] == middleware || m[0].kind_of?(middleware))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-14 12:51:13 -04:00
|
|
|
def build_sessions
|
2016-08-01 21:29:09 -04:00
|
|
|
middlewares = self.middlewares
|
|
|
|
|
|
|
|
unless using?(::Rack::Protection) || ENV['RACK_ENV'] == 'test'
|
|
|
|
middlewares.unshift [[::Rack::Protection, { use: :authenticity_token }], nil]
|
|
|
|
end
|
|
|
|
|
2016-10-14 13:01:44 -04:00
|
|
|
s = sessions
|
|
|
|
return unless s
|
|
|
|
|
2016-08-01 19:29:22 -04:00
|
|
|
unless using? ::Rack::Session::Cookie
|
|
|
|
unless secret = Web.session_secret
|
|
|
|
require 'securerandom'
|
|
|
|
secret = SecureRandom.hex(64)
|
|
|
|
end
|
|
|
|
|
2016-10-14 12:51:13 -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
|
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
|
2016-07-26 10:03:17 -04:00
|
|
|
%w(stylesheets javascripts images).each do |asset_dir|
|
|
|
|
map "/#{asset_dir}" do
|
2016-08-30 16:07:33 -04: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
|
|
|
|
2016-08-30 16:16:57 -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
|
|
|
|
|
2016-07-29 18:41:42 -04:00
|
|
|
Sidekiq::WebAction.class_eval "def _render\n#{ERB.new(File.read(Web::LAYOUT)).src}\nend"
|
2012-03-04 15:58:16 -05:00
|
|
|
end
|
2015-08-31 13:57:03 -04:00
|
|
|
|
|
|
|
if defined?(::ActionDispatch::Request::Session) &&
|
2016-10-14 13:16:39 -04:00
|
|
|
!::ActionDispatch::Request::Session.method_defined?(:each)
|
2015-08-31 13:57:03 -04:00
|
|
|
# mperham/sidekiq#2460
|
|
|
|
# Rack apps can't reuse the Rails session store without
|
2016-09-06 14:15:52 -04:00
|
|
|
# this monkeypatch, fixed in Rails 5.
|
2015-08-31 13:57:03 -04:00
|
|
|
class ActionDispatch::Request::Session
|
|
|
|
def each(&block)
|
|
|
|
hash = self.to_hash
|
|
|
|
hash.each(&block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|