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.



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

def initialize(options)
  @config = options
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Class Method Details

.define(listeners) ⇒ Object



175
176
177
178
179
180
181
182
183
# File 'lib/esi/config.rb', line 175

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



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

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



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

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'
    else
      @config[:cache] = 'ruby'
    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)


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

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)


131
132
133
134
135
136
137
138
# File 'lib/esi/config.rb', line 131

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



140
141
142
# File 'lib/esi/config.rb', line 140

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

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

Yields:

  • (config_router)


169
170
171
172
173
# File 'lib/esi/config.rb', line 169

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