Create a gist now

Instantly share code, notes, and snippets.

Embed
Mountable resque-web for rails 3+ apps
# https://gist.github.com/1214052
require 'sinatra/base'
class ResqueWeb < Sinatra::Base
require 'resque/server'
use Rack::ShowExceptions
if CFG[:user].present? and CFG[:password].present?
Resque::Server.use Rack::Auth::Basic do |user, password|
user == CFG[:user] && password == CFG[:password]
end
end
def call(env)
@server ||= Resque::Server.new
status, headers, body = @server.call(env)
# in production/staging with nginx, assets always hang endless <-> this fixes it
if body.is_a? Sinatra::Helpers::StaticFile
buffer = []
body.each{|x| buffer << x }
body = buffer
end
[status, headers, body]
end
end
# ...
match "/resque" => ResqueWeb, :anchor => false
# ...
@dbackeus

This comment has been minimized.

Show comment
Hide comment
@dbackeus

dbackeus Sep 14, 2011

Here's what I do using devise for authentication:

# routes.rb
mount Resque::Server => "/resque", :as => 'resque'

# config/initializers/resque.rb
Resque::Server.use(AdminRack)

# app/rack/admin_rack.rb
class AdminRack  
  def initialize(app)
    @app = app
  end

  def call(env)
    user = env['warden'].authenticate(:scope => :user)
    if user && user.admin?
      @app.call(env) 
    else
      throw(:warden, :scope => :user, :message => "Unauthorized")
    end
  end
end

Here's what I do using devise for authentication:

# routes.rb
mount Resque::Server => "/resque", :as => 'resque'

# config/initializers/resque.rb
Resque::Server.use(AdminRack)

# app/rack/admin_rack.rb
class AdminRack  
  def initialize(app)
    @app = app
  end

  def call(env)
    user = env['warden'].authenticate(:scope => :user)
    if user && user.admin?
      @app.call(env) 
    else
      throw(:warden, :scope => :user, :message => "Unauthorized")
    end
  end
end
@evansagge

This comment has been minimized.

Show comment
Hide comment
@evansagge

evansagge Sep 15, 2011

Simplest way to do it:

# routes.rb
mount Resque::Server.new, :at => "/resque"

If you're running authentication thru Devise (:admin scope):

# routes.rb
authenticate :admin do
  mount Resque::Server.new, :at => "/resque"
end

Simplest way to do it:

# routes.rb
mount Resque::Server.new, :at => "/resque"

If you're running authentication thru Devise (:admin scope):

# routes.rb
authenticate :admin do
  mount Resque::Server.new, :at => "/resque"
end
@Sohair63

This comment has been minimized.

Show comment
Hide comment
@Sohair63

Sohair63 Dec 28, 2017

Using ENV variables for specific users

# routes.rb
mount Resque::SecureResqueServer, at: '/jobs'
# config/initializers/resque_auth.rb
require 'resque/server'

class Resque::SecureResqueServer < Resque::Server
  before do
   # running authentication via Devise
    user = request.env['warden'].authenticate(scope: :user)
    redirect '/' unless user&.email.in?(ENV['RESQUE_USER_EMAILS'].to_s.split(','))
  end
end

Sohair63 commented Dec 28, 2017

Using ENV variables for specific users

# routes.rb
mount Resque::SecureResqueServer, at: '/jobs'
# config/initializers/resque_auth.rb
require 'resque/server'

class Resque::SecureResqueServer < Resque::Server
  before do
   # running authentication via Devise
    user = request.env['warden'].authenticate(scope: :user)
    redirect '/' unless user&.email.in?(ENV['RESQUE_USER_EMAILS'].to_s.split(','))
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment