1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/lib/sidekiq/web.rb

113 lines
2.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2013-08-22 11:32:42 +04:00
require 'erb'
2013-04-29 14:56:05 -07:00
require 'yaml'
2013-05-16 08:27:10 -07:00
2013-04-29 14:56:05 -07:00
require 'sidekiq'
require 'sidekiq/api'
2012-07-17 22:14:15 -07:00
require 'sidekiq/paginator'
require 'sidekiq/web/router'
require 'sidekiq/web/application'
require 'rack/protection'
2012-07-17 22:14:15 -07:00
require 'rack/builder'
require 'rack/static'
require 'rack/session/cookie'
module Sidekiq
class Web
REQUEST_METHOD = 'REQUEST_METHOD'.freeze
PATH_INFO = 'PATH_INFO'.freeze
2012-09-30 14:57:51 +02:00
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',
2014-02-09 14:56:01 -08:00
"Dead" => 'morgue',
}
class << self
def default_tabs
DEFAULT_TABS
2013-04-15 13:43:31 -04:00
end
def custom_tabs
@custom_tabs ||= {}
2013-08-22 11:32:42 +04:00
end
alias_method :tabs, :custom_tabs
2014-08-01 23:40:42 +00:00
def locales
@locales ||= LOCALES
end
def session_secret=(secret)
@secret = secret
2014-02-09 14:56:01 -08:00
end
attr_accessor :app_url, :session_secret
attr_writer :locales
2014-02-09 14:56:01 -08:00
end
def initialize
secret = Web.session_secret
2014-02-09 14:56:01 -08:00
if secret.nil?
# explicitly generating a session secret eagerly to play nice with preforking
begin
require 'securerandom'
secret = SecureRandom.hex(64)
rescue LoadError, NotImplementedError
# SecureRandom raises a NotImplementedError if no random device is available
secret = "%064x" % Kernel.rand(2**256-1)
end
end
@app = Rack::Builder.new do
%w(stylesheets javascripts images).each do |asset_dir|
map "/#{asset_dir}" do
run Rack::File.new("#{ASSETS}/#{asset_dir}")
end
end
use Rack::Session::Cookie, secret: secret
use ::Rack::Protection, use: :authenticity_token unless ENV['RACK_ENV'] == 'test'
run WebApplication.new
end
end
def call(env)
@app.call(env)
end
def self.call(env)
@app ||= new
@app.call(env)
2012-12-05 11:28:55 -05:00
end
ERB.new(File.read LAYOUT).def_method(WebAction, '_render')
end
end
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