Class: ESI::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/esi/config.rb

Overview

This file provides configuration options to mongrel-esi Mongrel ESI is a proxy caching server with limited load balancing capablities

ESI::Config.define(listeners) do|config|

  # define the caching rules globally for all routes, defaults to ruby
  config.cache do|c|
    c.memcached do|mc|
      mc.servers = ['localhost:11211']  # memcahed servers
      mc.namespace = 'mesi'  # namespace for cache storage
    end
    c.ttl = 600 # default fragment time to live, when <esi:include does not include the max-age attribute
  end

  # define rules for when to enable esi processing globally for all routes
  # using content type it is more flexible, but sometimes you will want to be
  # explicit about when to enable esi processing. For those cases, enable_for_surrogate_only will
  # require the presense of the Surrogate-Control header to contain the content="ESI/1.0" line.
  # see [http://www.w3.org/TR/edge-arch Edge Arch] for details.
  config.esi do|c|
    c.allowed_content_types = ['text/plain', 'text/html']
    #c.enable_for_surrogate_only = true # default is false
  end

  # define request path routing rules, these rules match against request path to select a specific server
  config.routes do|s|
    #s.match( /content/ ) do|r|
    #  r.servers = ['127.0.0.1:4000']
    #end
    s.default do|r|
      r.servers = ['127.0.0.1:3000']
    end
  end

end

Defined Under Namespace

Classes: CacheConfig, ConfigRouter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Config

Returns a new instance of Config.



45
46
47
# File 'lib/esi/config.rb', line 45

def initialize(options)
  @config = options
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



43
44
45
# File 'lib/esi/config.rb', line 43

def config
  @config
end

Class Method Details

.define(listeners) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'lib/esi/config.rb', line 179

def self.define( listeners )
  listeners.each do|host,server|
    esi_handlers = server.classifier.handler_map.select do|uri,handler|
      handler.first.class == ESI::Dispatcher
    end
    config = esi_handlers.first.last.first.config
    yield config
  end
end

Instance Method Details

#[](key) ⇒ Object

access configuration values



50
51
52
# File 'lib/esi/config.rb', line 50

def [](key)
  @config[key]
end

#cacheObject

returns the cache object as given in the config/esi.yml cache: key, or defaults to ruby as in uses this process the options allowed are ruby and memcache



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/esi/config.rb', line 104

def cache
  if block_given?
    # allow this method to be called in config scripts
    cache_options = CacheConfig.new
    yield cache_options
    if cache_options.memcached?
      @config[:cache] = 'memcached'
    end
    @config[:cache_options] = cache_options.options
  else
    cache_type = @config[:cache]
    options = @config[:cache_options]
    # always return the same cache object, per process
    $cache ||= case cache_type
    when 'ruby'
      ESI::RubyCache.new(options)
    when 'memcached'
      ESI::MemcachedCache.new(options)
    else
      raise "Unsupported cache"
    end
  end
end

#enable_esi_processor?(headers) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/esi/config.rb', line 54

def enable_esi_processor?( headers )
  # check for surrogate control configuration
  # check for matching content type
  # if both are set it surrogate takes presendence 
  use_esi = false
  allowed_content_types = @config[:allowed_content_types]

  if allowed_content_types and headers["content-type"] and allowed_content_types.respond_to?(:detect)
    use_esi = allowed_content_types.detect do |type|
      headers["content-type"].match( type )
    end
    use_esi = true if use_esi
  end

  if @config[:enable_for_surrogate_only]
    use_esi = headers["surrogate-control"] and /ESI/.match(headers["surrogate-control"])
  end

  use_esi
end

#esi {|options| ... } ⇒ Object

Yields:

  • (options)


128
129
130
131
132
133
134
135
# File 'lib/esi/config.rb', line 128

def esi
  options = OpenStruct.new({})
  yield options
  @config[:allowed_content_types] = options.allowed_content_types if options.allowed_content_types
  @config[:enable_for_surrogate_only] = options.enable_for_surrogate_only if options.enable_for_surrogate_only
  @config[:chunk_size] = options.chunk_size if options.chunk_size
  @config[:max_depth] = options.max_depth if options.max_depth
end

#routerObject



137
138
139
# File 'lib/esi/config.rb', line 137

def router
  Router.new( @config[:routing] )
end

#routes {|config_router| ... } ⇒ Object

Yields:

  • (config_router)


166
167
168
169
170
# File 'lib/esi/config.rb', line 166

def routes
  config_router = ConfigRouter.new
  yield config_router
  @config[:routing] = config_router.routes
end

#start_invalidator?Boolean

return true/false depending on the value of invalidator: on/off within in config/esi.yml

Returns:

  • (Boolean)


175
176
177
# File 'lib/esi/config.rb', line 175

def start_invalidator?
  @config[:invalidator]
end