rack-delegate 0.2.0 → 0.3.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.
- checksums.yaml +4 -4
- data/README.md +31 -14
- data/lib/rack/delegate/net_http_request_builder.rb +6 -1
- data/lib/rack/delegate/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 856faf0870ee7daa0433a674f87dfc8380947c53
|
4
|
+
data.tar.gz: 094fb2e6ff4307150a05f097a4c3326f26a240b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5cc3a14a4943c56ca31ca1cfe407daf203983da252b7502d8125bff3b84694add043caa452b13a17386551ac93275b1d81a167589e431dfb5af299338a5cab01
|
7
|
+
data.tar.gz: ee3c816bd192adfab66012420a8bfe8932066f6e627da59fd1e276ffc56b78b99a06d3e4735419e9dd61bcf6f8cbe951f75469eadab31e0667e9863b71562bbc
|
data/README.md
CHANGED
@@ -6,7 +6,7 @@ Heroku's, and what-not-fancy-cloud-services, without a bit of complications.
|
|
6
6
|
|
7
7
|
The proxy can sit after the request authentication, or before it, depending on
|
8
8
|
the services you have to route requests to. You can rewrite requests URLs, and
|
9
|
-
|
9
|
+
the whole `Net::HTTPRequest` to be sent out.
|
10
10
|
|
11
11
|
## Installation
|
12
12
|
|
@@ -48,6 +48,31 @@ module Macro
|
|
48
48
|
end
|
49
49
|
```
|
50
50
|
|
51
|
+
---
|
52
|
+
|
53
|
+
Wait, what happened? `Rack::Delegate.configure` created a class, we can use as
|
54
|
+
an middleware. The configuration is based on a DSL, you can check it out
|
55
|
+
[here][DSL].
|
56
|
+
|
57
|
+
In the example above, we said:
|
58
|
+
|
59
|
+
* Rewrite the incoming requests URL and strip the leading `/api` out of them,
|
60
|
+
before sending them off to the service that will handle them. The block of
|
61
|
+
`rewrite` is an `instance_eval` of an `URI` object. You can call all the
|
62
|
+
methods on `URI`.
|
63
|
+
|
64
|
+
* Don't proxy requests which don't match a constraint. A constraint is any
|
65
|
+
object that responds to `matches?` (you can reuse your Rails constraints, for
|
66
|
+
example), `call`, or `===`. The method is called with a plain `Rack::Request`
|
67
|
+
as an input. If you pass it a block, that block becomes a constraint.
|
68
|
+
|
69
|
+
* Proxy requests matching a path of `/api/users` to
|
70
|
+
`http://users-service.intern`. Because we have setup a rewrite rule, we
|
71
|
+
will hit `http://users-service.intern/users` and not
|
72
|
+
`http://users-service.intern/api/users`
|
73
|
+
|
74
|
+
---
|
75
|
+
|
51
76
|
Yes, you can insert multiple `Rack::Delegate` middleware instances in your
|
52
77
|
stack. Say, one for requests that don't require authentication and one for
|
53
78
|
requests that do.
|
@@ -56,14 +81,10 @@ _(Given that the request authentication is a middleware itself.)_
|
|
56
81
|
|
57
82
|
```ruby
|
58
83
|
Macro::UnauthenticatedGateway = Rack::Delegate.configure do
|
59
|
-
rewrite { path.gsub!(%r{\A/api}, '') }
|
60
|
-
|
61
84
|
from %r{\A/registration}, to: 'http://registration-service.intern'
|
62
85
|
end
|
63
86
|
|
64
87
|
Macro::AuthenticatedGateway = Rack::Delegate.configure do
|
65
|
-
rewrite { path.gsub!(%r{\A/api}, '') }
|
66
|
-
|
67
88
|
from %r{\A/api/users}, to: 'http://users-service.intern'
|
68
89
|
from %r{\A/api/payments}, to: 'http://payments-service.intern'
|
69
90
|
end
|
@@ -71,7 +92,7 @@ end
|
|
71
92
|
module Macro
|
72
93
|
class Appplication
|
73
94
|
middleware.insert_before "Auth", UnauthenticatedGateway
|
74
|
-
middleware.insert_after
|
95
|
+
middleware.insert_after "Auth", AuthenticatedGateway
|
75
96
|
end
|
76
97
|
end
|
77
98
|
```
|
@@ -80,13 +101,9 @@ end
|
|
80
101
|
|
81
102
|

|
82
103
|
|
83
|
-
|
84
|
-
|
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.
|
104
|
+
If you wanna go the (micro) services route, you can do it quickly with Ruby and
|
105
|
+
prototype. If you need the speed, you can check out [OpenResty].
|
91
106
|
|
92
107
|
[OpenResty]: https://openresty.org/
|
108
|
+
[DSL]: https://github.com/gsamokovarov/rack-delegate/blob/v0.2.0/lib/rack/delegate/configuration.rb
|
109
|
+
[URI]: http://ruby-doc.org/stdlib-2.3.0/libdoc/uri/rdoc/URI.html
|
@@ -3,6 +3,11 @@ require 'net/http'
|
|
3
3
|
module Rack
|
4
4
|
module Delegate
|
5
5
|
class NetHttpRequestBuilder < Struct.new(:rack_request, :uri_rewriter, :net_http_request_rewriter)
|
6
|
+
CONTENT_HEADERS = %w(
|
7
|
+
CONTENT_LENGTH
|
8
|
+
CONTENT_TYPE
|
9
|
+
).freeze
|
10
|
+
|
6
11
|
def build
|
7
12
|
net_http_request_class.new(url).tap do |net_http_request|
|
8
13
|
delegate_rack_headers_to(net_http_request)
|
@@ -46,7 +51,7 @@ module Rack
|
|
46
51
|
|
47
52
|
def headers_from_rack_request(rack_request)
|
48
53
|
rack_request.env
|
49
|
-
.select { |key, _| key.start_with?('HTTP_') }
|
54
|
+
.select { |key, _| key.start_with?('HTTP_') || CONTENT_HEADERS.include?(key) }
|
50
55
|
.collect { |key, value| [key.sub(/^HTTP_/, '').tr('_', '-'), value] }
|
51
56
|
end
|
52
57
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-delegate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Genadi Samokovarov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-03-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|