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

134 lines
2.9 KiB
Ruby
Raw Normal View History

# 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'
require 'sidekiq/web/helpers'
2012-07-18 01:14:15 -04:00
require 'sidekiq/web/router'
require 'sidekiq/web/action'
require 'sidekiq/web/application'
require 'rack/protection'
2012-07-18 01:14:15 -04:00
require 'rack/builder'
require 'rack/file'
require 'rack/session/cookie'
module Sidekiq
class Web
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
DEFAULT_TABS = {
"Dashboard" => '',
"Busy" => 'busy',
"Queues" => 'queues',
"Retries" => 'retries',
"Scheduled" => 'scheduled',
2014-02-09 17:56:01 -05:00
"Dead" => 'morgue',
}
class << self
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
def default_tabs
DEFAULT_TABS
2013-04-15 13:43:31 -04:00
end
def custom_tabs
@custom_tabs ||= {}
2013-08-22 03:32:42 -04:00
end
alias_method :tabs, :custom_tabs
2014-08-01 19:40:42 -04:00
def locales
@locales ||= LOCALES
end
def views
@views ||= VIEWS
end
def session_secret=(secret)
@session_secret = secret
2014-02-09 17:56:01 -05:00
end
attr_accessor :app_url, :session_secret, :redis_pool
attr_writer :locales, :views
end
def settings
self.class.settings
2014-02-09 17:56:01 -05:00
end
def initialize
unless secret = Web.session_secret
2016-07-27 01:19:56 -04:00
require 'securerandom'
secret = SecureRandom.hex(64)
end
klass = self.class
2016-07-27 01:19:56 -04:00
@app = ::Rack::Builder.new do
%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}")
end
end
2016-07-27 01:19:56 -04:00
use ::Rack::Session::Cookie, secret: secret
use ::Rack::Protection, use: :authenticity_token unless ENV['RACK_ENV'] == 'test'
2016-07-30 10:12:08 -04:00
Web.middlewares.each {|middleware, block| use *middleware, &block }
run WebApplication.new(klass)
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
2016-07-27 01:19:56 -04:00
def self.register(extension)
extension.registered(WebApplication)
end
end
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"
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