Repository is archived
No commit activity in last 3 years
No release in over 3 years
Adds support for serving assets from multiple providers.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0
 Project Readme

Asset Host Selection Build Status

A basic pattern for ActionController to select between multiple CDNs.

Supports:

  • Multiple asset providers
  • Disabling asset providers
  • CDN killswitch environment variable to disable all CDNs

Usage

The host application is required to configure the desired asset providers, and handle the selection logic with an object that responds to #select and returns a provider:

# providers = {
#   :cloudfront => {
#     :subdomain => "example",
#     :domain    => "cloudfront.net",
#     :cdn       => true
#   },
#   :edgecast => {
#     :subdomain => "assets",
#     :domain    => example.com,
#     :cdn       => true
#   },
#   :internal => {
#     :subdomain => "assets2",
#     :domain    => example.com,
#     :cdn       => false
#   }
# }
#
#
# provider_pool     = ProviderPool.new(providers)
# config.asset_host = AssetHostSelection::AssetHostname.new(provider_pool)

class ProviderPool

  attr_reader :providers

  def initialize(provider_attributes)
    @providers = AssetHostSelection::AssetProvider.build_all(provider_attributes)
  end

  def select(request)
    account  = request.env["account"]
    provider = account.asset_provider

    if provider.try(:enabled?)
      provider
    else
      providers[:internal]
    end
  end

end

Copyright and license

Copyright 2013 Zendesk Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.