rack-mogilefs 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/HISTORY.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.2.0 (2010-11-29)
2
+
3
+ * Add X-Reproxy-Url support
4
+
1
5
  ## 0.1.3 (2010-09-10)
2
6
 
3
7
  * Depend on rack/mime instead of mime-types
data/README.md CHANGED
@@ -2,7 +2,34 @@
2
2
 
3
3
  If you are using Nginx you'll probably want to serve MogileFS files with
4
4
  the [MogileFS Module](http://www.grid.net.ru/nginx/mogilefs.en.html), but if
5
- you need to serve them out of a Rack app, this should help.
5
+ you need a quick way to serve them out of a Rack app, this should help.
6
+
7
+ ## Caveats ( serving files vs reproxying )
8
+
9
+ Serving files through Ruby is slow. The preferred method is to set an
10
+ X-Reproxy-Url header from your app and let the web server serve the file. For
11
+ Nginx, you could have a config like this:
12
+
13
+ location /reproxy {
14
+ internal;
15
+ set $reproxy $upstream_http_x_reproxy_url;
16
+ proxy_pass $reproxy;
17
+ proxy_hide_header Content-Type;
18
+ }
19
+
20
+ For Apache, there is [mod_reproxy](http://github.com/jamis/mod_reproxy)
21
+
22
+ `Rack::MogileFS` will use this method if you pass a strategy option of `:reproxy`
23
+
24
+ use Rack::MogileFS, :strategy => :reproxy
25
+
26
+ `Rack::MogileFS` will look up the internal urls for the file, and set two
27
+ headers to reproxy the request:
28
+
29
+ X-Accel-Redirect: /reproxy
30
+ X-Reproxy-Url: http://internal.ip/path/to/mogile/file.fid
31
+
32
+ You'll have to make sure your web server knows how to handle this request.
6
33
 
7
34
  ## Getting Started:
8
35
 
@@ -4,8 +4,9 @@ module Rack
4
4
 
5
5
  def initialize(options={})
6
6
  @options = {
7
- :client => nil,
8
- :mapper => nil,
7
+ :client => nil,
8
+ :mapper => nil,
9
+ :strategy => :serve,
9
10
  :default_content_type => "image/png"
10
11
  }.merge(options)
11
12
 
@@ -14,13 +15,13 @@ module Rack
14
15
 
15
16
  def call(env)
16
17
  path = key_for_path(env['PATH_INFO'].dup)
17
- data = client.get_file_data(path)
18
- size = Utils.bytesize(data).to_s
19
18
 
20
- [ 200, {
21
- "Content-Type" => content_type(path),
22
- "Content-Length" => size
23
- }, [data] ]
19
+ if @options[:strategy] == :reproxy
20
+ reproxy(path)
21
+ else
22
+ serve_file(path)
23
+ end
24
+
24
25
  rescue ::MogileFS::Backend::UnknownKeyError => e
25
26
  [ 404, { "Content-Type" => "text/html" }, [e.message] ]
26
27
  rescue ::MogileFS::UnreachableBackendError => e
@@ -31,6 +32,26 @@ module Rack
31
32
 
32
33
  protected
33
34
 
35
+ def reproxy(path)
36
+ paths = client.get_paths(path)
37
+
38
+ [ 200, {
39
+ "Content-Type" => content_type(path),
40
+ "X-Accel-Redirect" => "/reproxy",
41
+ "X-Reproxy-Url" => paths.join(" ")
42
+ }, [] ]
43
+ end
44
+
45
+ def serve_file(path)
46
+ data = client.get_file_data(path)
47
+ size = Utils.bytesize(data).to_s
48
+
49
+ [ 200, {
50
+ "Content-Type" => content_type(path),
51
+ "Content-Length" => size
52
+ }, [data] ]
53
+ end
54
+
34
55
  def key_for_path(path)
35
56
  @options[:mapper].respond_to?(:call) ? @options[:mapper].call(path) : path
36
57
  end
@@ -39,10 +60,6 @@ module Rack
39
60
  Mime.mime_type(::File.extname(path), @options[:default_content_type])
40
61
  end
41
62
 
42
- def content_length(data)
43
- Utils.bytesize(data)
44
- end
45
-
46
63
  def client
47
64
  @options[:client]
48
65
  end
data/lib/rack/mogilefs.rb CHANGED
@@ -12,10 +12,14 @@ module Rack
12
12
 
13
13
  def call(env)
14
14
  if env['PATH_INFO'] =~ @options[:path]
15
- Endpoint.new(@options).call(env)
15
+ endpoint.call(env)
16
16
  else
17
17
  @app.call(env)
18
18
  end
19
19
  end
20
+
21
+ def endpoint
22
+ @endpoint ||= Endpoint.new(@options)
23
+ end
20
24
  end
21
25
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-mogilefs
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 3
10
- version: 0.1.3
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ben Marini