roda-proxy 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1ae4d0a181837cb04d8af444ad3a7876bc1182c83a943bd615487da7e72c011f
4
+ data.tar.gz: 1cd548656ada75429ccd2bb0d918a33d1854f008b1247bcd7fe24028cf20e815
5
+ SHA512:
6
+ metadata.gz: 29cc8cef4d73ce1a109aee475a25da1adfe4d34eb027ba3928d70f84beb6369460adc602666872b1a2ad34cd1b61ad038cd46f34246e2e4b0a68d48fcc2c88a5
7
+ data.tar.gz: 5009c2e0962d5c2a36b8301bacad8f75d3950855d34a79dd6df636f4f30293ed31d9952f7200ed9ed1bacd3da3bd3bde5466e292a5d57dabba25987e473077e2
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,19 @@
1
+ Layout/SpaceInsideParens:
2
+ Enabled: false
3
+
4
+ Metrics/LineLength:
5
+ Max: 120
6
+
7
+ Metrics/ModuleLength:
8
+ Exclude:
9
+ - "**/*_spec.rb"
10
+
11
+ Metrics/BlockLength:
12
+ Exclude:
13
+ - "**/*_spec.rb"
14
+
15
+ Layout/TrailingWhitespace:
16
+ Enabled: false
17
+
18
+ Style/Semicolon:
19
+ Enabled: false
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in roda-proxy.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
9
+ gem 'rspec', '~> 3.0'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Nigel Brookes-Thomas
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.
@@ -0,0 +1,91 @@
1
+ # Roda::Proxy
2
+
3
+ Roda Proxy is a very simple reverse proxy for Roda. It is designed to proxy APIs, it will not rewrite HTML.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'roda-proxy'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install roda-proxy
20
+
21
+ ## Usage
22
+
23
+ Add the plugin to your Roda app and pass it two parameters (one is required, one is optional).
24
+
25
+ ```ruby
26
+ require 'roda'
27
+ require 'roda/proxy'
28
+
29
+ class App < Roda
30
+ plugin :proxy,
31
+ to: 'https://my.server.com',
32
+ path_prefix: '/my/api/path'
33
+ ```
34
+
35
+ The `to:` parameter is required. It should describe the scheme, host and port of the proxy. It should **not** end with a `/`.
36
+
37
+ The `path_prefix:` parameter is optional. It defaults to `/`. If you chose to specify it, it **should** start and end with a `/`.
38
+
39
+ The plugin provides both an unconditional and a conditional proxy directive.
40
+
41
+ To invoke the proxy in your routes, see this example:
42
+
43
+ ```ruby
44
+ route do |r|
45
+ # /my
46
+ r.on 'my' do
47
+ # /my/api
48
+ r.on 'api' do
49
+ # /my/api/path
50
+ r.is 'path' do
51
+ # GET /my/api/path
52
+ r.get do
53
+ r.proxy
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ ```
60
+
61
+ The proxy will always be invoked. Headers and body are passed through unmodified in both directions with the exception of `Host` which is rewritten to match the target.
62
+
63
+ Also provided is a conditional proxy:
64
+
65
+ ```ruby
66
+ route do |r|
67
+ # /my
68
+ r.on 'my' do
69
+ # /my/api
70
+ r.on 'api' do
71
+ # /my/api/path
72
+ r.is 'path' do
73
+ # GET /my/api/path
74
+ r.get do
75
+ r.proxy_when(r.env['HTTP_PROXY'] == 'true', probability: 0.5) do
76
+ 'This request has not been proxied'
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ ```
84
+
85
+ With `proxy_when` the first optional parameter expects a truthy value or a block / lambda that returns a truthy value. This must be equivalent to `true` for the proxying to occur. The optional probability is a float between 0 and 1 indicating the probability that proxying will happen. Both paramters can be used alone or in isolation.
86
+
87
+ If and only if proxying does not occur will the block be evaluated and return to Roda for rendering.
88
+
89
+ ## License
90
+
91
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'roda/proxy'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require './addressing'
4
+
5
+ run App
Binary file
Binary file
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'roda/proxy/version'
5
+
6
+ # :nodoc:
7
+ class Roda
8
+ # :nodoc:
9
+ module RodaPlugins
10
+ # Roda plugin for simple API proxying
11
+ module Proxy
12
+
13
+ # Respond to the configure method to set the destination when proxying
14
+ # Expects the following options:
15
+ # [to] Required. The scheme and host of the proxy. Should not end with a slash.
16
+ # [path_prefix] Optional. The path to append to the above for proxying.
17
+ # The current request path will be prefixed on to this value.
18
+ # Should begin and end with a +/+. Defaults to +/+.
19
+ # For example, if the path prefix is +/foo/+ and the request received
20
+ # by Roda is +GET /postcode/lookup+, The proxied request will be dispatched
21
+ # to +GET /home/postcode/lookup+
22
+ # Example:
23
+ # plugin :proxy, to: 'https://foo.bar', path: '/my/api'
24
+ def self.configure(app, opts = {})
25
+ app.opts[:proxy_to] = opts.fetch(:to, nil)
26
+ app.opts[:proxy_path] = opts.fetch(:path_prefix, '/')
27
+
28
+ raise 'Proxy host not set, use "plugin :proxy, to: http://example.com"' unless app.opts[:proxy_to]
29
+ end
30
+
31
+ # :nodoc:
32
+ module RequestMethods
33
+
34
+ # Proxies the request, forwarding all headers except +Host+ which is
35
+ # rewritten to be the destination host. The response headers, body and
36
+ # status are returned to the client.
37
+ def proxy
38
+ method = Faraday.method(env['REQUEST_METHOD'].downcase.to_sym)
39
+ f_response = method.call(_proxy_url) { |req| _proxy_request(req) }
40
+ _respond(f_response)
41
+ end
42
+
43
+ # Conditionally proxies when +condition+ is true and with selective probability.
44
+ # For instance, to proxy 50% of the time:
45
+ # r.proxy_when(probability: 0.5)
46
+ # Condition can be a truthy value or a block / lambda, in which case
47
+ # the result from the +#call+ is expected to be truthy.
48
+ # r.proxy_when( r.env['HTTP_PROXY_ME'] == 'true' )
49
+ # The two parameters can be combined, the probability is evaluated first.
50
+ # r.proxy_when( r.env['HTTP_PROXY_ME'] == 'true', probability: 0.8 )
51
+ # If and only if this method choses not to proxy is the block evaluated.
52
+ # The block is then expected to return a meaningful response to Roda.
53
+ def proxy_when(condition = true, probability: 1.0)
54
+ shall_proxy = Random.rand(0.0..1.0) <= probability
55
+
56
+ if shall_proxy && ( condition.respond_to?(:call) ? condition.call : condition )
57
+ yield(self)
58
+ else
59
+ proxy
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+
66
+ def _proxy_url
67
+ @_proxy_url ||= URI(roda_class.opts[:proxy_to])
68
+ .then { |uri| uri.path = roda_class.opts[:proxy_path]; uri }
69
+ .then { |uri| uri.query = env['QUERY_STRING']; uri }
70
+ end
71
+
72
+ def _proxy_headers
73
+ env
74
+ .select { |k, _v| k.start_with? 'HTTP_' }
75
+ .reject { |k, _v| k == 'HTTP_HOST' }
76
+ .transform_keys do |k|
77
+ k.sub(/^HTTP_/, '')
78
+ .split('_')
79
+ .map(&:capitalize)
80
+ .join('-')
81
+ end.merge({ 'Host' => "#{_proxy_url.host}:#{_proxy_url.port}" })
82
+ end
83
+
84
+ def _proxy_request(req)
85
+ req.headers = _proxy_headers
86
+ end
87
+
88
+ def _respond(proxied_response)
89
+ response.status = proxied_response.status
90
+ proxied_response.headers.each { |k, v| response[k] = v }
91
+ response.write(proxied_response.body)
92
+ end
93
+ end
94
+ end
95
+
96
+ register_plugin :proxy, Proxy
97
+ end
98
+ end
Binary file
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Roda
4
+ module Proxy
5
+ VERSION = '1.0.0'
6
+ end
7
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/roda/proxy/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'roda-proxy'
7
+ spec.version = Roda::Proxy::VERSION
8
+ spec.authors = ['Nigel Brookes-Thomas']
9
+ spec.email = ['nigel@brookes-thomas.co.uk']
10
+
11
+ spec.summary = 'Reverse proxy plugin for Roda'
12
+ spec.description = 'A very simple reverse proxy for Roda'
13
+ spec.homepage = 'https://github.com/BillyRuffian/roda-proxy'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
16
+
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://github.com/BillyRuffian/roda-proxy'
21
+ spec.metadata['changelog_uri'] = 'https://github.com/BillyRuffian/roda-proxy'
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_dependency 'faraday', '~> 1.0'
33
+ spec.add_dependency 'roda', '~> 3.0'
34
+
35
+ spec.add_development_dependency 'rerun', '~> 0.13'
36
+ spec.add_development_dependency 'rubocop', '~> 0.80'
37
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: roda-proxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nigel Brookes-Thomas
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: roda
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rerun
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.80'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.80'
69
+ description: A very simple reverse proxy for Roda
70
+ email:
71
+ - nigel@brookes-thomas.co.uk
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - config.ru
87
+ - lib/.DS_Store
88
+ - lib/roda/.DS_Store
89
+ - lib/roda/proxy.rb
90
+ - lib/roda/proxy/.DS_Store
91
+ - lib/roda/proxy/version.rb
92
+ - roda-proxy.gemspec
93
+ homepage: https://github.com/BillyRuffian/roda-proxy
94
+ licenses:
95
+ - MIT
96
+ metadata:
97
+ allowed_push_host: https://rubygems.org
98
+ homepage_uri: https://github.com/BillyRuffian/roda-proxy
99
+ source_code_uri: https://github.com/BillyRuffian/roda-proxy
100
+ changelog_uri: https://github.com/BillyRuffian/roda-proxy
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: 2.3.0
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubygems_version: 3.1.2
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Reverse proxy plugin for Roda
120
+ test_files: []