-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_ui.rb
More file actions
87 lines (71 loc) · 1.94 KB
/
proxy_ui.rb
File metadata and controls
87 lines (71 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require 'rubygems'
require 'sinatra'
require 'haml'
require 'sass'
require 'rest_client'
get '/' do
redirect '/home'
end
get '/home' do
haml :home, :format => :html5
end
get /\/tenant_a_and_b\/?(.*)/ do |uri|
render_index('tenant_a_and_b', ['tenant_a', 'tenant_b'], uri)
end
get /\/tenant_a\/?(.*)/ do |uri|
render_index('tenant_a', ['tenant_a'], uri)
end
get /\/tenant_b\/?(.*)/ do |uri|
render_index('tenant_b', ['tenant_b'], uri)
end
get /\/tenant_crud\/?(.*)/ do |uri|
render_index('tenant_crud', ['tenant_crud'], uri)
end
post /\/tenant_crud\/?(.*)/ do |uri|
tenant_urls = decode_url(@tenants, uri)
RestClient.post(tenant_urls['tenant_crud'], params) do |response, request, result, &block|
if [301, 302, 307].include? response.code
response.follow_redirection(request, result, &block)
else
response.return!(request, result, &block)
end
end
end
def render_index(proxy_ui_namespace, tenants, uri)
begin
@request_host = request.host
@proxy_ui_namespace = proxy_ui_namespace
@tenants = tenants
@content = {}
tenant_urls = decode_url(@tenants, uri)
@tenants.each do |tenant|
tenant_url = tenant_urls[tenant] || TENANT_HOSTS[tenant]
@content[tenant] = RestClient.get(tenant_url, params: params).to_s
end
haml :tenants, :format => :html5
rescue Errno::ECONNREFUSED, RestClient::ResourceNotFound
haml :four_oh_four
end
end
TENANT_HOSTS = {
'tenant_a' => 'http://localhost:9494',
'tenant_b' => 'http://localhost:9595',
'tenant_crud' => 'http://localhost:9696'
}
def decode_url(tenants, uri)
res = {}
tenant_paths = uri.split('--')
tenant_paths.shift
tenant_paths.each do |path|
path_parts = path.split('/')
tenant_name = path_parts.shift
res[tenant_name] = tenant_uri(tenant_name, path_parts.join('/'))
end
res
end
def tenant_uri(tenant, uri)
"#{TENANT_HOSTS[tenant]}/#{uri}"
end
def is_active(link)
request.path.match(link) ? 'active' : ''
end