rails_multisite 2.3.0 → 3.1.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 +13 -0
- data/lib/rails_multisite/connection_management/rails_60_compat.rb +29 -0
- data/lib/rails_multisite/connection_management/rails_61_compat.rb +37 -0
- data/lib/rails_multisite/connection_management.rb +31 -26
- data/lib/rails_multisite/railtie.rb +7 -2
- data/lib/rails_multisite/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 99ce217ca2700339f5e12dbe053a99c982e371984d7b52341939c4af8c8dd8f2
|
|
4
|
+
data.tar.gz: d653055f22c50b36201509cb9112a2dde807290bce92e8ccf7d4d53fcfe56628
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 401d946022e824da7e263da2d8717af89d944bbf7c35ce4e7eab6e64643fdb0a402df73586d2df0e16a3bcb2e4a56f906a7e55f29e5ff496db25df108cc7bda9
|
|
7
|
+
data.tar.gz: e073dbde5eb080f4d78db98c4842cbeafc22c58c4df6e41933321553327e0bdeffad0cd08d3c09d1f35193550b1d38a5814ee5e70125f28a5bbe98cb6839135c
|
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_hostnames`
|
|
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_hostnames = ['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
|
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RailsMultisite
|
|
4
|
+
class ConnectionManagement
|
|
5
|
+
class ConnectionSpecification
|
|
6
|
+
class << self
|
|
7
|
+
def current
|
|
8
|
+
ActiveRecord::Base.connection_pool.spec
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def db_spec_cache(configs)
|
|
12
|
+
resolve_configs = configs
|
|
13
|
+
# rails 6 needs to use a proper object for the resolver
|
|
14
|
+
if defined?(ActiveRecord::DatabaseConfigurations)
|
|
15
|
+
resolve_configs = ActiveRecord::DatabaseConfigurations.new(configs)
|
|
16
|
+
end
|
|
17
|
+
resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(resolve_configs)
|
|
18
|
+
configs.map { |k, _| [k, resolver.spec(k.to_sym)] }.to_h
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def default
|
|
22
|
+
ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver
|
|
23
|
+
.new(ActiveRecord::Base.configurations)
|
|
24
|
+
.spec(Rails.env.to_sym)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RailsMultisite
|
|
4
|
+
class ConnectionManagement
|
|
5
|
+
class ConnectionSpecification
|
|
6
|
+
class << self
|
|
7
|
+
def current
|
|
8
|
+
new(ActiveRecord::Base.connection_pool.db_config)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def db_spec_cache(configs)
|
|
12
|
+
resolve_configs = ActiveRecord::DatabaseConfigurations.new(configs)
|
|
13
|
+
configs.map { |k, _| [k, new(resolve_configs.resolve(k.to_sym))] }.to_h
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def default
|
|
17
|
+
new(ActiveRecord::Base.configurations.resolve(Rails.env.to_sym))
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
attr_reader :spec
|
|
22
|
+
|
|
23
|
+
def initialize(spec)
|
|
24
|
+
@spec = spec
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def name
|
|
28
|
+
spec.env_name
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def to_hash
|
|
32
|
+
spec.configuration_hash
|
|
33
|
+
end
|
|
34
|
+
alias config to_hash
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
if Rails.version >= '6.1'
|
|
4
|
+
require 'rails_multisite/connection_management/rails_61_compat'
|
|
5
|
+
else
|
|
6
|
+
require 'rails_multisite/connection_management/rails_60_compat'
|
|
7
|
+
end
|
|
8
|
+
|
|
3
9
|
module RailsMultisite
|
|
4
10
|
class ConnectionManagement
|
|
5
|
-
|
|
6
11
|
DEFAULT = 'default'
|
|
7
|
-
SPEC_KLASS = ActiveRecord::ConnectionAdapters::ConnectionSpecification
|
|
8
12
|
|
|
9
13
|
def self.default_config_filename
|
|
10
14
|
File.absolute_path(Rails.root.to_s + "/config/multisite.yml")
|
|
@@ -35,6 +39,14 @@ module RailsMultisite
|
|
|
35
39
|
end
|
|
36
40
|
end
|
|
37
41
|
|
|
42
|
+
def self.asset_hostnames
|
|
43
|
+
@asset_hostnames
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def self.asset_hostnames=(h)
|
|
47
|
+
@asset_hostnames = h
|
|
48
|
+
end
|
|
49
|
+
|
|
38
50
|
def self.config_filename
|
|
39
51
|
@instance.config_filename
|
|
40
52
|
end
|
|
@@ -96,16 +108,11 @@ module RailsMultisite
|
|
|
96
108
|
end
|
|
97
109
|
|
|
98
110
|
def self.current_hostname
|
|
99
|
-
|
|
100
|
-
spec ||= ActiveRecord::Base.connection_pool.spec
|
|
101
|
-
config = spec.config
|
|
102
|
-
config[:host_names].nil? ? config[:host] : config[:host_names].first
|
|
111
|
+
current_db_hostnames.first
|
|
103
112
|
end
|
|
104
113
|
|
|
105
114
|
def self.current_db_hostnames
|
|
106
|
-
|
|
107
|
-
spec ||= ActiveRecord::Base.connection_pool.spec
|
|
108
|
-
config = spec.config
|
|
115
|
+
config = (@instance&.connection_spec(db: current_db) || ConnectionSpecification.current).config
|
|
109
116
|
config[:host_names].nil? ? [config[:host]] : config[:host_names]
|
|
110
117
|
end
|
|
111
118
|
|
|
@@ -113,7 +120,7 @@ module RailsMultisite
|
|
|
113
120
|
if @instance
|
|
114
121
|
@instance.connection_spec(opts)
|
|
115
122
|
else
|
|
116
|
-
|
|
123
|
+
ConnectionSpecification.current
|
|
117
124
|
end
|
|
118
125
|
end
|
|
119
126
|
|
|
@@ -162,7 +169,7 @@ module RailsMultisite
|
|
|
162
169
|
end
|
|
163
170
|
|
|
164
171
|
@db_spec_cache = {}
|
|
165
|
-
@default_spec =
|
|
172
|
+
@default_spec = ConnectionSpecification.default
|
|
166
173
|
@default_connection_handler = ActiveRecord::Base.connection_handler
|
|
167
174
|
|
|
168
175
|
@reload_mutex = Mutex.new
|
|
@@ -173,7 +180,7 @@ module RailsMultisite
|
|
|
173
180
|
def load_config!
|
|
174
181
|
configs = YAML::load(File.open(@config_filename))
|
|
175
182
|
|
|
176
|
-
no_prepared_statements =
|
|
183
|
+
no_prepared_statements = @default_spec.config[:prepared_statements] == false
|
|
177
184
|
|
|
178
185
|
configs.each do |k, v|
|
|
179
186
|
raise ArgumentError.new("Please do not name any db default!") if k == DEFAULT
|
|
@@ -181,17 +188,8 @@ module RailsMultisite
|
|
|
181
188
|
v[:prepared_statements] = false if no_prepared_statements
|
|
182
189
|
end
|
|
183
190
|
|
|
184
|
-
resolve_configs = configs
|
|
185
|
-
|
|
186
|
-
# rails 6 needs to use a proper object for the resolver
|
|
187
|
-
if defined?(ActiveRecord::DatabaseConfigurations)
|
|
188
|
-
resolve_configs = ActiveRecord::DatabaseConfigurations.new(configs)
|
|
189
|
-
end
|
|
190
|
-
|
|
191
|
-
resolver = SPEC_KLASS::Resolver.new(resolve_configs)
|
|
192
|
-
|
|
193
191
|
# Build a hash of db name => spec
|
|
194
|
-
new_db_spec_cache =
|
|
192
|
+
new_db_spec_cache = ConnectionSpecification.db_spec_cache(configs)
|
|
195
193
|
new_db_spec_cache.each do |k, v|
|
|
196
194
|
# If spec already existed, use the old version
|
|
197
195
|
if v&.to_hash == @db_spec_cache[k]&.to_hash
|
|
@@ -209,7 +207,7 @@ module RailsMultisite
|
|
|
209
207
|
end
|
|
210
208
|
|
|
211
209
|
# Add the default hostnames as well
|
|
212
|
-
|
|
210
|
+
@default_spec.config[:host_names].each do |host|
|
|
213
211
|
new_host_spec_cache[host] = @default_spec
|
|
214
212
|
end
|
|
215
213
|
|
|
@@ -357,7 +355,7 @@ module RailsMultisite
|
|
|
357
355
|
end
|
|
358
356
|
|
|
359
357
|
def current_db
|
|
360
|
-
|
|
358
|
+
ConnectionSpecification.current.config[:db_key] || DEFAULT
|
|
361
359
|
end
|
|
362
360
|
|
|
363
361
|
def current_hostname
|
|
@@ -370,7 +368,14 @@ module RailsMultisite
|
|
|
370
368
|
end
|
|
371
369
|
|
|
372
370
|
request = Rack::Request.new(env)
|
|
373
|
-
|
|
371
|
+
|
|
372
|
+
host =
|
|
373
|
+
if request['__ws'] && self.class.asset_hostnames&.include?(request.host)
|
|
374
|
+
request.cookies.clear
|
|
375
|
+
request['__ws']
|
|
376
|
+
else
|
|
377
|
+
request.host
|
|
378
|
+
end
|
|
374
379
|
|
|
375
380
|
env["RAILS_MULTISITE_HOST"] = host
|
|
376
381
|
end
|
|
@@ -9,9 +9,14 @@ module RailsMultisite
|
|
|
9
9
|
initializer "RailsMultisite.init" do |app|
|
|
10
10
|
app.config.multisite = false
|
|
11
11
|
|
|
12
|
-
config_file =
|
|
12
|
+
config_file =
|
|
13
|
+
app.config.respond_to?(:multisite_config_path) &&
|
|
14
|
+
app.config.multisite_config_path.presence
|
|
15
|
+
|
|
16
|
+
config_file ||= ConnectionManagement.default_config_filename
|
|
17
|
+
|
|
13
18
|
if File.exist?(config_file)
|
|
14
|
-
ConnectionManagement.config_filename =
|
|
19
|
+
ConnectionManagement.config_filename = config_file
|
|
15
20
|
app.config.multisite = true
|
|
16
21
|
Rails.logger.formatter = RailsMultisite::Formatter.new
|
|
17
22
|
|
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:
|
|
4
|
+
version: 3.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sam Saffron
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2021-09-10 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|
|
@@ -61,6 +61,8 @@ files:
|
|
|
61
61
|
- README.md
|
|
62
62
|
- lib/rails_multisite.rb
|
|
63
63
|
- lib/rails_multisite/connection_management.rb
|
|
64
|
+
- lib/rails_multisite/connection_management/rails_60_compat.rb
|
|
65
|
+
- lib/rails_multisite/connection_management/rails_61_compat.rb
|
|
64
66
|
- lib/rails_multisite/formatter.rb
|
|
65
67
|
- lib/rails_multisite/middleware.rb
|
|
66
68
|
- lib/rails_multisite/railtie.rb
|
|
@@ -85,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
85
87
|
- !ruby/object:Gem::Version
|
|
86
88
|
version: '0'
|
|
87
89
|
requirements: []
|
|
88
|
-
rubygems_version: 3.1.
|
|
90
|
+
rubygems_version: 3.1.6
|
|
89
91
|
signing_key:
|
|
90
92
|
specification_version: 4
|
|
91
93
|
summary: Multi tenancy support for Rails
|