rack-static_fallback 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +2 -0
- data/lib/rack/generate_readme.rb +11 -0
- data/lib/rack/static_fallback.rb +81 -0
- data/rack-static_fallback.gemspec +23 -0
- metadata +88 -0
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
Bounces or redirects requests to missing static files.
|
2
|
+
Partially inspired by [http://menendez.com/blog/using-django-as-pass-through-image-proxy/](http://menendez.com/blog/using-django-as-pass-through-image-proxy/)
|
3
|
+
|
4
|
+
I.e. could be useful when you want to run the server with production database locally
|
5
|
+
and have user uploaded content fetched transparently from production site.
|
6
|
+
|
7
|
+
Options:
|
8
|
+
:mode - [ :off,
|
9
|
+
:bounce, # returns 404 to any request to static URL,
|
10
|
+
:fallback ] # any request to static path is redirected to :fallback_static_url
|
11
|
+
:static_path_regex - Regexp which matches the path to your static files.
|
12
|
+
Along the lines of the Capistrano conventions defaults to `%r{/system/(audios|photos|videos)}`
|
13
|
+
:fallback_static_url - URL of the production site
|
14
|
+
|
15
|
+
Install via rubygems:
|
16
|
+
|
17
|
+
gem install rack-static_fallback
|
18
|
+
|
19
|
+
then, for Rails (any version which supports Rack) apps, add the following to your 'config/environments/development.rb'
|
20
|
+
|
21
|
+
config.middleware.insert_after ::Rack::Lock,
|
22
|
+
::Rack::StaticFallback, :mode => :fallback,
|
23
|
+
:static_path_regex => %r{/system/uploads},
|
24
|
+
:fallback_static_url => "http://myproductionsite.com/"
|
25
|
+
|
26
|
+
note that Rails 3 won't auto-load the gem files, so you may need to
|
27
|
+
|
28
|
+
require 'rack/static_fallback'
|
29
|
+
|
30
|
+
at the beginning of 'development.rb'. Aternatively, you should also be able to use the gem in non-Rails apps with:
|
31
|
+
|
32
|
+
use ::Rack::StaticFallback, :mode => :fallback,
|
33
|
+
:static_path_regex => %r{/system/uploads},
|
34
|
+
:fallback_static_url => "http://myproductionsite.com/"
|
35
|
+
|
36
|
+
in your 'config.ru' (this is untested so mileage may vary)
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
File.open(File.dirname(__FILE__) + "/../../README.md", "w") do |f|
|
2
|
+
first_comment = []
|
3
|
+
IO.read(File.dirname(__FILE__) + "/static_fallback.rb").each_line do |l|
|
4
|
+
if l =~ /\s*#/
|
5
|
+
first_comment << l.sub(/^\s*#\s/, "").rstrip
|
6
|
+
elsif !first_comment.empty?
|
7
|
+
break
|
8
|
+
end
|
9
|
+
end
|
10
|
+
f.write(first_comment.join("\n"))
|
11
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module Rack
|
2
|
+
# Bounces or redirects requests to missing static files.
|
3
|
+
# Partially inspired by [http://menendez.com/blog/using-django-as-pass-through-image-proxy/](http://menendez.com/blog/using-django-as-pass-through-image-proxy/)
|
4
|
+
#
|
5
|
+
# I.e. could be useful when you want to run the server with production database locally
|
6
|
+
# and have user uploaded content fetched transparently from production site.
|
7
|
+
#
|
8
|
+
# Options:
|
9
|
+
# :mode - [ :off,
|
10
|
+
# :bounce, # returns 404 to any request to static URL,
|
11
|
+
# :fallback ] # any request to static path is redirected to :fallback_static_url
|
12
|
+
# :static_path_regex - Regexp which matches the path to your static files.
|
13
|
+
# Along the lines of the Capistrano conventions defaults to `%r{/system/(audios|photos|videos)}`
|
14
|
+
# :fallback_static_url - URL of the production site
|
15
|
+
#
|
16
|
+
# Install via rubygems:
|
17
|
+
#
|
18
|
+
# gem install rack-static_fallback
|
19
|
+
#
|
20
|
+
# then, for Rails (any version which supports Rack) apps, add the following to your 'config/environments/development.rb'
|
21
|
+
#
|
22
|
+
# config.middleware.insert_after ::Rack::Lock,
|
23
|
+
# ::Rack::StaticFallback, :mode => :fallback,
|
24
|
+
# :static_path_regex => %r{/system/uploads},
|
25
|
+
# :fallback_static_url => "http://myproductionsite.com/"
|
26
|
+
#
|
27
|
+
# note that Rails 3 won't auto-load the gem files, so you may need to
|
28
|
+
#
|
29
|
+
# require 'rack/static_fallback'
|
30
|
+
#
|
31
|
+
# at the beginning of 'development.rb'. Aternatively, you should also be able to use the gem in non-Rails apps with:
|
32
|
+
#
|
33
|
+
# use ::Rack::StaticFallback, :mode => :fallback,
|
34
|
+
# :static_path_regex => %r{/system/uploads},
|
35
|
+
# :fallback_static_url => "http://myproductionsite.com/"
|
36
|
+
#
|
37
|
+
# in your 'config.ru' (this is untested so mileage may vary)
|
38
|
+
#
|
39
|
+
|
40
|
+
class StaticFallback
|
41
|
+
def initialize(app, options = {})
|
42
|
+
@app = app
|
43
|
+
@mode = options[:mode] || :off
|
44
|
+
# along the lines of the Capistrano defaults
|
45
|
+
@static_path_regex = options[:static_path_regex] || %r{/system/(audios|photos|videos)}
|
46
|
+
@fallback_static_url = options[:fallback_static_url]
|
47
|
+
|
48
|
+
if @mode == :fallback && !@fallback_static_url
|
49
|
+
raise ArgumentError, "Rack::StaticFallback :mode => :fallback option specified without :fallback_static_url option"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def call(env)
|
54
|
+
if env["PATH_INFO"] =~ @static_path_regex
|
55
|
+
# If we get here that means that underlying web server wasn't able to serve the static file,
|
56
|
+
# i.e. it wasn't found.
|
57
|
+
case @mode
|
58
|
+
when :off
|
59
|
+
# pass the request to next middleware, ultimately Rails
|
60
|
+
@app.call(env)
|
61
|
+
|
62
|
+
when :bounce
|
63
|
+
# don't pass the request so that it doesn't hit framework, which
|
64
|
+
# speeds up things significantly
|
65
|
+
not_found
|
66
|
+
|
67
|
+
when :fallback
|
68
|
+
# redirect request to the production server
|
69
|
+
[ 302, { "Location" => ::File.join(@fallback_static_url, env["PATH_INFO"]) }, [] ]
|
70
|
+
end
|
71
|
+
|
72
|
+
else
|
73
|
+
@app.call(env)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def not_found
|
78
|
+
[ 404, { "Content-Type" => "text/html", "Content-Length" => "0" }, [] ]
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "rack-static_fallback"
|
6
|
+
s.version = '1.0.0'
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Evgeniy Dolzhenko", "Mike Fulcher"]
|
9
|
+
s.email = ["dolzenko@gmail.com", "mike@plan9design.co.uk"]
|
10
|
+
s.homepage = "http://github.com/dolzenko/rack-static_fallback"
|
11
|
+
s.summary = "Bounces or redirects requests to missing static files"
|
12
|
+
s.description = "Bounces or redirects requests to missing static files. Could be useful when you want to run the server with production database locally and have user uploaded content fetched transparently from production site."
|
13
|
+
|
14
|
+
s.rubyforge_project = "rack-static_fallback"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# Runtime
|
22
|
+
s.add_runtime_dependency 'rack'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-static_fallback
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Evgeniy Dolzhenko
|
14
|
+
- Mike Fulcher
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-02-08 00:00:00 +00:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: rack
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
version: "0"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
description: Bounces or redirects requests to missing static files. Could be useful when you want to run the server with production database locally and have user uploaded content fetched transparently from production site.
|
37
|
+
email:
|
38
|
+
- dolzenko@gmail.com
|
39
|
+
- mike@plan9design.co.uk
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- Gemfile
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- lib/rack/generate_readme.rb
|
51
|
+
- lib/rack/static_fallback.rb
|
52
|
+
- rack-static_fallback.gemspec
|
53
|
+
has_rdoc: true
|
54
|
+
homepage: http://github.com/dolzenko/rack-static_fallback
|
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: rack-static_fallback
|
83
|
+
rubygems_version: 1.5.0
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Bounces or redirects requests to missing static files
|
87
|
+
test_files: []
|
88
|
+
|