rack-forwarder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a4c9b8f710ca294e07f789ec24a2348253289ef
4
+ data.tar.gz: 2f638516a8a1136def94e159a826fd0ed01307a1
5
+ SHA512:
6
+ metadata.gz: bc1dd09ffdf72f5a20aa4f121632e242c15ebb0be5b762bb20e02977e8415818211df4dad8780b855d9def7625aaf552f8fa9b1717acbd4daa22ef2bf58ca9cc
7
+ data.tar.gz: d7307c3d49c3fa00abf103e0ee2a5159ff2025d6252f5919a35315c77d877a00f6e6813ab4acf6638564e7af3e57825dff257e6708ac93237076545c41455931
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-forwarder.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Tim Cooper
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Rack::Forwarder
2
+
3
+ Declarative forwarding rack middleware based on
4
+ https://gist.github.com/chneukirchen/32376.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "rack-forwarder"
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install rack-forwarder
21
+
22
+ ## Usage
23
+
24
+ ``` ruby
25
+ require "rack/forwarder"
26
+
27
+ use Rack::Forwarder do
28
+ # Forward /foo/bar to http://localhost:5000/bar
29
+ forward %r{/foo(/bar)}, to: "http://localhost:5000$1"
30
+
31
+ # Forward /foo to http://localhost:5000/foo
32
+ forward %r{/foo}, to: "http://localhost:5000"
33
+ end
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it ( https://github.com/[my-github-username]/rack-forwarder/fork )
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,20 @@
1
+ module Rack
2
+ class Forwarder
3
+ class Registry
4
+ Matcher = Struct.new(:regexp, :to) do
5
+ def match?(path)
6
+ regexp =~ path
7
+ end
8
+
9
+ def url_from(path)
10
+ return URI.join(to, path).to_s unless to =~ /\$\d+/
11
+
12
+ regexp.match(path).captures.to_enum
13
+ .with_index(1).each_with_object(to) do |(match, index), url|
14
+ url.gsub!("$#{index}", match)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ module Rack
2
+ class Forwarder
3
+ class Registry
4
+ def initialize
5
+ @store = []
6
+ end
7
+
8
+ def register(regexp, to)
9
+ @store << Matcher.new(regexp, to)
10
+ end
11
+
12
+ def match?(path)
13
+ @store.find { |matcher| matcher.match?(path) }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class Forwarder
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,61 @@
1
+ require "excon"
2
+ require "rack"
3
+
4
+ require "rack/forwarder/version"
5
+ require "rack/forwarder/matcher"
6
+ require "rack/forwarder/registry"
7
+
8
+ module Rack
9
+ class Forwarder
10
+ HEADERS_TO_FORWARD = %w(
11
+ Content-Type
12
+ Content-Length
13
+ )
14
+
15
+ def initialize(app, options = {}, &block)
16
+ @app = app
17
+ @matchers = Registry.new
18
+ @options = options
19
+ instance_eval(&block)
20
+ end
21
+
22
+ def forward(regexp, to:)
23
+ @matchers.register(regexp, to)
24
+ end
25
+
26
+ def call(env)
27
+ request = Request.new(env)
28
+ matcher = @matchers.match?(request.path)
29
+ return @app.call(env) unless matcher
30
+
31
+ request_method = request.request_method.to_s.downcase
32
+ options = {
33
+ headers: extract_http_headers(env),
34
+ }.merge(@options)
35
+ response = Excon.public_send(
36
+ request_method,
37
+ matcher.url_from(request.path),
38
+ options,
39
+ )
40
+
41
+ [response.status, headers_from_response(response.headers), [response.body]]
42
+ end
43
+
44
+ private
45
+
46
+ def extract_http_headers(env)
47
+ headers = env.each_with_object(Utils::HeaderHash.new) do |(key, value), hash|
48
+ hash[$1] = value if key =~ /HTTP_(.*)/
49
+ end
50
+ headers["X-Request-Id"] = env["action_dispatch.request_id"]
51
+
52
+ headers
53
+ end
54
+
55
+ def headers_from_response(headers)
56
+ HEADERS_TO_FORWARD.each_with_object(Utils::HeaderHash.new) do |header, hash|
57
+ hash[header] = headers[header]
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/forwarder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-forwarder"
8
+ spec.version = Rack::Forwarder::VERSION
9
+ spec.authors = ["Tim Cooper"]
10
+ spec.email = ["coop@latrobest.com"]
11
+ spec.summary = "Forward requests"
12
+ spec.description = spec.summary
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rack-test"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+
26
+ spec.add_dependency "rack"
27
+ spec.add_dependency "excon"
28
+ end
@@ -0,0 +1,42 @@
1
+ require "spec_helper"
2
+ require "rack/test"
3
+
4
+ RSpec.describe Rack::Forwarder do
5
+ include Rack::Test::Methods
6
+
7
+ let(:app) do
8
+ Rack::Builder.new do
9
+ use Rack::Forwarder do
10
+ forward %r{/foo(/bar)}, to: "http://example.com$1"
11
+ forward %r{/foo}, to: "http://example.com"
12
+ end
13
+
14
+ run ->(env) { [200, {"Content-Type" => "text/plain"}, ["hello world"]] }
15
+ end
16
+ end
17
+
18
+ context "none matching" do
19
+ it "is successful" do
20
+ get "/"
21
+
22
+ expect(last_response).to be_successful
23
+ expect(last_response.body).to eq("hello world")
24
+ end
25
+ end
26
+
27
+ context "forwarding" do
28
+ it "forwards the specified regex" do
29
+ Excon.stub({url: "http://example.com/foo"}, status: 200)
30
+
31
+ get "/foo"
32
+ end
33
+ end
34
+
35
+ context "capture groups" do
36
+ it "transforms the path based on capture groups" do
37
+ Excon.stub({url: "http://example.com/bar"}, status: 200)
38
+
39
+ get "/foo/bar"
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require "rack/forwarder"
3
+
4
+ RSpec.configure do |config|
5
+ config.before(:all) do
6
+ Excon.defaults[:mock] = true
7
+ end
8
+
9
+ config.after(:each) do
10
+ Excon.stubs.clear
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-forwarder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tim Cooper
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack-test
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: excon
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Forward requests
98
+ email:
99
+ - coop@latrobest.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/rack/forwarder.rb
111
+ - lib/rack/forwarder/matcher.rb
112
+ - lib/rack/forwarder/registry.rb
113
+ - lib/rack/forwarder/version.rb
114
+ - rack-forwarder.gemspec
115
+ - spec/rack/forwarder_spec.rb
116
+ - spec/spec_helper.rb
117
+ homepage: ''
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.2.2
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Forward requests
141
+ test_files:
142
+ - spec/rack/forwarder_spec.rb
143
+ - spec/spec_helper.rb