rack-mogilefs 0.1.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/README.md +63 -0
- data/Rakefile +12 -0
- data/lib/rack/mogilefs/endpoint.rb +53 -0
- data/lib/rack/mogilefs.rb +20 -0
- data/lib/rack-mogilefs.rb +1 -0
- data/test/mogilefs_test.rb +42 -0
- data/test/test_helper.rb +6 -0
- metadata +116 -0
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Rack middleware/endpoint for serving MogileFS files
|
2
|
+
|
3
|
+
## Getting Started:
|
4
|
+
|
5
|
+
First install the gem:
|
6
|
+
|
7
|
+
gem install rack-mogilefs
|
8
|
+
|
9
|
+
There are a variety of ways to use it:
|
10
|
+
|
11
|
+
### Rack middleware
|
12
|
+
# (config.ru)
|
13
|
+
use Rack::MogileFS, :path => %r{^/system/*}
|
14
|
+
|
15
|
+
### Rails 3:
|
16
|
+
|
17
|
+
# (config/routes.rb)
|
18
|
+
match "/system/*" => Rack::MogileFS::Endpoint.new
|
19
|
+
|
20
|
+
### Rails 2:
|
21
|
+
|
22
|
+
# (app/metal/mogilefs.rb)
|
23
|
+
class MogileFSMetal
|
24
|
+
def self.call(env)
|
25
|
+
if env["PATH_INFO"] =~ %r{^/system/*}
|
26
|
+
Rack::MogileFS::Endpoint.new.call(env)
|
27
|
+
else
|
28
|
+
[ 404, { "Content-Type" => "text/html" }, ["Not Found"] ]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
## File Lookup
|
34
|
+
|
35
|
+
`Rack::MogileFS` uses the request path (`PATH_INFO`) for the MogileFS key. The
|
36
|
+
content type of the file is detected by the extension, using the `mime-types`
|
37
|
+
gem.
|
38
|
+
|
39
|
+
## Configuration
|
40
|
+
|
41
|
+
By default will look for a yaml config in `config/mogilefs.yml` that looks
|
42
|
+
like this:
|
43
|
+
|
44
|
+
development:
|
45
|
+
connection:
|
46
|
+
domain: "development.myapp.com"
|
47
|
+
hosts:
|
48
|
+
- 127.0.0.1:7001
|
49
|
+
- 127.0.0.1:7001
|
50
|
+
class: "file"
|
51
|
+
staging:
|
52
|
+
...
|
53
|
+
|
54
|
+
|
55
|
+
and initialize a mogilefs client like this:
|
56
|
+
|
57
|
+
config = YAML.load_file( Rails.root.join("config/mogilefs.yml") )[Rails.env]
|
58
|
+
MogileFS::MogileFS.new( config["connection"].symbolize_keys )
|
59
|
+
|
60
|
+
You can override this by passing in a MogileFS client of your own:
|
61
|
+
|
62
|
+
Rack::MogileFS::Endpoint.new :client => MyMogileFSClient.new
|
63
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
module Rack
|
2
|
+
class MogileFS
|
3
|
+
class Endpoint
|
4
|
+
|
5
|
+
def initialize(options={})
|
6
|
+
@options = {
|
7
|
+
:client => nil,
|
8
|
+
:default_content_type => "image/png"
|
9
|
+
}.merge(options)
|
10
|
+
|
11
|
+
@options[:client] ||= default_client
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
data = client.get_file_data(env['PATH_INFO'])
|
16
|
+
[ 200, { "Content-Type" => content_type(env['PATH_INFO']) }, [ data ] ]
|
17
|
+
rescue ::MogileFS::Backend::UnknownKeyError
|
18
|
+
[ 404, { "Content-Type" => "text/html" }, ["Not Found"] ]
|
19
|
+
rescue ::MogileFS::UnreachableBackendError => e
|
20
|
+
[ 503, { "Content-Type" => "text/html" }, [e.message] ]
|
21
|
+
rescue ::MogileFS::Error => e
|
22
|
+
[ 500, { "Content-Type" => "text/html" }, [e.message] ]
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
|
27
|
+
def content_type(path_info)
|
28
|
+
ext = path_info.split(".").last
|
29
|
+
MIME::Types.type_for(ext).first.content_type
|
30
|
+
rescue
|
31
|
+
@options[:default_content_type]
|
32
|
+
end
|
33
|
+
|
34
|
+
def client
|
35
|
+
@options[:client]
|
36
|
+
end
|
37
|
+
|
38
|
+
def default_client
|
39
|
+
::MogileFS::MogileFS.new(config)
|
40
|
+
end
|
41
|
+
|
42
|
+
def config
|
43
|
+
if defined?(Rails)
|
44
|
+
yml = YAML.load_file( Rails.root.join("config/mogilefs.yml") )
|
45
|
+
yml[Rails.env]["connection"].symbolize_keys
|
46
|
+
else
|
47
|
+
{ :domain => "default", :hosts => ["127.0.0.1:7001"] }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'mogilefs'
|
2
|
+
require 'mime/types'
|
3
|
+
|
4
|
+
module Rack
|
5
|
+
class MogileFS
|
6
|
+
autoload :Endpoint, "rack/mogilefs/endpoint"
|
7
|
+
|
8
|
+
def initialize(app, options={})
|
9
|
+
@app, @options = app, options
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(env)
|
13
|
+
if env['PATH_INFO'] =~ @options[:path]
|
14
|
+
Endpoint.new(@options).call(env)
|
15
|
+
else
|
16
|
+
@app.call(env)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'rack/mogilefs'
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestMiddleWare < Test::Unit::TestCase
|
4
|
+
class MockMogileFsClient
|
5
|
+
def get_file_data(key)
|
6
|
+
"Mock Data"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_middleware_with_path
|
11
|
+
app = Rack::Builder.new do
|
12
|
+
use Rack::Lint
|
13
|
+
use Rack::MogileFS, :path => %r{^/system/*}
|
14
|
+
use Rack::Lint
|
15
|
+
run lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, World!"] }
|
16
|
+
end
|
17
|
+
|
18
|
+
response = Rack::MockRequest.new(app).get('/')
|
19
|
+
assert_equal "Hello, World!", response.body
|
20
|
+
|
21
|
+
response = Rack::MockRequest.new(app).get('/system/unreachable.txt')
|
22
|
+
assert_equal "couldn't connect to mogilefsd backend", response.body
|
23
|
+
assert_equal 503, response.status
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_middleware_with_mock_mogile_client
|
27
|
+
app = Rack::Builder.new do
|
28
|
+
use Rack::Lint
|
29
|
+
use Rack::MogileFS, :path => %r{^/system/*}, :client => MockMogileFsClient.new
|
30
|
+
use Rack::Lint
|
31
|
+
run lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, World!"] }
|
32
|
+
end
|
33
|
+
|
34
|
+
response = Rack::MockRequest.new(app).get('/')
|
35
|
+
assert_equal "Hello, World!", response.body
|
36
|
+
|
37
|
+
response = Rack::MockRequest.new(app).get('/system/mocked.txt')
|
38
|
+
assert_equal "Mock Data", response.body
|
39
|
+
assert_equal 200, response.status
|
40
|
+
assert_equal "text/plain", response.headers['Content-Type']
|
41
|
+
end
|
42
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-mogilefs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ben Marini
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-08 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: mime-types
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - "="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 47
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 16
|
33
|
+
version: "1.16"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rack
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: mocha
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
type: :development
|
63
|
+
version_requirements: *id003
|
64
|
+
description: A rack middleware and/or endpoint to serve up files from MogileFS
|
65
|
+
email: bmarini@gmail.com
|
66
|
+
executables: []
|
67
|
+
|
68
|
+
extensions: []
|
69
|
+
|
70
|
+
extra_rdoc_files: []
|
71
|
+
|
72
|
+
files:
|
73
|
+
- lib/rack/mogilefs/endpoint.rb
|
74
|
+
- lib/rack/mogilefs.rb
|
75
|
+
- lib/rack-mogilefs.rb
|
76
|
+
- README.md
|
77
|
+
- Rakefile
|
78
|
+
- test/mogilefs_test.rb
|
79
|
+
- test/test_helper.rb
|
80
|
+
has_rdoc: true
|
81
|
+
homepage: http://github.com/bmarini/rack-mogilefs
|
82
|
+
licenses: []
|
83
|
+
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: 3
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
hash: 3
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
version: "0"
|
107
|
+
requirements: []
|
108
|
+
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 1.3.7
|
111
|
+
signing_key:
|
112
|
+
specification_version: 3
|
113
|
+
summary: A rack middleware and/or endpoint to serve up files from MogileFS
|
114
|
+
test_files:
|
115
|
+
- test/mogilefs_test.rb
|
116
|
+
- test/test_helper.rb
|