zzz_action_cable_preferences.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require_relative '../../lib/zammad/service/redis'
  3. # If REDIS_URL is not set, fall back to default port / localhost, to ease configuration
  4. # for simple installations.
  5. redis_url = ENV['REDIS_URL'].presence || 'redis://localhost:6379'
  6. driver = redis_url.start_with?('rediss://') ? :ruby : :hiredis
  7. Rails.application.config.action_cable.cable = {
  8. adapter: :redis,
  9. driver: driver,
  10. url: redis_url,
  11. channel_prefix: "zammad_#{Rails.env}",
  12. }
  13. begin
  14. Zammad::Service::Redis.new.ping
  15. Rails.logger.info { "ActionCable is using the redis instance at #{redis_url}." }
  16. rescue Redis::CannotConnectError => e
  17. warn "There was an error trying to connect to Redis via #{redis_url}."
  18. if ENV['REDIS_URL'].present?
  19. warn 'Please make sure Redis is available.'
  20. else
  21. warn 'Please provide a Redis instance at localhost:6379 or set REDIS_URL to point to a different location.'
  22. end
  23. warn e.inspect
  24. Zammad::SafeMode.continue_or_exit!
  25. end
  26. Rails.application.reloader.to_prepare do
  27. begin
  28. Rails.application.config.action_cable.allow_same_origin_as_host = true
  29. # Support for configurations where the HTTP_HOST header is not correctly forwarded:
  30. request_origins = [%r{https?://localhost:\d+}]
  31. request_origins << "#{Setting.get('http_type')}://#{Setting.get('fqdn')}"
  32. Rails.application.config.action_cable.allowed_request_origins = request_origins
  33. Rails.application.config.action_cable.disable_request_forgery_protection = true if !Rails.env.production?
  34. Rails.logger.info { "ActionCable is configured to accept requests from #{request_origins.join(', ')}." }
  35. rescue ActiveRecord::ConnectionNotEstablished, ActiveRecord::NoDatabaseError, ActiveRecord::StatementInvalid
  36. Rails.logger.warn { "Database doesn't exist. Skipping allowed_request_origins configuration." }
  37. end
  38. end