Skip to content

Commit 26eaf35

Browse files
Refactored configuration and added a new skip_teardown option
1 parent 20e81a2 commit 26eaf35

File tree

8 files changed

+90
-51
lines changed

8 files changed

+90
-51
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ source "https://rubygems.org"
55
# Specify your gem's dependencies in rails-nginx.gemspec
66
gemspec
77

8+
gem "debug"
89
gem "rake"
910
gem "rspec"
1011
gem "standard"

Gemfile.lock

+13-9
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ GEM
9090
connection_pool (2.5.0)
9191
crass (1.0.6)
9292
date (3.4.1)
93+
debug (1.10.0)
94+
irb (~> 1.10)
95+
reline (>= 0.3.8)
9396
diff-lcs (1.6.0)
9497
drb (2.2.1)
9598
erb (4.0.4)
@@ -130,18 +133,18 @@ GEM
130133
net-smtp (0.5.1)
131134
net-protocol
132135
nio4r (2.7.4)
133-
nokogiri (1.18.2)
136+
nokogiri (1.18.3)
134137
mini_portile2 (~> 2.8.2)
135138
racc (~> 1.4)
136-
nokogiri (1.18.2-aarch64-linux-gnu)
139+
nokogiri (1.18.3-aarch64-linux-gnu)
137140
racc (~> 1.4)
138-
nokogiri (1.18.2-arm-linux-gnu)
141+
nokogiri (1.18.3-arm-linux-gnu)
139142
racc (~> 1.4)
140-
nokogiri (1.18.2-arm64-darwin)
143+
nokogiri (1.18.3-arm64-darwin)
141144
racc (~> 1.4)
142-
nokogiri (1.18.2-x86_64-darwin)
145+
nokogiri (1.18.3-x86_64-darwin)
143146
racc (~> 1.4)
144-
nokogiri (1.18.2-x86_64-linux-gnu)
147+
nokogiri (1.18.3-x86_64-linux-gnu)
145148
racc (~> 1.4)
146149
parallel (1.26.3)
147150
parser (3.3.7.1)
@@ -248,7 +251,7 @@ GEM
248251
standard-performance (1.6.0)
249252
lint_roller (~> 1.1)
250253
rubocop-performance (~> 1.23.0)
251-
stringio (3.1.3)
254+
stringio (3.1.5)
252255
thor (1.3.2)
253256
timeout (0.4.3)
254257
tty-color (0.6.0)
@@ -259,13 +262,13 @@ GEM
259262
unicode-display_width (3.1.4)
260263
unicode-emoji (~> 4.0, >= 4.0.4)
261264
unicode-emoji (4.0.4)
262-
uri (1.0.2)
265+
uri (1.0.3)
263266
useragent (0.16.11)
264267
websocket-driver (0.7.7)
265268
base64
266269
websocket-extensions (>= 0.1.0)
267270
websocket-extensions (0.1.5)
268-
zeitwerk (2.7.1)
271+
zeitwerk (2.7.2)
269272

270273
PLATFORMS
271274
aarch64-linux
@@ -276,6 +279,7 @@ PLATFORMS
276279
x86_64-linux
277280

