thrift-rack-middleware 0.0.1

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.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in thrift-rack-middleware.gemspec
4
+ gemspec
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ desc 'Default: run specs.'
5
+ task :default => :spec
6
+
7
+ desc "Run specs"
8
+ RSpec::Core::RakeTask.new do |t|
9
+ t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
10
+ # Put spec opts in a file named .rspec in root
11
+ end
@@ -0,0 +1,74 @@
1
+ #
2
+ # Licensed to the Apache Software Foundation (ASF) under one
3
+ # or more contributor license agreements. See the NOTICE file
4
+ # distributed with this work for additional information
5
+ # regarding copyright ownership. The ASF licenses this file
6
+ # to you under the Apache License, Version 2.0 (the
7
+ # "License"); you may not use this file except in compliance
8
+ # with the License. You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing,
13
+ # software distributed under the License is distributed on an
14
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ # KIND, either express or implied. See the License for the
16
+ # specific language governing permissions and limitations
17
+ # under the License.
18
+ #
19
+
20
+ # A Rack application to be used within a Rails application to provide an API
21
+ # access via Thrift. You have to insert it into the MiddlewareStack of Rails
22
+ # within a custom initializer and not within the environment, because Thrift
23
+ # is not fully loaded at that point.
24
+ #
25
+ # Here is a sample of to to use it:
26
+ #
27
+ # ActionController::Dispatcher.middleware.insert_before Rails::Rack::Metal, Thrift::RackMiddleware,
28
+ # { :processor => YourCustomProcessor.new,
29
+ # :hook_path => "/the_path_to_receive_api_calls",
30
+ # :protocol_factory => Thrift::BinaryProtocolAcceleratedFactory.new }
31
+ #
32
+ # Some benchmarking showed this is much slower then any Thrift solution without
33
+ # Rails, but it is still fast enough if you need to integrate your Rails app
34
+ # into a Thrift-based infrastructure.
35
+ #
36
+ begin
37
+ require 'rack'
38
+ require 'rack/response'
39
+ require 'rack/request'
40
+ rescue LoadError => e
41
+ Kernel.warn "[WARNING] The Rack library could not be found. Please install it to use the Thrift::RackMiddleware server part."
42
+ end
43
+
44
+ require "thrift"
45
+
46
+ module Thrift
47
+ class RackMiddleware
48
+ attr_reader :hook_path, :processor, :protocol_factory
49
+
50
+ def initialize(app, options = {})
51
+ @app = app
52
+ @processor = options[:processor] || (raise ArgumentError, "You have to specify a processor.")
53
+ @protocol_factory = options[:protocol_factory] || BinaryProtocolFactory.new
54
+ @hook_path = options[:hook_path] || "/rpc_api"
55
+ end
56
+
57
+ def call(env)
58
+ request = Rack::Request.new(env)
59
+ if request.post? && request.path == hook_path
60
+ output = StringIO.new
61
+ transport = IOStreamTransport.new(request.body, output)
62
+ protocol = @protocol_factory.get_protocol(transport)
63
+ @processor.process(protocol, protocol)
64
+
65
+ output.rewind
66
+ response = Rack::Response.new(output)
67
+ response["Content-Type"] = "application/x-thrift"
68
+ response.finish
69
+ else
70
+ @app.call(env)
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,7 @@
1
+ module Thrift
2
+ module Rack
3
+ module Middleware
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,95 @@
1
+ #
2
+ # Licensed to the Apache Software Foundation (ASF) under one
3
+ # or more contributor license agreements. See the NOTICE file
4
+ # distributed with this work for additional information
5
+ # regarding copyright ownership. The ASF licenses this file
6
+ # to you under the Apache License, Version 2.0 (the
7
+ # "License"); you may not use this file except in compliance
8
+ # with the License. You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing,
13
+ # software distributed under the License is distributed on an
14
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ # KIND, either express or implied. See the License for the
16
+ # specific language governing permissions and limitations
17
+ # under the License.
18
+ #
19
+
20
+ require 'spec_helper'
21
+ require 'thrift/rack_middleware'
22
+
23
+ include Thrift
24
+
25
+ describe RackMiddleware do
26
+ before(:each) do
27
+ @processor = mock("Processor")
28
+ @factory = mock("ProtocolFactory")
29
+ @mock_app = mock("AnotherRackApp")
30
+ @middleware = RackMiddleware.new(@mock_app, :processor => @processor, :protocol_factory => @factory)
31
+ end
32
+
33
+ it "should call next rack application in the stack if request was not a post and not pointed to the hook_path" do
34
+ env = {"REQUEST_METHOD" => "GET", "PATH_INFO" => "not_the_hook_path"}
35
+ @mock_app.should_receive(:call).with(env)
36
+ Rack::Response.should_not_receive(:new)
37
+ @middleware.call(env)
38
+ end
39
+
40
+ it "should serve using application/x-thrift" do
41
+ env = {"REQUEST_METHOD" => "POST", "PATH_INFO" => "/rpc_api"}
42
+ IOStreamTransport.stub!(:new)
43
+ @factory.stub!(:get_protocol)
44
+ @processor.stub!(:process)
45
+ response = mock("RackResponse")
46
+ response.should_receive(:[]=).with("Content-Type", "application/x-thrift")
47
+ response.should_receive(:finish)
48
+ Rack::Response.should_receive(:new).and_return(response)
49
+ @middleware.call(env)
50
+ end
51
+
52
+ it "should use the IOStreamTransport" do
53
+ body = mock("body")
54
+ env = {"REQUEST_METHOD" => "POST", "PATH_INFO" => "/rpc_api", "rack.input" => body}
55
+ output = mock("output")
56
+ output.should_receive(:rewind)
57
+ StringIO.should_receive(:new).and_return(output)
58
+ protocol = mock("protocol")
59
+ transport = mock("transport")
60
+ IOStreamTransport.should_receive(:new).with(body, output).and_return(transport)
61
+ @factory.should_receive(:get_protocol).with(transport).and_return(protocol)
62
+ @processor.should_receive(:process).with(protocol, protocol)
63
+ response = mock("RackResponse")
64
+ response.stub!(:[]=)
65
+ response.should_receive(:finish)
66
+ Rack::Response.should_receive(:new).and_return(response)
67
+ @middleware.call(env)
68
+ end
69
+
70
+ it "should have appropriate defaults for hook_path and protocol_factory" do
71
+ mock_factory = mock("BinaryProtocolFactory")
72
+ mock_proc = mock("Processor")
73
+ BinaryProtocolFactory.should_receive(:new).and_return(mock_factory)
74
+ rack_middleware = RackMiddleware.new(@mock_app, :processor => mock_proc)
75
+ rack_middleware.hook_path.should == "/rpc_api"
76
+ end
77
+
78
+ it "should understand :hook_path, :processor and :protocol_factory" do
79
+ mock_proc = mock("Processor")
80
+ mock_factory = mock("ProtocolFactory")
81
+
82
+ rack_middleware = RackMiddleware.new(@mock_app, :processor => mock_proc,
83
+ :protocol_factory => mock_factory,
84
+ :hook_path => "/thrift_api")
85
+
86
+ rack_middleware.processor.should == mock_proc
87
+ rack_middleware.protocol_factory.should == mock_factory
88
+ rack_middleware.hook_path.should == "/thrift_api"
89
+ end
90
+
91
+ it "should raise ArgumentError if no processor was specified" do
92
+ lambda { RackMiddleware.new(@mock_app) }.should raise_error(ArgumentError)
93
+ end
94
+ end
95
+
@@ -0,0 +1,3 @@
1
+ RSpec.configure do |config|
2
+ #
3
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "thrift/rack_middleware/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "thrift-rack-middleware"
7
+ s.version = Thrift::Rack::Middleware::VERSION
8
+ s.authors = ["deees"]
9
+ s.email = ["tomas.brazys@gmail.com"]
10
+ s.homepage = "http://github.com/deees/thrift-rack-middleware"
11
+ s.summary = "Thrift rack middleware"
12
+ s.description = "Rack middleware for thrift services"
13
+
14
+ s.rubyforge_project = "thrift-rack-middleware"
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
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+ s.add_runtime_dependency "rack"
24
+ s.add_runtime_dependency "thrift"
25
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thrift-rack-middleware
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - deees
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-03-20 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: rack
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :runtime
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: thrift
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id003
48
+ description: Rack middleware for thrift services
49
+ email:
50
+ - tomas.brazys@gmail.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - .gitignore
59
+ - Gemfile
60
+ - Rakefile
61
+ - lib/thrift/rack_middleware.rb
62
+ - lib/thrift/rack_middleware/version.rb
63
+ - spec/rack_middleware_spec.rb
64
+ - spec/spec_helper.rb
65
+ - thrift-rack-middleware.gemspec
66
+ homepage: http://github.com/deees/thrift-rack-middleware
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options: []
71
+
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ requirements: []
87
+
88
+ rubyforge_project: thrift-rack-middleware
89
+ rubygems_version: 1.8.9
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Thrift rack middleware
93
+ test_files:
94
+ - spec/rack_middleware_spec.rb
95
+ - spec/spec_helper.rb