sitehub 0.4.10 → 0.5.0.alpha2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +18 -7
- data/lib/sitehub.rb +1 -0
- data/lib/sitehub/builder.rb +18 -32
- data/lib/sitehub/collection/route_collection.rb +0 -6
- data/lib/sitehub/core.rb +69 -0
- data/lib/sitehub/equality.rb +0 -1
- data/lib/sitehub/forward_proxy.rb +4 -17
- data/lib/sitehub/location_rewriters.rb +2 -0
- data/lib/sitehub/middleware.rb +3 -1
- data/lib/sitehub/middleware/config_loader.rb +39 -0
- data/lib/sitehub/middleware/reverse_proxy.rb +1 -1
- data/lib/sitehub/middleware/route.rb +35 -0
- data/lib/sitehub/middleware/{forward_proxies.rb → routes.rb} +21 -13
- data/lib/sitehub/{nil_proxy.rb → nil_route.rb} +1 -1
- data/lib/sitehub/{forward_proxy_builder.rb → route_builder.rb} +90 -56
- data/lib/sitehub/version.rb +1 -1
- data/sitehub.gemspec +2 -1
- data/spec/sitehub/builder_spec.rb +58 -64
- data/spec/sitehub/collection/route_collection_spec.rb +0 -13
- data/spec/sitehub/core_spec.rb +131 -0
- data/spec/sitehub/forward_proxy_spec.rb +1 -47
- data/spec/sitehub/middleware/config_loader_spec.rb +64 -0
- data/spec/sitehub/middleware/route_spec.rb +53 -0
- data/spec/sitehub/middleware/{forward_proxies_spec.rb → routes_spec.rb} +71 -42
- data/spec/sitehub/{nil_proxy_spec.rb → nil_route_spec.rb} +2 -2
- data/spec/sitehub/{forward_proxy_builder_spec.rb → route_builder_spec.rb} +88 -59
- data/spec/support/shared_contexts/middleware_context.rb +7 -1
- data/spec/support/shared_examples/sitehub_json_context.rb +46 -0
- metadata +35 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f337700fc9aa4189ae5eca245175ce90215feb89
|
4
|
+
data.tar.gz: e13f12292d4dfd071025531ffa3a1e318f62e830
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7d744f3fa8f2fa9170291eab863c4d1f6b79024b3811ec1a292409c188ace0ea62e4289575241b5af4c64d0359affaa25b9571e5c316e8ed2733552d3d587b5
|
7
|
+
data.tar.gz: f481d91e80a2e9de3681174c9ca0da813ee4e724395308ec2abbe5fd29e4220ddfa356c58b0f22a25d48424b7284c31799bb453a4929d28b576449ab7e536da7
|
data/Gemfile.lock
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
sitehub (0.
|
4
|
+
sitehub (0.5.0.alpha2)
|
5
|
+
activesupport
|
5
6
|
em-http-request
|
6
7
|
em-synchrony
|
7
8
|
faraday
|
8
|
-
rack
|
9
|
+
rack
|
9
10
|
rack-fiber_pool
|
10
11
|
rack-ssl-enforcer
|
11
12
|
thin
|
@@ -14,6 +15,11 @@ PATH
|
|
14
15
|
GEM
|
15
16
|
remote: https://rubygems.org/
|
16
17
|
specs:
|
18
|
+
activesupport (5.0.0)
|
19
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
20
|
+
i18n (~> 0.7)
|
21
|
+
minitest (~> 5.1)
|
22
|
+
tzinfo (~> 1.1)
|
17
23
|
addressable (2.4.0)
|
18
24
|
ast (2.3.0)
|
19
25
|
axiom-types (0.1.1)
|
@@ -27,10 +33,11 @@ GEM
|
|
27
33
|
simplecov (>= 0.7.1, < 1.0.0)
|
28
34
|
coercible (1.0.0)
|
29
35
|
descendants_tracker (~> 0.0.1)
|
36
|
+
concurrent-ruby (1.0.2)
|
30
37
|
cookiejar (0.3.3)
|
31
38
|
crack (0.4.3)
|
32
39
|
safe_yaml (~> 1.0.0)
|
33
|
-
daemons (1.2.
|
40
|
+
daemons (1.2.3)
|
34
41
|
descendants_tracker (0.0.4)
|
35
42
|
thread_safe (~> 0.3, >= 0.3.1)
|
36
43
|
diff-lcs (1.2.5)
|
@@ -43,10 +50,10 @@ GEM
|
|
43
50
|
http_parser.rb (>= 0.6.0)
|
44
51
|
em-socksify (0.3.1)
|
45
52
|
eventmachine (>= 1.0.0.beta.4)
|
46
|
-
em-synchrony (1.0.
|
53
|
+
em-synchrony (1.0.5)
|
47
54
|
eventmachine (>= 1.0.0.beta.1)
|
48
55
|
equalizer (0.0.11)
|
49
|
-
eventmachine (1.2.
|
56
|
+
eventmachine (1.2.0.1)
|
50
57
|
faraday (0.9.2)
|
51
58
|
multipart-post (>= 1.2, < 3)
|
52
59
|
geminabox (0.13.1)
|
@@ -58,17 +65,19 @@ GEM
|
|
58
65
|
hashdiff (0.3.0)
|
59
66
|
http_parser.rb (0.6.0)
|
60
67
|
httpclient (2.8.0)
|
68
|
+
i18n (0.7.0)
|
61
69
|
ice_nine (0.11.2)
|
62
70
|
json (1.8.3)
|
63
71
|
macaddr (1.7.1)
|
64
72
|
systemu (~> 2.6.2)
|
65
73
|
memory_profiler (0.9.6)
|
74
|
+
minitest (5.9.0)
|
66
75
|
multipart-post (2.0.0)
|
67
76
|
nesty (1.0.2)
|
68
77
|
parser (2.3.1.2)
|
69
78
|
ast (~> 2.2)
|
70
79
|
powerpack (0.1.1)
|
71
|
-
rack (1.6.
|
80
|
+
rack (1.6.4)
|
72
81
|
rack-fiber_pool (0.9.3)
|
73
82
|
rack-protection (1.5.3)
|
74
83
|
rack
|
@@ -113,13 +122,15 @@ GEM
|
|
113
122
|
rack-protection (~> 1.4)
|
114
123
|
tilt (>= 1.3, < 3)
|
115
124
|
systemu (2.6.5)
|
116
|
-
thin (1.7.
|
125
|
+
thin (1.7.0)
|
117
126
|
daemons (~> 1.0, >= 1.0.9)
|
118
127
|
eventmachine (~> 1.0, >= 1.0.4)
|
119
128
|
rack (>= 1, < 3)
|
120
129
|
thread_safe (0.3.5)
|
121
130
|
tilt (2.0.5)
|
122
131
|
timecop (0.8.1)
|
132
|
+
tzinfo (1.2.2)
|
133
|
+
thread_safe (~> 0.1)
|
123
134
|
unicode-display_width (1.0.5)
|
124
135
|
uuid (2.3.8)
|
125
136
|
macaddr (~> 1.0)
|
data/lib/sitehub.rb
CHANGED
data/lib/sitehub/builder.rb
CHANGED
@@ -1,20 +1,14 @@
|
|
1
|
-
require 'sitehub/getter_setter_methods'
|
2
|
-
require 'sitehub/forward_proxy_builder'
|
3
|
-
require 'sitehub/middleware'
|
4
1
|
require 'logger'
|
2
|
+
require 'sitehub/core'
|
3
|
+
require 'active_support/cache'
|
5
4
|
|
6
5
|
class SiteHub
|
7
|
-
class InvalidProxyDefinitionException < Exception
|
8
|
-
end
|
9
|
-
|
10
6
|
class Builder
|
11
|
-
attr_reader :sitehub, :forward_proxies, :reverse_proxies
|
12
|
-
|
13
7
|
include Middleware
|
14
8
|
extend GetterSetterMethods
|
15
9
|
|
16
|
-
|
17
|
-
|
10
|
+
attr_reader :core
|
11
|
+
getter_setters :access_logger, :error_logger, :config_server
|
18
12
|
|
19
13
|
def force_ssl(except: [])
|
20
14
|
@force_ssl = true
|
@@ -22,41 +16,33 @@ class SiteHub
|
|
22
16
|
end
|
23
17
|
|
24
18
|
def initialize(&block)
|
25
|
-
@
|
19
|
+
@core = Core.new
|
26
20
|
instance_eval(&block) if block
|
27
21
|
end
|
28
22
|
|
29
|
-
def forward_proxies
|
30
|
-
@forward_proxies ||= ForwardProxies.new(sitehub_cookie_name)
|
31
|
-
end
|
32
|
-
|
33
23
|
def build
|
34
|
-
forward_proxies.init
|
35
24
|
add_default_middleware
|
36
|
-
|
37
|
-
apply_middleware(
|
25
|
+
use ConfigLoader, config_server if config_server
|
26
|
+
apply_middleware(core.build)
|
38
27
|
end
|
39
28
|
|
40
29
|
def add_default_middleware
|
41
|
-
use Middleware::ReverseProxy, reverse_proxies
|
42
|
-
use Middleware::TransactionId
|
43
|
-
use Middleware::ErrorHandling
|
44
|
-
use Middleware::Logging::AccessLogger, access_logger || ::Logger.new(STDOUT)
|
45
|
-
use Middleware::Logging::ErrorLogger, error_logger || ::Logger.new(STDERR)
|
46
|
-
use Rack::FiberPool
|
47
30
|
use Rack::SslEnforcer, except: @ssl_exclusions if @force_ssl
|
31
|
+
use Rack::FiberPool
|
32
|
+
use Middleware::Logging::ErrorLogger, error_logger || ::Logger.new(STDERR)
|
33
|
+
use Middleware::Logging::AccessLogger, access_logger || ::Logger.new(STDOUT)
|
34
|
+
use Middleware::ErrorHandling
|
35
|
+
use Middleware::TransactionId
|
48
36
|
end
|
49
37
|
|
50
|
-
def
|
51
|
-
|
52
|
-
|
53
|
-
forward_proxies.add_proxy(url: url,
|
54
|
-
mapped_path: mapped_path,
|
55
|
-
&block)
|
38
|
+
def respond_to?(method)
|
39
|
+
super || core.respond_to?(method)
|
56
40
|
end
|
57
41
|
|
58
|
-
def
|
59
|
-
|
42
|
+
def method_missing(method, *args, &block)
|
43
|
+
core.send(method, *args, &block)
|
44
|
+
rescue NoMethodError
|
45
|
+
super
|
60
46
|
end
|
61
47
|
end
|
62
48
|
end
|
data/lib/sitehub/core.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'sitehub/route_builder'
|
2
|
+
require 'forwardable'
|
3
|
+
|
4
|
+
class SiteHub
|
5
|
+
class InvalidProxyDefinitionException < Exception
|
6
|
+
end
|
7
|
+
|
8
|
+
class ConfigError < Exception
|
9
|
+
end
|
10
|
+
|
11
|
+
module CollectionMethods
|
12
|
+
def collection(hash, item)
|
13
|
+
hash[item] || []
|
14
|
+
end
|
15
|
+
|
16
|
+
def collection!(hash, item)
|
17
|
+
return hash[item] if hash[item]
|
18
|
+
raise ConfigError, "missing: #{item}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Core
|
23
|
+
class << self
|
24
|
+
# TODO: default action for missing key, throw exception?
|
25
|
+
def from_hash(config)
|
26
|
+
new do
|
27
|
+
extend CollectionMethods
|
28
|
+
sitehub_cookie_name config[:sitehub_cookie_name] if config[:sitehub_cookie_name]
|
29
|
+
|
30
|
+
collection!(config, :proxies).each do |proxy|
|
31
|
+
routes.add_route route_builder: RouteBuilder.from_hash(proxy, sitehub_cookie_name)
|
32
|
+
end
|
33
|
+
|
34
|
+
collection(config, :reverse_proxies).each do |proxy|
|
35
|
+
reverse_proxy proxy[:downstream_url] => proxy[:path]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
include Equality
|
42
|
+
extend Forwardable
|
43
|
+
|
44
|
+
attr_reader :routes, :reverse_proxies
|
45
|
+
def_delegator :routes, :sitehub_cookie_name
|
46
|
+
|
47
|
+
def initialize(&block)
|
48
|
+
@reverse_proxies = {}
|
49
|
+
@routes = Middleware::Routes.new
|
50
|
+
instance_eval(&block) if block
|
51
|
+
end
|
52
|
+
|
53
|
+
def build
|
54
|
+
Middleware::ReverseProxy.new(routes.init, reverse_proxies)
|
55
|
+
end
|
56
|
+
|
57
|
+
def proxy(opts = {}, &block)
|
58
|
+
mapped_path, url = *(opts.respond_to?(:to_a) ? opts.to_a : [opts]).flatten
|
59
|
+
|
60
|
+
routes.add_route(url: url,
|
61
|
+
mapped_path: mapped_path,
|
62
|
+
&block)
|
63
|
+
end
|
64
|
+
|
65
|
+
def reverse_proxy(hash)
|
66
|
+
reverse_proxies.merge!(hash)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/sitehub/equality.rb
CHANGED
@@ -14,7 +14,6 @@ class SiteHub
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def ==(other)
|
17
|
-
return false unless other.is_a?(_clazz)
|
18
17
|
fields = instance_variables.find_all { |field| !_clazz.transient_fields.include?(field) }
|
19
18
|
fields.all? do |variable|
|
20
19
|
instance_variable_get(variable) == other.instance_variable_get(variable)
|
@@ -1,18 +1,13 @@
|
|
1
|
-
# rubocop:disable Metrics/ParameterLists
|
2
1
|
class SiteHub
|
3
2
|
class ForwardProxy
|
4
|
-
include
|
3
|
+
include Equality
|
5
4
|
|
6
|
-
attr_reader :downstream_client, :
|
5
|
+
attr_reader :downstream_client, :mapped_path, :mapped_url
|
7
6
|
|
8
7
|
transient :downstream_client
|
9
8
|
|
10
|
-
def initialize(
|
9
|
+
def initialize(mapped_path:, mapped_url:)
|
11
10
|
@downstream_client = DownstreamClient.new
|
12
|
-
@sitehub_cookie_path = sitehub_cookie_path
|
13
|
-
@sitehub_cookie_name = sitehub_cookie_name
|
14
|
-
@id = id
|
15
|
-
@rule = rule
|
16
11
|
@mapped_path = mapped_path
|
17
12
|
@mapped_url = mapped_url
|
18
13
|
end
|
@@ -21,15 +16,7 @@ class SiteHub
|
|
21
16
|
request = env[REQUEST]
|
22
17
|
request.map(mapped_path, mapped_url)
|
23
18
|
|
24
|
-
downstream_client.call(request)
|
25
|
-
response.set_cookie(sitehub_cookie_name,
|
26
|
-
path: resolve_sitehub_cookie_path(request),
|
27
|
-
value: id)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def resolve_sitehub_cookie_path(request)
|
32
|
-
sitehub_cookie_path || request.path
|
19
|
+
downstream_client.call(request)
|
33
20
|
end
|
34
21
|
end
|
35
22
|
end
|
data/lib/sitehub/middleware.rb
CHANGED
@@ -2,9 +2,11 @@ $LOAD_PATH.unshift(__dir__)
|
|
2
2
|
require 'middleware/logging'
|
3
3
|
require 'middleware/transaction_id'
|
4
4
|
require 'middleware/error_handling'
|
5
|
-
require 'middleware/
|
5
|
+
require 'middleware/routes'
|
6
6
|
require 'middleware/reverse_proxy'
|
7
|
+
require 'middleware/config_loader'
|
7
8
|
require 'rack/ssl-enforcer'
|
9
|
+
require 'middleware/route'
|
8
10
|
require 'rack/fiber_pool'
|
9
11
|
|
10
12
|
class SiteHub
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class SiteHub
|
2
|
+
class ConfigServer
|
3
|
+
attr_reader :server_url, :http_client
|
4
|
+
def initialize(url)
|
5
|
+
@server_url = url
|
6
|
+
@http_client = Faraday.new(ssl: { verify: false }) do |con|
|
7
|
+
con.adapter :em_synchrony
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def get
|
12
|
+
JSON(http_client.get(server_url).body, symbolize_names: true)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module Middleware
|
17
|
+
class ConfigLoader
|
18
|
+
attr_reader :config_server, :app, :cache
|
19
|
+
|
20
|
+
def initialize(_app, config_server_url)
|
21
|
+
@config_server = ConfigServer.new(config_server_url)
|
22
|
+
@cache = ActiveSupport::Cache::MemoryStore.new(size: 1.megabytes)
|
23
|
+
end
|
24
|
+
|
25
|
+
def call(env)
|
26
|
+
load_config
|
27
|
+
@app.call env
|
28
|
+
end
|
29
|
+
|
30
|
+
def load_config
|
31
|
+
config = cache.fetch(:sitehub_config, expires_in: 30) do
|
32
|
+
config_server.get
|
33
|
+
end
|
34
|
+
|
35
|
+
@app = Core.from_hash(config).build
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'sitehub/middleware'
|
2
|
+
require 'sitehub/rules'
|
3
|
+
|
4
|
+
class SiteHub
|
5
|
+
class Route
|
6
|
+
include Middleware, Resolver, Rules, Equality
|
7
|
+
|
8
|
+
attr_reader :id, :sitehub_cookie_name, :sitehub_cookie_path, :app
|
9
|
+
|
10
|
+
def initialize(app, id:, sitehub_cookie_name:, sitehub_cookie_path: nil)
|
11
|
+
@app = app
|
12
|
+
@id = id
|
13
|
+
@sitehub_cookie_name = sitehub_cookie_name
|
14
|
+
@sitehub_cookie_path = sitehub_cookie_path
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(env)
|
18
|
+
request = env[REQUEST]
|
19
|
+
@app.call(env).tap do |response|
|
20
|
+
response.set_cookie(sitehub_cookie_name,
|
21
|
+
path: resolve_sitehub_cookie_path(request),
|
22
|
+
value: id)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def resolve_sitehub_cookie_path(request)
|
27
|
+
sitehub_cookie_path || request.path
|
28
|
+
end
|
29
|
+
|
30
|
+
def init
|
31
|
+
@app = apply_middleware(@app)
|
32
|
+
self
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -1,21 +1,23 @@
|
|
1
|
+
require 'sitehub/getter_setter_methods'
|
1
2
|
require 'sitehub/constants'
|
2
|
-
require 'sitehub/
|
3
|
+
require 'sitehub/nil_route'
|
3
4
|
require 'rack/request'
|
4
5
|
require 'rack/response'
|
5
6
|
require 'rack/utils'
|
6
7
|
require 'em-http'
|
7
|
-
require 'forwardable'
|
8
8
|
|
9
9
|
class SiteHub
|
10
10
|
module Middleware
|
11
|
-
class
|
12
|
-
|
11
|
+
class Routes < Hash
|
12
|
+
NIL_ROUTE = NilRoute.new
|
13
13
|
|
14
|
-
|
14
|
+
include Equality
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
extend GetterSetterMethods
|
17
|
+
getter_setter :sitehub_cookie_name, RECORDED_ROUTES_COOKIE
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
self.default = NIL_ROUTE
|
19
21
|
end
|
20
22
|
|
21
23
|
def call(env)
|
@@ -31,11 +33,17 @@ class SiteHub
|
|
31
33
|
self
|
32
34
|
end
|
33
35
|
|
34
|
-
def
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
36
|
+
def add_route(url: nil, mapped_path: nil, route_builder: nil, &block)
|
37
|
+
if route_builder
|
38
|
+
self[route_builder.mapped_path] = route_builder
|
39
|
+
return
|
40
|
+
end
|
41
|
+
|
42
|
+
self[mapped_path] = RouteBuilder.new(sitehub_cookie_name: sitehub_cookie_name,
|
43
|
+
mapped_path: mapped_path,
|
44
|
+
&block).tap do |builder|
|
45
|
+
builder.default(url: url) if url
|
46
|
+
end
|
39
47
|
end
|
40
48
|
|
41
49
|
def mapped_proxy(path:, request:)
|