rails_multisite 2.1.0 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rails_multisite might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +13 -0
- data/lib/rails_multisite/connection_management.rb +66 -13
- data/lib/rails_multisite/railtie.rb +12 -4
- data/lib/rails_multisite/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c423a9ad1f1506b7278fdad487f740236f6241a5cfced260e135cc4392f0f24d
|
4
|
+
data.tar.gz: 3da1e820fedb5a8ec1c7c34c41668cf62d8891bab765f554e54186688fe27464
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68717b4c14ccf6586de353e1e455aa948e931a498fcb5816008f5f6655e6d8c896793ef635532cc056073f917f7ea6e7205e7f5bc9b4a5a581e574cee76cd47b
|
7
|
+
data.tar.gz: 68779aedd6acb814e8401226ef67ad9636abf3ca71c954cef89226199fd754a34f78c84c5340af2bc2216f9e560d410867837a3fd3d221c53b08449ed60ceb68
|
data/README.md
CHANGED
@@ -93,6 +93,19 @@ To get a Rails console that is connected to `some_database_1` database:
|
|
93
93
|
RAILS_DB=db_one rails console
|
94
94
|
```
|
95
95
|
|
96
|
+
### CDN origin support
|
97
|
+
|
98
|
+
To avoid needing to configure many origins you can consider using `RailsMultisite::ConnectionManagement.asset_hostname`
|
99
|
+
|
100
|
+
When configured, requests to `asset_hostname`?__ws=another.host.name will be re-routed to the correct site. Cookies will
|
101
|
+
be stripped on all incoming requests.
|
102
|
+
|
103
|
+
Example:
|
104
|
+
|
105
|
+
- Multisite serves `sub.example.com` and `assets.example.com`
|
106
|
+
- `RailsMultisite::ConnectionManagement.asset_hostname = 'assets.example.com'`
|
107
|
+
- Requests to `https://assets.example.com/route/?__ws=sub.example.com` will be routed to the `sub.example.com`
|
108
|
+
|
96
109
|
|
97
110
|
## Contributing
|
98
111
|
|
@@ -11,6 +11,10 @@ module RailsMultisite
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def self.clear_settings!
|
14
|
+
@instance&.db_spec_cache&.each do |key, spec|
|
15
|
+
@instance.connection_handlers.delete(self.handler_key(spec))
|
16
|
+
end
|
17
|
+
|
14
18
|
@instance = nil
|
15
19
|
end
|
16
20
|
|
@@ -31,6 +35,14 @@ module RailsMultisite
|
|
31
35
|
end
|
32
36
|
end
|
33
37
|
|
38
|
+
def self.asset_hostname
|
39
|
+
@asset_hostname
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.asset_hostname=(h)
|
43
|
+
@asset_hostname = h
|
44
|
+
end
|
45
|
+
|
34
46
|
def self.config_filename
|
35
47
|
@instance.config_filename
|
36
48
|
end
|
@@ -98,6 +110,13 @@ module RailsMultisite
|
|
98
110
|
config[:host_names].nil? ? config[:host] : config[:host_names].first
|
99
111
|
end
|
100
112
|
|
113
|
+
def self.current_db_hostnames
|
114
|
+
spec = @instance.connection_spec(db: self.current_db) if @instance
|
115
|
+
spec ||= ActiveRecord::Base.connection_pool.spec
|
116
|
+
config = spec.config
|
117
|
+
config[:host_names].nil? ? [config[:host]] : config[:host_names]
|
118
|
+
end
|
119
|
+
|
101
120
|
def self.connection_spec(opts)
|
102
121
|
if @instance
|
103
122
|
@instance.connection_spec(opts)
|
@@ -114,17 +133,45 @@ module RailsMultisite
|
|
114
133
|
end
|
115
134
|
end
|
116
135
|
|
117
|
-
|
136
|
+
def self.handler_key(spec)
|
137
|
+
@handler_key_suffix ||= begin
|
138
|
+
if ActiveRecord::Base.respond_to?(:writing_role)
|
139
|
+
"_#{ActiveRecord::Base.writing_role}"
|
140
|
+
else
|
141
|
+
""
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
:"#{spec.name}#{@handler_key_suffix}"
|
146
|
+
end
|
147
|
+
|
148
|
+
def self.default_connection_handler=(connection_handler)
|
149
|
+
if @instance
|
150
|
+
unless connection_handler.is_a?(ActiveRecord::ConnectionAdapters::ConnectionHandler)
|
151
|
+
raise ArgumentError.new("Invalid connection handler")
|
152
|
+
end
|
153
|
+
|
154
|
+
@instance.default_connection_handler = connection_handler
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
attr_reader :config_filename, :db_spec_cache, :connection_handlers
|
159
|
+
attr_writer :default_connection_handler
|
118
160
|
|
119
161
|
def initialize(config_filename)
|
120
162
|
@config_filename = config_filename
|
121
163
|
|
122
|
-
@connection_handlers =
|
123
|
-
|
164
|
+
@connection_handlers = begin
|
165
|
+
if ActiveRecord::Base.respond_to?(:connection_handlers)
|
166
|
+
ActiveRecord::Base.connection_handlers
|
167
|
+
else
|
168
|
+
{}
|
169
|
+
end
|
170
|
+
end
|
124
171
|
|
172
|
+
@db_spec_cache = {}
|
125
173
|
@default_spec = SPEC_KLASS::Resolver.new(ActiveRecord::Base.configurations).spec(Rails.env.to_sym)
|
126
174
|
@default_connection_handler = ActiveRecord::Base.connection_handler
|
127
|
-
@established_default = false
|
128
175
|
|
129
176
|
@reload_mutex = Mutex.new
|
130
177
|
|
@@ -181,8 +228,7 @@ module RailsMultisite
|
|
181
228
|
@db_spec_cache = new_db_spec_cache
|
182
229
|
|
183
230
|
# Clean up connection handler cache.
|
184
|
-
|
185
|
-
removed_specs.each { |s| @connection_handlers.delete(s) }
|
231
|
+
removed_specs.each { |s| @connection_handlers.delete(handler_key(s)) }
|
186
232
|
end
|
187
233
|
|
188
234
|
def reload
|
@@ -210,18 +256,14 @@ module RailsMultisite
|
|
210
256
|
spec ||= @default_spec
|
211
257
|
handler = nil
|
212
258
|
if spec != @default_spec
|
213
|
-
handler = @connection_handlers[spec]
|
259
|
+
handler = @connection_handlers[handler_key(spec)]
|
214
260
|
unless handler
|
215
261
|
handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
|
216
262
|
handler_establish_connection(handler, spec)
|
217
|
-
@connection_handlers[spec] = handler
|
263
|
+
@connection_handlers[handler_key(spec)] = handler
|
218
264
|
end
|
219
265
|
else
|
220
266
|
handler = @default_connection_handler
|
221
|
-
if !@established_default
|
222
|
-
handler_establish_connection(handler, spec)
|
223
|
-
@established_default = true
|
224
|
-
end
|
225
267
|
end
|
226
268
|
|
227
269
|
ActiveRecord::Base.connection_handler = handler
|
@@ -336,7 +378,14 @@ module RailsMultisite
|
|
336
378
|
end
|
337
379
|
|
338
380
|
request = Rack::Request.new(env)
|
339
|
-
|
381
|
+
|
382
|
+
host =
|
383
|
+
if request['__ws'] && request.host == self.class.asset_hostname
|
384
|
+
request.cookies.clear
|
385
|
+
request['__ws']
|
386
|
+
else
|
387
|
+
request.host
|
388
|
+
end
|
340
389
|
|
341
390
|
env["RAILS_MULTISITE_HOST"] = host
|
342
391
|
end
|
@@ -355,5 +404,9 @@ module RailsMultisite
|
|
355
404
|
handler.establish_connection(spec.config)
|
356
405
|
end
|
357
406
|
|
407
|
+
def handler_key(spec)
|
408
|
+
self.class.handler_key(spec)
|
409
|
+
end
|
410
|
+
|
358
411
|
end
|
359
412
|
end
|
@@ -7,20 +7,28 @@ module RailsMultisite
|
|
7
7
|
end
|
8
8
|
|
9
9
|
initializer "RailsMultisite.init" do |app|
|
10
|
-
|
10
|
+
app.config.multisite = false
|
11
11
|
|
12
12
|
config_file = ConnectionManagement.default_config_filename
|
13
13
|
if File.exist?(config_file)
|
14
14
|
ConnectionManagement.config_filename = ConnectionManagement.default_config_filename
|
15
|
-
|
15
|
+
app.config.multisite = true
|
16
16
|
Rails.logger.formatter = RailsMultisite::Formatter.new
|
17
|
-
|
18
|
-
app.
|
17
|
+
|
18
|
+
if !skip_middleware?(app.config)
|
19
|
+
app.middleware.insert_after(ActionDispatch::Executor, RailsMultisite::Middleware)
|
20
|
+
app.middleware.delete(ActionDispatch::Executor)
|
21
|
+
end
|
19
22
|
|
20
23
|
if ENV['RAILS_DB'].present?
|
21
24
|
ConnectionManagement.establish_connection(db: ENV['RAILS_DB'], raise_on_missing: true)
|
22
25
|
end
|
23
26
|
end
|
24
27
|
end
|
28
|
+
|
29
|
+
def skip_middleware?(config)
|
30
|
+
return false if !config.respond_to?(:skip_multisite_middleware)
|
31
|
+
config.skip_multisite_middleware
|
32
|
+
end
|
25
33
|
end
|
26
34
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_multisite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Saffron
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -70,7 +70,7 @@ files:
|
|
70
70
|
homepage: ''
|
71
71
|
licenses: []
|
72
72
|
metadata: {}
|
73
|
-
post_install_message:
|
73
|
+
post_install_message:
|
74
74
|
rdoc_options: []
|
75
75
|
require_paths:
|
76
76
|
- lib
|
@@ -86,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
86
|
version: '0'
|
87
87
|
requirements: []
|
88
88
|
rubygems_version: 3.0.3
|
89
|
-
signing_key:
|
89
|
+
signing_key:
|
90
90
|
specification_version: 4
|
91
91
|
summary: Multi tenancy support for Rails
|
92
92
|
test_files: []
|