rails_multisite 2.1.0 → 2.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e9d253acd86ab0b218a43d6cf67bc7bd3948b03c8288872f75c8788ff1c54373
4
- data.tar.gz: 1141a30cad5dba7992b6adc5ab1c315488789430f9bee82ba4bb0b189447d27c
3
+ metadata.gz: c423a9ad1f1506b7278fdad487f740236f6241a5cfced260e135cc4392f0f24d
4
+ data.tar.gz: 3da1e820fedb5a8ec1c7c34c41668cf62d8891bab765f554e54186688fe27464
5
5
  SHA512:
6
- metadata.gz: 6a2a3ac0846e4b589a44e5285d8bba6be418e0a535c642a95f23073dcf16d51425e87d27ed74e5e58b04e25a5cc88620d7937530bd25285dd013d8e81689fb6e
7
- data.tar.gz: 5520157f4aed32cc36b03e289ccb56b4f43385f7a153f64245a4ab570b13138826b8cf1d2aaac78b40b1d7231501575a53df4af0f1d56472ef267e82f90f6d92
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
- attr_reader :config_filename
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
- @db_spec_cache = {}
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
- # (@connection_handlers is a hash of spec => handler)
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
- host = request['__ws'] || request.host
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
- Rails.configuration.multisite = false
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
- Rails.configuration.multisite = true
15
+ app.config.multisite = true
16
16
  Rails.logger.formatter = RailsMultisite::Formatter.new
17
- app.middleware.insert_after(ActionDispatch::Executor, RailsMultisite::Middleware)
18
- app.middleware.delete(ActionDispatch::Executor)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  module RailsMultisite
4
- VERSION = "2.1.0"
4
+ VERSION = "2.4.0"
5
5
  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.1.0
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-03-01 00:00:00.000000000 Z
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: []