278281
DEPENDENCIES
282+
debug
279283
rails-nginx!
280284
rake
281285
rspec

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ By default, Rails NGINX will configure [Ruby NGINX](https://github.com/bert-mccu
107107
| `ssl_certificate_key_path` | `Rails.root` + `tmp/nginx/_[DOMAIN]-key.pem` | `/home/bert/hello_world/tmp/nginx/_hello-world.test-key.pem` |
108108
| `access_log_path` | `Rails.root` + `log/nginx/[DOMAIN].access.log` | `/home/bert/hello_world/log/hello-world.test.access.log` |
109109
| `error_log_path` | `Rails.root` + `log/nginx/[DOMAIN].error.log` | `/home/bert/hello_world/log/hello-world.test.error.log` |
110+
| `skip_teardown`* | `false` | |
111+
112+
\* Skipping teardown simply leaves the NGINX configuration and hosts mapping in place. This can be useful if you require elevated permissions to change the hosts file, and you don't want to be asked to enter your credentials every time. Any attempts to visit the server's domain while the server is offline will result in a NGINX error page.
110113
111114
### Advanced Usage
112115

bin/dummy

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ bundle install
1818

1919
# We need Puma 6.50 for the new on_stopped hook.
2020
bundle remove puma
21-
bundle add puma ">= 6.5.0"
21+
bundle add puma --version ">= 6.5.0"
2222

2323
# Configure Puma with rails-nginx
2424
cat >> ./config/puma.rb << EOF

lib/rails/nginx.rb

+13-27
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "socket"
44
require "ruby/nginx"
5+
require_relative "nginx/configuration"
56
require_relative "nginx/version"
67

78
module Rails
@@ -13,48 +14,28 @@ def self.port
1314
end
1415

1516
def self.start!(options = {}, &block)
16-
return unless rails_server?
17+
rails_config = Configuration.new(options)
18+
return unless rails_config.rails_server?
1719

18-
config = Ruby::Nginx.add!(options.reverse_merge(defaults(options)), &block)
20+
config = Ruby::Nginx.add!(rails_config.nginx_options, &block)
1921

2022
puts start_message(config.options)
2123
rescue Ruby::Nginx::Error => e
2224
abort "[Ruby::Nginx] #{e.message}"
2325
end
2426

2527
def self.stop!(options = {}, &block)
26-
return unless rails_server?
28+
rails_config = Configuration.new(options)
29+
return unless rails_config.rails_server? && rails_config.teardown?
2730

28-
Ruby::Nginx.remove!(options.reverse_merge(defaults(options)), &block)
31+
puts stop_message(rails_config.nginx_options)
32+
Ruby::Nginx.remove!(rails_config.nginx_options, &block)
2933
rescue Ruby::Nginx::Error => e
3034
abort "[Ruby::Nginx] #{e.message}"
3135
end
3236

3337
private
3438

35-
def self.rails_server?
36-
defined?(Rails::Server)
37-
end
38-
private_class_method :rails_server?
39-
40-
def self.defaults(options)
41-
domain = options[:domain] || "#{Rails.application.class.module_parent.name.underscore.dasherize}.test"
42-
43-
{
44-
domain: domain,
45-
port: port,
46-
root_path: Rails.public_path,
47-
template_path: File.expand_path("nginx/templates/nginx.conf.erb", __dir__),
48-
ssl: true,
49-
log: true,
50-
ssl_certificate_path: Rails.root.join("tmp/nginx/_#{domain}.pem"),
51-
ssl_certificate_key_path: Rails.root.join("tmp/nginx/_#{domain}-key.pem"),
52-
access_log_path: Rails.root.join("log/nginx/#{domain}.access.log"),
53-
error_log_path: Rails.root.join("log/nginx/#{domain}.error.log")
54-
}
55-
end
56-
private_class_method :defaults
57-
5839
def self.start_message(config)
5940
message = ["* Rails NGINX version: #{Rails::Nginx::VERSION}"]
6041
message << "* HTTP Endpoint: http://#{config[:domain]}"
@@ -65,5 +46,10 @@ def self.start_message(config)
6546
message.join("\n")
6647
end
6748
private_class_method :start_message
49+
50+
def self.stop_message(config)
51+
"- Tearing down Rails NGINX config: #{config[:domain]}"
52+
end
53+
private_class_method :stop_message
6854
end
6955
end

lib/rails/nginx/configuration.rb

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
module Rails
4+
module Nginx
5+
class Configuration
6+
def initialize(options = {})
7+
@options = options if rails_server?
8+
end
9+
10+
def rails_server?
11+
defined?(Rails::Server)
12+
end
13+
14+
def teardown?
15+
!@options[:skip_teardown]
16+
end
17+
18+
def nginx_options
19+
defaults.merge(@options).except(:skip_teardown)
20+
end
21+
22+
private
23+
24+
def domain
25+
@options[:domain] || "#{Rails.application.class.module_parent.name.underscore.dasherize}.test"
26+
end
27+
28+
def defaults
29+
{
30+
domain: domain,
31+
port: ENV["PORT"],
32+
root_path: Rails.public_path,
33+
template_path: File.expand_path("templates/nginx.conf.erb", __dir__),
34+
ssl: true,
35+
log: true,
36+
ssl_certificate_path: Rails.root.join("tmp/nginx/_#{domain}.pem"),
37+
ssl_certificate_key_path: Rails.root.join("tmp/nginx/_#{domain}-key.pem"),
38+
access_log_path: Rails.root.join("log/nginx/#{domain}.access.log"),
39+
error_log_path: Rails.root.join("log/nginx/#{domain}.error.log"),
40+
skip_teardown: false
41+
}
42+
end
43+
end
44+
end
45+
end

spec/dummy/Gemfile.lock

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ../..
33
specs:
4-
rails-nginx (1.0.0.pre.beta.5)
4+
rails-nginx (1.0.0.pre.beta.6)
55
puma
66
rails
77
ruby-nginx (>= 1.0.0.pre.beta.4)
@@ -84,7 +84,7 @@ GEM
8484
benchmark (0.4.0)
8585
bigdecimal (3.1.9)
8686
builder (3.3.0)
87-
cgi (0.4.1)
87+
cgi (0.4.2)
8888
concurrent-ruby (1.3.5)
8989
connection_pool (2.5.0)
9090
crass (1.0.6)
@@ -127,21 +127,21 @@ GEM
127127
net-smtp (0.5.1)
128128
net-protocol
129129
nio4r (2.7.4)
130-
nokogiri (1.18.2-aarch64-linux-gnu)
130+
nokogiri (1.18.3-aarch64-linux-gnu)
131131
racc (~> 1.4)
132-
nokogiri (1.18.2-aarch64-linux-musl)
132+
nokogiri (1.18.3-aarch64-linux-musl)
133133
racc (~> 1.4)
134-
nokogiri (1.18.2-arm-linux-gnu)
134+
nokogiri (1.18.3-arm-linux-gnu)
135135
racc (~> 1.4)
136-
nokogiri (1.18.2-arm-linux-musl)
136+
nokogiri (1.18.3-arm-linux-musl)
137137
racc (~> 1.4)
138-
nokogiri (1.18.2-arm64-darwin)
138+
nokogiri (1.18.3-arm64-darwin)
139139
racc (~> 1.4)
140-
nokogiri (1.18.2-x86_64-darwin)
140+
nokogiri (1.18.3-x86_64-darwin)
141141
racc (~> 1.4)
142-
nokogiri (1.18.2-x86_64-linux-gnu)
142+
nokogiri (1.18.3-x86_64-linux-gnu)
143143
racc (~> 1.4)
144-
nokogiri (1.18.2-x86_64-linux-musl)
144+
nokogiri (1.18.3-x86_64-linux-musl)
145145
racc (~> 1.4)
146146
pastel (0.8.0)
147147
tty-color (~> 0.5)
@@ -201,21 +201,21 @@ GEM
201201
thor
202202
tty-command (~> 0.10, >= 0.10.1)
203203
securerandom (0.4.1)
204-
stringio (3.1.3)
204+
stringio (3.1.5)
205205
thor (1.3.2)
206206
timeout (0.4.3)
207207
tty-color (0.6.0)
208208
tty-command (0.10.1)
209209
pastel (~> 0.8)
210210
tzinfo (2.0.6)
211211
concurrent-ruby (~> 1.0)
212-
uri (1.0.2)
212+
uri (1.0.3)
213213
useragent (0.16.11)
214214
websocket-driver (0.7.7)
215215
base64
216216
websocket-extensions (>= 0.1.0)
217217
websocket-extensions (0.1.5)
218-
zeitwerk (2.7.1)
218+
zeitwerk (2.7.2)
219219

220220
PLATFORMS
221221
aarch64-linux-gnu

spec/support/dummy_server.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
ENV["SKIP_PROMPT"] = "true"
44
APP_PATH = File.expand_path("../dummy/config/application", __dir__)
5-
ARGV = ["server"]
5+
ARGV = ["server", "-p", "3001"]
66

77
class DummyServer
88
include Singleton

0 commit comments

Comments
 (0)