Skip to content

Instantly share code, notes, and snippets.

@kirpachov
Created January 29, 2025 15:03
Show Gist options
  • Save kirpachov/e1b0817bec665df0792ae77088373084 to your computer and use it in GitHub Desktop.
Save kirpachov/e1b0817bec665df0792ae77088373084 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
class RoutesBasicAuth
class << self
def call(klass, username:, password:)
raise ArgumentError, 'klass must be a class' unless klass.is_a?(Class)
Rack::Builder.new do
if username.is_a?(String) && username.present? && password.is_a?(String) && password.present?
use Rack::Auth::Basic do |provided_username, provided_password|
ActiveSupport::SecurityUtils.secure_compare(provided_username, username) &&
ActiveSupport::SecurityUtils.secure_compare(provided_password, password)
end
else
puts "No username or password provided. Basic auth is disabled for #{klass}."
Rails.logger.warn "No username or password provided. Basic auth is disabled for #{klass}."
end
map '/' do
run klass
end
end
end
end
end
# frozen_string_literal: true
require 'sidekiq/web'
require 'sidekiq/cron/web'
require 'sidekiq-status/web'
Rails.application.routes.draw do # rubocop:disable Metrics/BlockLength
mount RoutesBasicAuth.call(
Sidekiq::Web,
username: ENV["BASIC_AUTH_USERNAME"],
password: ENV["BASIC_AUTH_PASSWORD"]
) => '/sidekiq'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment