cloud_front 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +2 -0
- data/Rakefile +2 -0
- data/cloud_front.gemspec +21 -0
- data/lib/cloud_front.rb +25 -0
- data/lib/cloud_front/hashed_request_interpreter.rb +20 -0
- data/lib/cloud_front/railtie.rb +53 -0
- data/lib/cloud_front/version.rb +3 -0
- metadata +88 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/cloud_front.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "cloud_front/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "cloud_front"
|
7
|
+
s.version = CloudFront::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Nathaniel Bibler"]
|
10
|
+
s.email = ["gem@nathanielbibler.com"]
|
11
|
+
s.homepage = "https://github.com/nbibler/cloud_front"
|
12
|
+
s.summary = %q{A Rack middleware and Rails 3 configuration for using the Amazon CloudFront CDN as a front for your application as a custom origin.}
|
13
|
+
s.description = %q{This library sets up your application to use the Amazon CloudFront CDN by modifying your Rails 3 asset_host to the host you specify. It also allows you to use your Rails 3 application directly as the origin server for CloudFront alleviating the need to upload your resources to Amazon S3. The included middleware manages recognizing and re-routing CloudFront-based requests to local assets.}
|
14
|
+
|
15
|
+
s.add_dependency 'sha_header', '~> 0.0.0'
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
data/lib/cloud_front.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'cloud_front/railtie' if defined?(::Rails::Railtie)
|
2
|
+
|
3
|
+
module CloudFront
|
4
|
+
autoload :HashedRequestInterpreter, 'cloud_front/hashed_request_interpreter'
|
5
|
+
|
6
|
+
mattr_accessor :domain
|
7
|
+
|
8
|
+
def self.asset_host(source, request)
|
9
|
+
if source =~ %r{^//}
|
10
|
+
''
|
11
|
+
else
|
12
|
+
"#{request.protocol}#{domain}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.asset_path(asset_path)
|
17
|
+
absolute_path = File.join(ActionController::Base.config.assets_dir, asset_path)
|
18
|
+
if asset_path.to_s !~ %r{^//} && (File.exist?(absolute_path) || asset_path.to_s =~ /_packaged\.(?:css|js)$/)
|
19
|
+
relative_path = Pathname.new(asset_path)
|
20
|
+
"/%s%s/%s%s" % [ENV['COMMIT_HASH'], relative_path.dirname, relative_path.basename(relative_path.extname), relative_path.extname.to_s]
|
21
|
+
else
|
22
|
+
asset_path
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module CloudFront
|
2
|
+
class HashedRequestInterpreter
|
3
|
+
def initialize(app)
|
4
|
+
@app = app
|
5
|
+
end
|
6
|
+
|
7
|
+
def call(env)
|
8
|
+
setup_path_info!(env)
|
9
|
+
@app.call(env)
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
|
16
|
+
def setup_path_info!(env)
|
17
|
+
env['PATH_INFO'].gsub!(%r{^/#{ENV['COMMIT_HASH']}}, '')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rails/railtie'
|
2
|
+
|
3
|
+
module CloudFront
|
4
|
+
class Railtie < ::Rails::Railtie
|
5
|
+
config.cloud_front = ActiveSupport::OrderedOptions.new
|
6
|
+
|
7
|
+
initializer 'cloud_front.configure' do |app|
|
8
|
+
CloudFront.domain = app.config.cloud_front.domain
|
9
|
+
end
|
10
|
+
|
11
|
+
initializer 'cloud_front.insert_middleware', :before => 'cloud_front.configure' do |app|
|
12
|
+
insert_middleware(app) if enabled?(app)
|
13
|
+
end
|
14
|
+
|
15
|
+
initializer 'cloud_front.configure_asset_host', :before => 'cloud_front.insert_middleware' do |app|
|
16
|
+
setup_asset_host(app) if enabled?(app)
|
17
|
+
end
|
18
|
+
|
19
|
+
initializer 'cloud_front.configure_asset_path', :before => 'cloud_front.configure_asset_host' do |app|
|
20
|
+
setup_asset_path(app) if enabled?(app)
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
def enabled?(app)
|
26
|
+
app.config.cloud_front.domain.present?
|
27
|
+
end
|
28
|
+
private :enabled?
|
29
|
+
|
30
|
+
def insert_middleware(app)
|
31
|
+
app.config.middleware.
|
32
|
+
insert_before(ActionDispatch::Static,
|
33
|
+
CloudFront::HashedRequestInterpreter)
|
34
|
+
rescue
|
35
|
+
app.config.middleware.insert 0, CloudFront::HashedRequestInterpreter
|
36
|
+
end
|
37
|
+
private :insert_middleware
|
38
|
+
|
39
|
+
def setup_asset_host(app)
|
40
|
+
app.config.action_controller.asset_host = lambda do |source, request|
|
41
|
+
CloudFront.asset_host(source, request)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
private :setup_asset_host
|
45
|
+
|
46
|
+
def setup_asset_path(app)
|
47
|
+
app.config.action_controller.asset_path = lambda do |path|
|
48
|
+
CloudFront.asset_path(path)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
private :setup_asset_path
|
52
|
+
end
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cloud_front
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Nathaniel Bibler
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-05-11 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: sha_header
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 31
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
- 0
|
33
|
+
version: 0.0.0
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
description: This library sets up your application to use the Amazon CloudFront CDN by modifying your Rails 3 asset_host to the host you specify. It also allows you to use your Rails 3 application directly as the origin server for CloudFront alleviating the need to upload your resources to Amazon S3. The included middleware manages recognizing and re-routing CloudFront-based requests to local assets.
|
37
|
+
email:
|
38
|
+
- gem@nathanielbibler.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- .gitignore
|
47
|
+
- Gemfile
|
48
|
+
- Rakefile
|
49
|
+
- cloud_front.gemspec
|
50
|
+
- lib/cloud_front.rb
|
51
|
+
- lib/cloud_front/hashed_request_interpreter.rb
|
52
|
+
- lib/cloud_front/railtie.rb
|
53
|
+
- lib/cloud_front/version.rb
|
54
|
+
homepage: https://github.com/nbibler/cloud_front
|
55
|
+
licenses: []
|
56
|
+
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: 3
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.8.1
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: A Rack middleware and Rails 3 configuration for using the Amazon CloudFront CDN as a front for your application as a custom origin.
|
87
|
+
test_files: []
|
88
|
+
|