rack-delegate 0.1.0

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: 3c1d5ca6de8d7bac5979e2ac16460b6a801bc313
4
+ data.tar.gz: 28804039fe13751a0beaa1e18f05dede5ca5553d
5
+ SHA512:
6
+ metadata.gz: 58696d495777a91a8df345f57ee8eaf43ab238fbf812b9b004d2741b6680b5fc6b0bd3b2404c927a28ea7bf584781ab215dfb226efafff5885466df8ac8b61ff
7
+ data.tar.gz: ee8b16bc8edcdcf0d5595bbcb7c5b22f729fb45940a3b2b2d4128698547ae88b8500175dea5c229283eb42447330e853502476966f07a9116a915a9eda1d9feb
data/.but.jpg ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .tags
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-delegate.gemspec
4
+ gemspec
5
+
6
+ gem 'bundler', '~> 1.9'
7
+ gem 'rake', '~> 10.0'
8
+
9
+ gem 'minitest'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Genadi Samokovarov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # Rack Delegate
2
+
3
+ Rack level reverse proxy. Route requests to services with pure Ruby if that's
4
+ your boat. The boat won't be pretty fast, but you can deploy it on your
5
+ Heroku's, and what-not-fancy-cloud-services, without a bit of complications.
6
+
7
+ The proxy can sit after the request authentication, or before it, depending on
8
+ the services you have to route requests to. You can rewrite requests URLs, and
9
+ even the whole `Net::HTTP::Reqest` to be sent out.
10
+
11
+ ## Installation
12
+
13
+ Put this in the application `Gemfile`:
14
+
15
+ ```ruby
16
+ gem 'rack-delegate'
17
+ ```
18
+
19
+ `rack-delegate` requires Ruby 2.0 and above.
20
+
21
+ ## Usage
22
+
23
+ To use `rack-delegate`, you first need to configure a delegator proxy. You can
24
+ do that with `Rack::Delegate.configure`. Note that `Rack::Delegate.configure`,
25
+ actually creates a middleware, which we can insert in an arbitrary stack later
26
+ on.
27
+
28
+ ```ruby
29
+ Macro::ApiGateway = Rack::Delegate.configure do
30
+ # Strips the leading /api out of the outgoing requests.
31
+ rewrite { path.gsub!(%r{\A/api}, '') }
32
+
33
+ # Don't proxy requests without them matching on the condition in the block.
34
+ constraints { |request| Version.new('v1').matches?(request) }
35
+
36
+ # With the rewrite on, requests you /api/users will go to
37
+ # http://users-service.intern/users.
38
+ from %r{\A/api/users}, to: 'http://users-service.intern'
39
+
40
+ # Requests go to http://payments-service.intern/payments.
41
+ from %r{\A/api/payments}, to: 'http://payments-service.intern'
42
+ end
43
+
44
+ module Macro
45
+ class Appplication
46
+ middleware.use ApiGateway
47
+ end
48
+ end
49
+ ```
50
+
51
+ Yes, you can insert multiple `Rack::Delegate` middleware instances in your
52
+ stack. Say, one for requests that don't require authentication and one for
53
+ requests that do.
54
+
55
+ _(Given that the request authentication is a middleware itself.)_
56
+
57
+ ```ruby
58
+ Macro::UnauthenticatedGateway = Rack::Delegate.configure do
59
+ rewrite { path.gsub!(%r{\A/api}, '') }
60
+
61
+ from %r{\A/registration}, to: 'http://registration-service.intern'
62
+ end
63
+
64
+ Macro::AuthenticatedGateway = Rack::Delegate.configure do
65
+ rewrite { path.gsub!(%r{\A/api}, '') }
66
+
67
+ from %r{\A/api/users}, to: 'http://users-service.intern'
68
+ from %r{\A/api/payments}, to: 'http://payments-service.intern'
69
+ end
70
+
71
+ module Macro
72
+ class Appplication
73
+ middleware.insert_before "Auth", UnauthenticatedGateway
74
+ middleware.insert_after "Auth", AuthenticatedGateway
75
+ end
76
+ end
77
+ ```
78
+
79
+ ## Why
80
+
81
+ ![but](https://raw.githubusercontent.com/gsamokovarov/rack-delegate/master/.but.jpg)
82
+
83
+ A question well asked, dear sir! Going micro-services early may bite you. Going
84
+ micro-services for the sake of it may bite you. Going micro-services, because
85
+ you can't manage that shitty app, well... may result in shitty services as
86
+ well.
87
+
88
+ Anyway, if you wanna go that route, you can do it quickly with Ruby and
89
+ prototype. And if you do, you may as well need a better API gateway than this
90
+ one. [OpenResty] may be useful for you.
91
+
92
+ [OpenResty]: https://openresty.org/
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'rake/testtask'
2
+ require 'bundler/gem_tasks'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'lib'
6
+ t.libs << 'test'
7
+ t.pattern = 'test/**/*_test.rb'
8
+ t.verbose = false
9
+ end
10
+
11
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rack/delegate"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ require 'rack'
2
+
3
+ module Rack
4
+ module Delegate
5
+ autoload :Rewriter, 'rack/delegate/rewriter'
6
+ autoload :NetHttpRequestBuilder, 'rack/delegate/net_http_request_builder'
7
+ autoload :Delegator, 'rack/delegate/delegator'
8
+ autoload :NetworkErrorResponse, 'rack/delegate/network_error_response'
9
+ autoload :Constraint, 'rack/delegate/constraint'
10
+ autoload :Action, 'rack/delegate/action'
11
+ autoload :ConstrainedAction, 'rack/delegate/constrained_action'
12
+ autoload :Configuration, 'rack/delegate/configuration'
13
+ autoload :Dispatcher, 'rack/delegate/dispatcher'
14
+
15
+ def self.configure(&block)
16
+ dispatcher = Dispatcher.configure(&block)
17
+
18
+ Struct.new(:app) do
19
+ define_method :call do |env|
20
+ request = Request.new(env)
21
+
22
+ if action = dispatcher.dispatch(request)
23
+ action.call(env)
24
+ else
25
+ app.call(env)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,11 @@
1
+ module Rack
2
+ module Delegate
3
+ class Action < Struct.new(:pattern, :delegator)
4
+ def dispatch(request)
5
+ pattern.match(request.fullpath) do
6
+ throw :dispatched, delegator
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,57 @@
1
+ module Rack
2
+ module Delegate
3
+ class Configuration
4
+ def self.from_block(&block)
5
+ config = new
6
+ config.instance_eval(&block)
7
+ config.actions
8
+ end
9
+
10
+ attr_reader :actions
11
+
12
+ def initialize
13
+ @actions = []
14
+ @constraints = []
15
+ @rewriter = Rewriter.new
16
+ @changer = Rewriter.new
17
+ end
18
+
19
+ def from(pattern, to:, constraints: nil)
20
+ action = Action.new(pattern, Delegator.new(to, @rewriter, @changer))
21
+ action = Rack::Timeout.new(action) if timeout?
22
+
23
+ constraints = Array(constraints).concat(@constraints)
24
+ action = ConstrainedAction.new(action, constraints) unless constraints.empty?
25
+
26
+ @actions << action
27
+ end
28
+
29
+ def rewrite(&block)
30
+ @rewriter = Rewriter.new do |uri|
31
+ uri.instance_eval(&block)
32
+ uri
33
+ end
34
+ end
35
+
36
+ def change
37
+ @changer = Rewriter.new do |request|
38
+ yield request
39
+ request
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def constraints(*args, &block)
46
+ @constraints << args.flatten << block
47
+ end
48
+
49
+ def timeout?
50
+ require 'rack/timeout'
51
+ true
52
+ rescue LoadError
53
+ false
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,17 @@
1
+ module Rack
2
+ module Delegate
3
+ class ConstrainedAction < Struct.new(:action, :constraints)
4
+ def dispatch(request)
5
+ if appropriate?(request)
6
+ action.dispatch(request)
7
+ end
8
+ end
9
+
10
+ private
11
+
12
+ def appropriate?(request)
13
+ Constraint.new(constraints) === request
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ module Rack
2
+ module Delegate
3
+ class Constraint
4
+ def initialize(*constraints)
5
+ @constraints = constraints.flatten.compact
6
+ end
7
+
8
+ def ===(request)
9
+ @constraints.all? do |constraint|
10
+ invoke_polyglot_constraint(constraint, request)
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ SUPPORTED_CONSTRAINTS_INTERFACE = [:matches?, :call, :===]
17
+
18
+ def invoke_polyglot_constraint(constraint, request)
19
+ method = SUPPORTED_CONSTRAINTS_INTERFACE.find do |method|
20
+ constraint.respond_to?(method)
21
+ end
22
+ constraint.public_send(method, request)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,55 @@
1
+ require 'timeout_errors'
2
+
3
+ module Rack
4
+ module Delegate
5
+ class Delegator
6
+ class << self
7
+ attr_accessor :network_error_response
8
+ end
9
+
10
+ def initialize(url, uri_rewriter, net_http_request_rewriter)
11
+ @url = URI(url)
12
+ @uri_rewriter = uri_rewriter
13
+ @net_http_request_rewriter = net_http_request_rewriter
14
+ end
15
+
16
+ def call(env)
17
+ rack_request = Request.new(env)
18
+ net_http_request = NetHttpRequestBuilder.new(rack_request, @uri_rewriter, @net_http_request_rewriter).build
19
+
20
+ http_response = Net::HTTP.start(*net_http_options) do |http|
21
+ http.request(net_http_request)
22
+ end
23
+
24
+ convert_to_rack_response(http_response)
25
+ rescue TimeoutErrors
26
+ # TODO: Should I let the error in as an input?
27
+ network_error_response.call(env)
28
+ end
29
+
30
+ private
31
+
32
+ def net_http_options
33
+ [@url.host, @url.port, https: @url.scheme == 'https']
34
+ end
35
+
36
+ def network_error_response
37
+ self.class.network_error_response ||= NetworkErrorResponse
38
+ end
39
+
40
+ def convert_to_rack_response(http_response)
41
+ status = http_response.code
42
+ headers = normalize_headers_for(http_response)
43
+ body = Array(http_response.body)
44
+
45
+ [status, headers, body]
46
+ end
47
+
48
+ def normalize_headers_for(http_response)
49
+ http_response.to_hash.tap do |headers|
50
+ headers.delete('status')
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,19 @@
1
+ module Rack
2
+ module Delegate
3
+ class Dispatcher < Struct.new(:actions)
4
+ def self.configure(&block)
5
+ new(Configuration.from_block(&block))
6
+ end
7
+
8
+ def dispatch(request)
9
+ catch :dispatched do
10
+ actions.each do |action|
11
+ action.dispatch(request)
12
+ end
13
+
14
+ nil
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,54 @@
1
+ require 'net/http'
2
+
3
+ module Rack
4
+ module Delegate
5
+ class NetHttpRequestBuilder < Struct.new(:rack_request, :uri_rewriter, :net_http_request_rewriter)
6
+ def build
7
+ net_http_request_class.new(url).tap do |net_http_request|
8
+ delegate_rack_headers_to(net_http_request)
9
+ delegate_rack_body_to(net_http_request)
10
+
11
+ rewrite_net_http_request(net_http_request)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def http_method
18
+ rack_request.request_method.capitalize
19
+ end
20
+
21
+ def net_http_request_class
22
+ Net::HTTP.const_get(http_method)
23
+ end
24
+
25
+ def url
26
+ uri_rewriter.rewrite(URI(rack_request.url))
27
+ end
28
+
29
+ def delegate_rack_headers_to(net_http_request)
30
+ net_http_request.initialize_http_header(headers_from_rack_request(rack_request))
31
+ end
32
+
33
+ def delegate_rack_body_to(net_http_request)
34
+ return unless net_http_request.request_body_permitted?
35
+
36
+ begin
37
+ net_http_request.body = rack_request.body.read
38
+ ensure
39
+ rack_request.body.rewind
40
+ end
41
+ end
42
+
43
+ def rewrite_net_http_request(net_http_request)
44
+ net_http_request_rewriter.rewrite(net_http_request)
45
+ end
46
+
47
+ def headers_from_rack_request(rack_request)
48
+ rack_request.env
49
+ .select { |key, _| key.start_with?('HTTP_') }
50
+ .collect { |key, value| [key.sub(/^HTTP_/, '').tr('_', '-'), value] }
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,17 @@
1
+ module Rack
2
+ module Delegate
3
+ class NetworkErrorResponse < Struct.new(:env)
4
+ def self.call(env)
5
+ new(env).call
6
+ end
7
+
8
+ def call
9
+ status = 504
10
+ headers = {'Content-Type' => 'text/plain'}
11
+ body = ["Gateway Timeout\n"]
12
+
13
+ [status, headers, body]
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ module Rack
2
+ module Delegate
3
+ class Rewriter
4
+ def initialize(&rewriter)
5
+ @rewriter = rewriter || proc { |id| id }
6
+ end
7
+
8
+ def rewrite(object)
9
+ @rewriter.call(object)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module Delegate
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'rack/delegate/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-delegate"
8
+ spec.version = Rack::Delegate::VERSION
9
+ spec.authors = ["Genadi Samokovarov"]
10
+ spec.email = ["gsamokovarov@gmail.com"]
11
+
12
+ spec.summary = "Rack level reverse proxy."
13
+ spec.description = "Rack level reverse proxy."
14
+ spec.homepage = "https://github.com/gsamokovarov/rack-delegate"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "rack"
23
+ spec.add_dependency "timeout_errors"
24
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-delegate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Genadi Samokovarov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: timeout_errors
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Rack level reverse proxy.
42
+ email:
43
+ - gsamokovarov@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".but.jpg"
49
+ - ".gitignore"
50
+ - ".travis.yml"
51
+ - CODE_OF_CONDUCT.md
52
+ - Gemfile
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - bin/setup
58
+ - lib/rack/delegate.rb
59
+ - lib/rack/delegate/action.rb
60
+ - lib/rack/delegate/configuration.rb
61
+ - lib/rack/delegate/constrained_action.rb
62
+ - lib/rack/delegate/constraint.rb
63
+ - lib/rack/delegate/delegator.rb
64
+ - lib/rack/delegate/dispatcher.rb
65
+ - lib/rack/delegate/net_http_request_builder.rb
66
+ - lib/rack/delegate/network_error_response.rb
67
+ - lib/rack/delegate/rewriter.rb
68
+ - lib/rack/delegate/version.rb
69
+ - rack-delegate.gemspec
70
+ homepage: https://github.com/gsamokovarov/rack-delegate
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.5.1
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Rack level reverse proxy.
94
+ test_files: []