2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2013-08-22 03:32:42 -04:00
|
|
|
require 'erb'
|
2013-04-29 17:56:05 -04:00
|
|
|
require 'yaml'
|
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-26 10:03:17 -04:00
|
|
|
require 'sidekiq/web/router'
|
|
|
|
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'
|
|
|
|
require 'rack/static'
|
|
|
|
require 'rack/session/cookie'
|
2015-07-06 15:51:15 -04:00
|
|
|
|
2016-07-26 10:03:17 -04:00
|
|
|
module Sidekiq
|
|
|
|
class Web
|
|
|
|
REQUEST_METHOD = 'REQUEST_METHOD'.freeze
|
|
|
|
PATH_INFO = 'PATH_INFO'.freeze
|
2012-09-30 08:57:51 -04:00
|
|
|
|
2016-07-26 10:03:17 -04:00
|
|
|
ROOT = File.expand_path(File.dirname(__FILE__) + "/../../web")
|
|
|
|
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 = {
|
|
|
|
"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
|
|
|
|
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-26 10:03:17 -04:00
|
|
|
def session_secret=(secret)
|
|
|
|
@secret = secret
|
2014-02-09 17:56:01 -05:00
|
|
|
end
|
|
|
|
|
2016-07-26 10:03:17 -04:00
|
|
|
attr_accessor :app_url, :session_secret
|
|
|
|
attr_writer :locales
|
2014-02-09 17:56:01 -05:00
|
|
|
end
|
|
|
|
|
2016-07-26 10:03:17 -04:00
|
|
|
def initialize
|
|
|
|
secret = Web.session_secret
|
2014-02-09 17:56:01 -05:00
|
|
|
|
2016-07-26 10:03:17 -04:00
|
|
|
if secret.nil?
|
2016-07-27 01:19:56 -04:00
|
|
|
require 'securerandom'
|
|
|
|
secret = SecureRandom.hex(64)
|
2012-05-11 19:48:03 -04:00
|
|
|
end
|
|
|
|
|
2016-07-27 01:19:56 -04:00
|
|
|
@app = ::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-27 01:19:56 -04:00
|
|
|
use ::Rack::Session::Cookie, secret: secret
|
2016-07-26 10:03:17 -04:00
|
|
|
use ::Rack::Protection, use: :authenticity_token unless ENV['RACK_ENV'] == 'test'
|
2013-06-21 22:43:06 -04:00
|
|
|
|
2016-07-26 10:03:17 -04:00
|
|
|
run WebApplication.new
|
2012-11-26 14:53:22 -05:00
|
|
|
end
|
2012-03-29 23:55:16 -04:00
|
|
|
end
|
2012-05-11 19:48:03 -04:00
|
|
|
|
2016-07-26 10:03:17 -04:00
|
|
|
def call(env)
|
|
|
|
@app.call(env)
|
2013-07-27 12:49:21 -04:00
|
|
|
end
|
|
|
|
|
2016-07-26 10:03:17 -04:00
|
|
|
def self.call(env)
|
|
|
|
@app ||= new
|
|
|
|
@app.call(env)
|
2012-12-05 11:28:55 -05:00
|
|
|
end
|
|
|
|
|
2016-07-27 01:19:56 -04:00
|
|
|
def self.register(extension)
|
|
|
|
extension.registered(WebApplication)
|
|
|
|
end
|
|
|
|
|
2016-07-26 10:03:17 -04:00
|
|
|
ERB.new(File.read LAYOUT).def_method(WebAction, '_render')
|
2012-03-04 15:58:16 -05:00
|
|
|
end
|
|
|
|
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
|