aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_cable/server/configuration.rb
blob: c5783f30e0c9d126903049d20b3592a6831b2c66 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
module ActionCable
  module Server
    class Configuration
      attr_accessor :logger, :log_tags
      attr_accessor :connection_class, :worker_pool_size
      attr_accessor :redis_path, :channels_path

      def initialize
        @logger   = Rails.logger
        @log_tags = []

        @connection_class  = ApplicationCable::Connection
        @worker_pool_size  = 100

        @redis_path    = Rails.root.join('config/redis/cable.yml')
        @channels_path = Rails.root.join('app/channels')
      end

      def channel_paths
        @channels ||= Dir["#{channels_path}/**/*_channel.rb"]
      end

      def channel_class_names
        @channel_class_names ||= channel_paths.collect do |channel_path|
          Pathname.new(channel_path).basename.to_s.split('.').first.camelize
        end
      end

      def redis
        @redis ||= config_for(redis_path).with_indifferent_access
      end

      private
        # FIXME: Extract this from Rails::Application in a way it can be used here.
        def config_for(path)
          if path.exist?
            require "yaml"
            require "erb"
            (YAML.load(ERB.new(path.read).result) || {})[Rails.env] || {}
          else
            raise "Could not load configuration. No such file - #{path}"
          end
        rescue Psych::SyntaxError => e
          raise "YAML syntax error occurred while parsing #{path}. " \
            "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
            "Error: #{e.message}"
        end
    end
  end
end