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-07-26 10:03:17 -04:00
|
|
|
def session_secret=(secret)
|
2016-07-28 22:36:21 -04:00
|
|
|
@session_secret = secret
|
2014-02-09 17:56:01 -05:00
|
|
|
end
|
|
|
|
|
2016-07-29 15:09:00 -04:00
|
|
|
attr_accessor :app_url, :session_secret, :redis_pool
|
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
|
|
|
|
|
|
|
|
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-07-30 16:09:26 -04:00
|
|
|
def build
|
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
|
|
|
|
|
|
|
|
use ::Rack::Session::Cookie, secret: secret
|
|
|
|
end
|
|
|
|
|
|
|
|
unless using?(::Rack::Protection) || ENV['RACK_ENV'] == 'test'
|
|
|
|
use ::Rack::Protection, use: :authenticity_token
|
2012-05-11 19:48:03 -04:00
|
|
|
end
|
|
|
|
|
2016-07-30 16:09:26 -04:00
|
|
|
middlewares = self.middlewares
|
2016-07-29 15:09:00 -04:00
|
|
|
klass = self.class
|
|
|
|
|
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-07-27 01:19:56 -04:00
|
|
|
run ::Rack::File.new("#{ASSETS}/#{asset_dir}")
|
2016-07-26 10:03:17 -04:00
|
|
|
end
|
|
|
|
end
|
2013-07-27 12:49:21 -04:00
|
|
|
|
2016-07-30 16:09:26 -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) &&
|
|
|
|
!::ActionDispatch::Request::Session.respond_to?(:each)
|
|
|
|
# mperham/sidekiq#2460
|
|
|
|
# Rack apps can't reuse the Rails session store without
|
|
|
|
# this monkeypatch
|
|
|
|
class ActionDispatch::Request::Session
|
|
|
|
def each(&block)
|
|
|
|
hash = self.to_hash
|
|
|
|
hash.each(&block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|