zzz_action_cable_preferences.rb 1.8 KB

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